Skip to content

Commit

Permalink
Fix implementations for draft 2020-12 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior authored Oct 25, 2024
1 parent 2ec671a commit 2598ad6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
16 changes: 7 additions & 9 deletions implementations/ajv/main.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Ajv from 'ajv';
import draft7schema from 'ajv/lib/refs/json-schema-draft-07.json' with { type: 'json' };
import fs from 'fs';
import readline from 'readline';
import { performance } from 'perf_hooks';

const DRAFTS = {
"https://json-schema.org/draft/2020-12/schema": (await import("ajv/dist/2020.js")).Ajv2020,
"https://json-schema.org/draft/2019-09/schema": (await import("ajv/dist/2019.js")).Ajv2019,
"http://json-schema.org/draft-07/schema#": (await import("ajv")).Ajv,
};

function readJSONFile(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
Expand All @@ -25,13 +29,7 @@ async function* readJSONLines(filePath) {
async function validateSchema(schemaPath, instancePath) {
const schema = readJSONFile(schemaPath);

const ajv = new Ajv({
schemaId: 'id',
meta: false,
validateSchema: false
});

ajv.addMetaSchema(draft7schema);
const ajv = new DRAFTS[schema['$schema']]({strict: false});
const validate = ajv.compile(schema);

const instances = [];
Expand Down
51 changes: 21 additions & 30 deletions implementations/ajv/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implementations/ajv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "1.0.0",
"author": "Juan Cruz Viotti <[email protected]>",
"dependencies": {
"ajv": "^6.12.6"
"ajv": "^8.17.1"
}
}
7 changes: 6 additions & 1 deletion implementations/hyperjump/main.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { registerSchema, validate } from "@hyperjump/json-schema/draft-07";
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
import fs from 'fs';
import readline from 'readline';
import { performance } from 'perf_hooks';

await Promise.all([
import("@hyperjump/json-schema/draft-2019-09"),
import("@hyperjump/json-schema/draft-07"),
]);

function readJSONFile(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
Expand Down
3 changes: 2 additions & 1 deletion implementations/python-jsonschema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
schema = json.load(open(example_dir / "schema.json"))
instances = [json.loads(doc) for doc in open(example_dir / "instances.jsonl").readlines()]

validator = jsonschema.Draft4Validator(schema)
Validator = jsonschema.validators.validator_for(schema)
validator = Validator(schema)

start = time.time_ns()
for instance in instances:
Expand Down

0 comments on commit 2598ad6

Please sign in to comment.