Skip to content

Commit c04fe3d

Browse files
committed
Add support for the relative-json-pointer format
1 parent b45f474 commit c04fe3d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

formats/formats-test-suite.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ const skip = new Set<string>([
4949
"|draft2019-09|idn-hostname.json",
5050
"|draft7|idn-hostname.json",
5151

52-
// Not supported
53-
"|draft2020-12|relative-json-pointer.json",
54-
"|draft2019-09|relative-json-pointer.json",
55-
"|draft7|relative-json-pointer.json",
56-
5752
// Leap seconds don't make sense without a date
5853
"|draft2020-12|time.json|validation of time strings|a valid time string with leap second, Zulu",
5954
"|draft2019-09|time.json|validation of time strings|a valid time string with leap second, Zulu",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const unescaped = `[\\u{00}-\\u{2E}\\u{30}-\\u{7D}\\u{7F}-\\u{10FFFF}]`; // %x2F ('/') and %x7E ('~') are excluded from 'unescaped'
2+
const escaped = `~[01]`; // representing '~' and '/', respectively
3+
const referenceToken = `(?:${unescaped}|${escaped})*`;
4+
const jsonPointer = `(?:/${referenceToken})*`;
5+
6+
const nonNegativeInteger = `(?:0|[1-9][0-9]*)`;
7+
const indexManipulation = `(?:[+-]${nonNegativeInteger})`;
8+
const relativeJsonPointer = `${nonNegativeInteger}(?:${indexManipulation}?${jsonPointer}|#)`;
9+
10+
export const isRelativeJsonPointer = RegExp.prototype.test.bind(new RegExp(`^${relativeJsonPointer}$`, "u"));
11+
12+
export default {
13+
id: "https://json-schema.org/format/relative-json-pointer",
14+
handler: (pointer) => typeof pointer !== "string" || isRelativeJsonPointer(pointer)
15+
};

formats/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import iriReference from "./handlers/iri-reference.js";
1313
import uuid from "./handlers/uuid.js";
1414
import uriTemplate from "./handlers/uri-template.js";
1515
import jsonPointer from "./handlers/json-pointer.js";
16+
import relativeJsonPointer from "./handlers/relative-json-pointer.js";
1617
import regex from "./handlers/regex.js";
1718

1819

@@ -29,6 +30,7 @@ addFormat(iriReference);
2930
addFormat(uuid);
3031
addFormat(uriTemplate);
3132
addFormat(jsonPointer);
33+
addFormat(relativeJsonPointer);
3234
addFormat(regex);
3335

3436
export {

0 commit comments

Comments
 (0)