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 a385849..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 @@ -159,8 +159,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, instanceLocation, context); /** @type API.EvaluationContext */ const keywordContext = { @@ -178,7 +177,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]?.[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 609e42e..38ec3a6 100644 --- a/src/test-suite/tests/propertyNames.json +++ b/src/test-suite/tests/propertyNames.json @@ -48,6 +48,137 @@ "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 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": {