Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down
7 changes: 3 additions & 4 deletions src/json-schema-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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;
}
Expand Down
131 changes: 131 additions & 0 deletions src/test-suite/tests/propertyNames.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Loading