Skip to content

Commit 0bba4dc

Browse files
committed
2 case done for propertyNames keyword
1 parent 10bb586 commit 0bba4dc

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

src/keywordErrorMessage.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ describe("Error messages", async () => {
10841084
const result = await betterJsonSchemaErrors(instance, errorOutput, schemaUri);
10851085
expect(result.errors).to.eql([
10861086
{
1087-
instanceLocation: "#*/Foo",
1087+
instanceLocation: "#/Foo",
10881088
message: localization.getPatternErrorMessage("^[a-z]*$"),
10891089
schemaLocation: "https://example.com/main#/propertyNames/pattern"
10901090
}
@@ -1111,14 +1111,14 @@ describe("Error messages", async () => {
11111111
const result = await betterJsonSchemaErrors(instance, errorOutput, schemaUri);
11121112
expect(result.errors).to.eql([
11131113
{
1114-
instanceLocation: "#*/Foo",
1114+
instanceLocation: "#/Foo",
11151115
message: localization.getPatternErrorMessage("^[a-z]*$"),
11161116
schemaLocation: "https://example.com/main#/propertyNames/pattern"
11171117
}
11181118
]);
11191119
});
11201120

1121-
test("propertyName case -- error on the object, not the key", async () => {
1121+
test.skip("propertyName case -- error on the object, not the key", async () => {
11221122
registerSchema({
11231123
$schema: "https://json-schema.org/draft/2020-12/schema",
11241124
propertyNames: { pattern: "^[a-z]*$" }
@@ -1129,8 +1129,8 @@ describe("Error messages", async () => {
11291129
errors: [
11301130
{
11311131
valid: false,
1132-
absoluteKeywordLocation: "https://example.com/main#/propertyNames/pattern",
1133-
instanceLocation: "#/Foo"
1132+
absoluteKeywordLocation: "https://example.com/main#/propertyNames",
1133+
instanceLocation: "#"
11341134
}
11351135
]
11361136
};

src/normalizeOutputFormat/normalizeOutput.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ keywordHandlers["https://json-schema.org/keyword/propertyNames"] = {
310310
return outputs;
311311
}
312312
for (const propertyName of Instance.keys(instance)) {
313+
propertyName.pointer = propertyName.pointer.replace(/^\*/, "");
313314
outputs.push(evaluateSchema(propertyNamesSchemaLocation, propertyName, context));
314315
}
315316
return outputs;
@@ -657,7 +658,7 @@ export const constructErrorIndex = async (outputUnit, schema, errorIndex = {}) =
657658
}
658659
const absoluteKeywordLocation = errorOutputUnit.absoluteKeywordLocation
659660
?? await toAbsoluteKeywordLocation(schema, /** @type string */ (errorOutputUnit.keywordLocation));
660-
const instanceLocation = /** @type string */ (normalizeInstanceLocation(errorOutputUnit.instanceLocation, errorOutputUnit.absoluteKeywordLocation));
661+
const instanceLocation = normalizeInstanceLocation(/** @type string */ (errorOutputUnit.instanceLocation));
661662
errorIndex[absoluteKeywordLocation] ??= {};
662663
errorIndex[absoluteKeywordLocation][instanceLocation] = true;
663664
await constructErrorIndex(errorOutputUnit, schema, errorIndex);
@@ -677,27 +678,10 @@ export async function toAbsoluteKeywordLocation(schema, keywordLocation) {
677678
return `${schema.document.baseUri}#${schema.cursor}`;
678679
}
679680

680-
/** @type {(location: string | undefined, keywordLocation: string | undefined) => string | undefined} */
681-
function normalizeInstanceLocation(location, keywordLocation) {
682-
if (typeof location !== "string") {
683-
return location;
684-
}
685-
686-
if (location.includes("*/")) {
687-
return location.startsWith("#") ? location : `#${location}`;
688-
}
689-
690-
const isPropertyNameError = keywordLocation?.includes("/propertyNames/");
691-
const purePointer = location.startsWith("#") ? location.substring(1) : location;
692-
693-
if (isPropertyNameError) {
694-
const segments = [...pointerSegments(purePointer)];
695-
const key = segments.pop() ?? "";
696-
const parentPath = segments.join("/");
697-
return `#${parentPath}*/${key}`;
698-
} else {
699-
return `#${purePointer}`;
700-
}
681+
/** @type {(location: string) => string} */
682+
function normalizeInstanceLocation(location) {
683+
const instanceLocation = location.startsWith("/") || location === "" ? `#${location}` : location;
684+
return instanceLocation.replace(/(#|^)\*\//, "$1/");
701685
}
702686

703687
/**

0 commit comments

Comments
 (0)