Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multiple instances #1

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dist/results/jsontoolkit/%: \
implementations/jsontoolkit/CMakeLists.txt \
implementations/jsontoolkit/main.cc \
schemas/%/schema.json \
schemas/%/instance.json \
schemas/%/instances.json \
| dist/results/jsontoolkit dist/temp/jsontoolkit
[ -d $(word 2,$|)/repo ] && git -C $(word 2,$|)/repo pull || git clone https://github.com/sourcemeta/jsontoolkit $(word 2,$|)/repo
cmake -S $(dir $<) -B $(word 2,$|)/build -DCMAKE_BUILD_TYPE:STRING=Release -DBUILD_SHARED_LIBS:BOOL=OFF
Expand All @@ -34,9 +34,9 @@ dist/results/jsontoolkit/%: \
# AJV

dist/results/ajv/%: \
implementations/ajv/main.js \
implementations/ajv/main.mjs \
schemas/%/schema.json \
schemas/%/instance.json \
schemas/%/instances.json \
node_modules \
| dist/results/ajv
node $< $(word 2,$^) $(word 3,$^) > $@
28 changes: 20 additions & 8 deletions implementations/ajv/main.js → implementations/ajv/main.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Ajv = require('ajv');
const draft4schema = require('ajv/lib/refs/json-schema-draft-04.json');
const fs = require('fs');
const { performance } = require('perf_hooks');
import Ajv from 'ajv';
import draft4schema from 'ajv/lib/refs/json-schema-draft-04.json' assert { type: 'json' };
import fs from 'fs';
import readline from 'readline';
import { performance } from 'perf_hooks';

function readJSONFile(filePath) {
try {
Expand All @@ -12,7 +13,16 @@ function readJSONFile(filePath) {
}
}

function validateSchema(schemaPath, instancePath) {
async function* readJSONLines(filePath) {
const rl = readline.createInterface({
input: fs.createReadStream(filePath),
});
for await (const line of rl) {
yield JSON.parse(line);
}
}

async function validateSchema(schemaPath, instancePath) {
const schema = readJSONFile(schemaPath);
const instance = readJSONFile(instancePath);

Expand All @@ -26,8 +36,10 @@ function validateSchema(schemaPath, instancePath) {
const validate = ajv.compile(schema);

const startTime = performance.now();
if (!validate(instance)) {
process.exit(1);
for await (const instance of readJSONLines(instancePath)) {
if (!validate(instance)) {
michaelmior marked this conversation as resolved.
Show resolved Hide resolved
process.exit(1);
}
}

const endTime = performance.now();
Expand All @@ -43,4 +55,4 @@ if (process.argv.length !== 4) {
const schemaPath = process.argv[2];
const instancePath = process.argv[3];

validateSchema(schemaPath, instancePath);
await validateSchema(schemaPath, instancePath);
46 changes: 0 additions & 46 deletions schemas/example/instance.json

This file was deleted.

1 change: 1 addition & 0 deletions schemas/example/instances.json
michaelmior marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"storeName":"TechGadgets Online","lastUpdated":"2024-08-27T14:30:00Z","categories":["Electronics","Accessories","Smart Home"],"products":[{"id":"PRD-EL-000001","name":"SuperSound Wireless Headphones","description":"High-quality wireless headphones with noise-cancelling technology.","price":129.99,"category":"Electronics","tags":["audio","wireless","headphones"],"inStock":true,"rating":{"average":4.7,"count":253}},{"id":"PRD-AC-000015","name":"DuraPro Phone Case","description":"Rugged phone case for ultimate protection.","price":24.95,"category":"Accessories","tags":["phone","protection"],"inStock":true,"rating":{"average":4.2,"count":187}},{"id":"PRD-SH-000008","name":"SmartHome Hub","description":"Central control unit for your smart home devices.","price":79.99,"category":"Smart Home","tags":["iot","control","automation"],"inStock":false,"rating":{"average":4.5,"count":42}}]}
Loading