From 00369a442aab43315a4486d22caf2a041b8ff5b8 Mon Sep 17 00:00:00 2001 From: MohitSAGAR11 Date: Sun, 8 Nov 2026 10:04:29 +0530 Subject: [PATCH 1/2] fix: report false schemas under propertyNames The error index stores property name locations without the "*" ("#*/foo" is indexed as "#/foo"). Keywords looked errors up that way, but boolean schemas used the location as is, so a false schema under propertyNames was treated as passing and produced no error. Fixes #302 --- src/json-schema-errors.js | 9 +-- src/test-suite/tests/propertyNames.json | 87 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/json-schema-errors.js b/src/json-schema-errors.js index a385849..f84a123 100644 --- a/src/json-schema-errors.js +++ b/src/json-schema-errors.js @@ -126,6 +126,8 @@ async function toAbsoluteKeywordLocation(schema, keywordLocation) { /** @type API.evaluateSchema */ export const evaluateSchema = (schemaLocation, instance, context) => { const instanceLocation = Instance.uri(instance); + // The error index stores property name locations ("#*/foo") without the "*" + const indexedInstanceLocation = instanceLocation.replace(/^#\*/, "#"); let valid = true; /** @type API.NormalizedOutput */ @@ -137,7 +139,7 @@ export const evaluateSchema = (schemaLocation, instance, context) => { const schemaNode = context.ast[schemaLocation]; if (typeof schemaNode === "boolean") { - const isSchemaValid = getReportedValidity(schemaLocation, instanceLocation, context); + const isSchemaValid = getReportedValidity(schemaLocation, indexedInstanceLocation, context); if (schemaNode === false && isSchemaValid !== true) { valid = false; output[instanceLocation] = { @@ -159,8 +161,7 @@ export const evaluateSchema = (schemaLocation, instance, context) => { const validationKeyword = getKeyword(keywordUri); - const keywordInstanceLocation = instanceLocation.replace(/^#\*/, "#"); - let isKeywordValid = getReportedValidity(keywordLocation, keywordInstanceLocation, context); + let isKeywordValid = getReportedValidity(keywordLocation, indexedInstanceLocation, context); /** @type API.EvaluationContext */ const keywordContext = { @@ -178,7 +179,7 @@ export const evaluateSchema = (schemaLocation, instance, context) => { const keywordOutput = keyword.evaluate?.(keywordValue, instance, keywordContext); - const isReported = context.errorIndex[keywordLocation]?.[keywordInstanceLocation] !== undefined; + const isReported = context.errorIndex[keywordLocation]?.[indexedInstanceLocation] !== undefined; if (validationKeyword.simpleApplicator && !isReported && keywordOutput?.some((suboutput) => getValidity(suboutput) === false)) { isKeywordValid = false; } diff --git a/src/test-suite/tests/propertyNames.json b/src/test-suite/tests/propertyNames.json index 609e42e..169b831 100644 --- a/src/test-suite/tests/propertyNames.json +++ b/src/test-suite/tests/propertyNames.json @@ -48,6 +48,93 @@ "instance": 42, "errors": [] }, + { + "description": "propertyNames false", + "compatibility": "6", + "schema": { + "propertyNames": false + }, + "instance": { "foo": 42 }, + "errors": [ + { + "messageId": "boolean-schema-message", + "instanceLocation": "#*/foo", + "schemaLocations": ["#/propertyNames"] + } + ] + }, + { + "description": "propertyNames with a false schema in allOf", + "compatibility": "6", + "schema": { + "propertyNames": { "allOf": [false] } + }, + "instance": { "foo": 42 }, + "errors": [ + { + "messageId": "boolean-schema-message", + "instanceLocation": "#*/foo", + "schemaLocations": ["#/propertyNames/allOf/0"] + } + ] + }, + { + "description": "propertyNames with a $ref to a false schema", + "compatibility": "6", + "schema": { + "propertyNames": { "$ref": "#/$defs/never" }, + "$defs": { + "never": false + } + }, + "instance": { "foo": 42 }, + "errors": [ + { + "messageId": "boolean-schema-message", + "instanceLocation": "#*/foo", + "schemaLocations": ["#/$defs/never"] + } + ] + }, + { + "description": "propertyNames false in anyOf", + "compatibility": "6", + "schema": { + "anyOf": [ + { "propertyNames": false }, + { "type": "string" } + ] + }, + "instance": { "foo": 42 }, + "errors": [ + { + "messageId": "anyOf-message", + "alternatives": [ + [ + { + "messageId": "boolean-schema-message", + "instanceLocation": "#*/foo", + "schemaLocations": ["#/anyOf/0/propertyNames"] + } + ], + [ + { + "messageId": "type-message", + "messageParams": { + "count": 1, + "type": "string", + "expectedTypes": { "or": ["string"] } + }, + "instanceLocation": "#", + "schemaLocations": ["#/anyOf/1/type"] + } + ] + ], + "instanceLocation": "#", + "schemaLocations": ["#/anyOf"] + } + ] + }, { "description": "propertyNames pass", "schema": { From db6a4a5c3ab8e025cc3e3b6c4391f82b596aa8ef Mon Sep 17 00:00:00 2001 From: Jason Desrosiers Date: Sat, 14 Nov 2026 16:53:52 -0800 Subject: [PATCH 2/2] fix: keep property name locations distinct in the error index The error index stripped the "*" from property name locations, so "#*/foo" and "#/foo" shared a key. A false schema under propertyNames was looked up with the "*" and found nothing (#302), and when propertyNames and a property value used the same subschema, an error on one was reported on the other. Index instance locations as the validator reports them and look them up exactly. Validators that don't use "*" for property names no longer have their property name errors found; a fallback or keywordLocation support can be added if there's demand. Fixes #302 Co-Authored-By: Claude Opus 5.5 --- src/index.d.ts | 5 ++- src/json-schema-errors.js | 10 +++--- src/test-suite/tests/propertyNames.json | 44 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index da1dd28..1a710c0 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -163,7 +163,10 @@ export type EvaluationContext = { /** * The validation results found in the validator's output. `true` means the - * keyword failed and `false` means the output says it passed. + * keyword failed and `false` means the output says it passed. Instance + * locations are as the validator reported them, prefixed with `#`. A property + * name's location is only distinct from its value's if the validator marks it + * with a `*` after the `#`. */ export type ErrorIndex = { [schemaLocation: string]: { diff --git a/src/json-schema-errors.js b/src/json-schema-errors.js index f84a123..362d4c1 100644 --- a/src/json-schema-errors.js +++ b/src/json-schema-errors.js @@ -99,7 +99,7 @@ const indexOutputUnits = async (outputUnit, schema, errorIndex) => { const absoluteKeywordLocation = subOutputUnit.absoluteKeywordLocation ?? await toAbsoluteKeywordLocation(schema, /** @type string */ (subOutputUnit.keywordLocation)); const instanceLocation = /** @type string */ (subOutputUnit.instanceLocation) - .replace(/^#?\*?/, "#"); + .replace(/^#?/, "#"); errorIndex[absoluteKeywordLocation] ??= {}; // If results conflict, the error wins @@ -126,8 +126,6 @@ async function toAbsoluteKeywordLocation(schema, keywordLocation) { /** @type API.evaluateSchema */ export const evaluateSchema = (schemaLocation, instance, context) => { const instanceLocation = Instance.uri(instance); - // The error index stores property name locations ("#*/foo") without the "*" - const indexedInstanceLocation = instanceLocation.replace(/^#\*/, "#"); let valid = true; /** @type API.NormalizedOutput */ @@ -139,7 +137,7 @@ export const evaluateSchema = (schemaLocation, instance, context) => { const schemaNode = context.ast[schemaLocation]; if (typeof schemaNode === "boolean") { - const isSchemaValid = getReportedValidity(schemaLocation, indexedInstanceLocation, context); + const isSchemaValid = getReportedValidity(schemaLocation, instanceLocation, context); if (schemaNode === false && isSchemaValid !== true) { valid = false; output[instanceLocation] = { @@ -161,7 +159,7 @@ export const evaluateSchema = (schemaLocation, instance, context) => { const validationKeyword = getKeyword(keywordUri); - let isKeywordValid = getReportedValidity(keywordLocation, indexedInstanceLocation, context); + let isKeywordValid = getReportedValidity(keywordLocation, instanceLocation, context); /** @type API.EvaluationContext */ const keywordContext = { @@ -179,7 +177,7 @@ export const evaluateSchema = (schemaLocation, instance, context) => { const keywordOutput = keyword.evaluate?.(keywordValue, instance, keywordContext); - const isReported = context.errorIndex[keywordLocation]?.[indexedInstanceLocation] !== undefined; + const isReported = context.errorIndex[keywordLocation]?.[instanceLocation] !== undefined; if (validationKeyword.simpleApplicator && !isReported && keywordOutput?.some((suboutput) => getValidity(suboutput) === false)) { isKeywordValid = false; } diff --git a/src/test-suite/tests/propertyNames.json b/src/test-suite/tests/propertyNames.json index 169b831..38ec3a6 100644 --- a/src/test-suite/tests/propertyNames.json +++ b/src/test-suite/tests/propertyNames.json @@ -135,6 +135,50 @@ } ] }, + { + "description": "propertyNames and a property value share a subschema and only the value fails", + "compatibility": "6", + "schema": { + "properties": { + "foo": { "$ref": "#/definitions/short" } + }, + "propertyNames": { "$ref": "#/definitions/short" }, + "definitions": { + "short": { "maxLength": 4 } + } + }, + "instance": { "foo": "abcdef" }, + "errors": [ + { + "messageId": "maxLength-message", + "messageParams": { "maxLength": 4 }, + "instanceLocation": "#/foo", + "schemaLocations": ["#/definitions/short/maxLength"] + } + ] + }, + { + "description": "propertyNames and a property value share a subschema and only the name fails", + "compatibility": "6", + "schema": { + "properties": { + "foobar": { "$ref": "#/definitions/short" } + }, + "propertyNames": { "$ref": "#/definitions/short" }, + "definitions": { + "short": { "maxLength": 4 } + } + }, + "instance": { "foobar": "abc" }, + "errors": [ + { + "messageId": "maxLength-message", + "messageParams": { "maxLength": 4 }, + "instanceLocation": "#*/foobar", + "schemaLocations": ["#/definitions/short/maxLength"] + } + ] + }, { "description": "propertyNames pass", "schema": {