Skip to content

Conversation

@srivastava-diya
Copy link
Contributor

@srivastava-diya srivastava-diya commented Jan 16, 2026

Added Support for JSON Schema Draft-04 Keywords

I have implemented full support for the requested Draft-04 keywords in @hyperjump/json-schema-errors.
Issue : #119

Changes made

items and additionalItems

Draft-04 treats items and additionalItems as siblings where additionalItems validation depends on items (if items is an array).

  • Implementation: Implemented a items normalization handler in items.js.
  • The items handler infers the URI of the sibling additionalItems keyword (by string manipulation of the items URI) and retrieves the schema from the AST. It then acts as the validator for the "tail" items that fall under additionalItems criteria.

dependencies

Implemented dependencies.js to handle both:

  • Schema Dependencies: Delegating validation to the dependent schema.
  • Property Dependencies: Checking for missing required properties (handled in error handler).

Numeric Keywords (maximum, minimum)

Implemented handlers maximum.js, minimum.js.

  • These keywords depend on keywords like exclusiveMaximum (boolean).
  • The error handlers navigate up the schema URI hierarchy to retrieve the parent schema and check the sibling values (e.g. exclusiveMaximum) to construct the correct error message ("less than" vs "less than or equal to").

Also Added test cases

@srivastava-diya
Copy link
Contributor Author

@jdesrosiers please have a look whenever you're free,because i feel a lot of it needs to be re iterated over

Copy link
Collaborator

@jdesrosiers jdesrosiers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a quick review. I didn't have enough time to look over everything in much detail.

Make sure you're working with @Adityakumar37 on the items/additionalItems keywords. I'm hoping to merge that PR first, then you can rebase and add the rest of the draft-04 implementation on top of that.

Anyway, your approach for those keywords isn't right. Those are simple applicators. Simple applicators don't need error handlers. Your normalization handlers should look a lot like the keywords from draft-2020-12 that serve the same purpose.

dependencies is a weird one because it's a simple applicator in some cases (schemas) and a validation keyword in other cases (string array). It can even do both at the same time. I need to go through that handler closer later, but it seems like it should be a problem.

Comment on lines 24 to 27
const parentUri = schemaLocation.replace(/\/[^/]+$/, "");
const parentSchema = await getSchema(parentUri);
const parentValue = /** @type {import("../../index.d.ts").JsonObject} */ (Schema.value(parentSchema));
const exclusiveMaximum = /** @type boolean | undefined */ (parentValue.exclusiveMaximum);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work in some cases when references are used. See the solution I used for minContains/maxContains in the contains handler for a better approach.

errors.push({
message: localization.getRequiredErrorMessage(missing),
instanceLocation: Instance.uri(instance),
schemaLocations: [`${schemaLocation}/${propertyPointer}`]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use schemaLocation here. You don't need a more specific pointer.

/** @type NormalizationHandler<any> */
const additionalItemsNormalizationHandler = {
evaluate(_params, _instance, _context) {
return [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning an empty array never makes sense. This should look a lot like draft-2020-12 items.

Comment on lines 29 to 35
const keywordOutput = dependenciesErrors[schemaLocation];
if (Array.isArray(keywordOutput)) {
for (const output of keywordOutput) {
const result = await getErrors(output, instance, localization);
errors.push(...result);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is how you got around the problem of dependencies being an applicator and a validator. I hadn't intended to use getErrors for this kind of thing, but it makes sense. 👍

Signed-off-by: Diya <diyasrivastava2023@gmail.com>
@srivastava-diya
Copy link
Contributor Author

hey @jdesrosiers ,
i have re-written a few normalization handlers in order to closely resemble some draft-2020-12 handlers. and foe the error handlers i think we only need it for dependencies , maximum, and minimum i have also changed them a bit and at the same time i am waiting for aditya's PR in order to rebase and work on top of those changes.

@srivastava-diya srivastava-diya changed the title Added Support for JSON Schema Draft-04 Keywords Add Support for JSON Schema Draft-04 Keywords Jan 19, 2026
Copy link
Collaborator

@jdesrosiers jdesrosiers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty close now. The only thing I haven't looked at yet is the tests.

Comment on lines +23 to +26
const parentLocation = schemaLocation.replace(/\/maximum$/, "");
const parent = await getSchema(parentLocation);
const exclusiveNode = await Schema.step("exclusiveMaximum", parent);
const exclusive = /** @type boolean */ (Schema.value(exclusiveNode) ?? false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't always work. You have to find the value using the keyword URI, not the keyword name. See the contains error handler for an example.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, contains isn't quite right either because it considers values from other schemas applied to the same location. You're going to have find a solution that doesn't hardcode the keyword name, but only considers keywords in the same subschema.

});
} else {
errors.push({
message: localization.getMaximumErrorMessage(maximum),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing I noticed that the messages for exclusiveMaximum and maximum are the same. This PR seems like a good opportunity to fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants