Skip to content

Commit 468e9d5

Browse files
committed
Add support for uri-template format
1 parent 55a7c1f commit 468e9d5

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

formats/formats-test-suite.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ const skip = new Set<string>([
8888
"|draft7|time.json|validation of time strings|valid leap second, negative time-offset",
8989
"|draft2020-12|time.json|validation of time strings|valid leap second, large negative time-offset",
9090
"|draft2019-09|time.json|validation of time strings|valid leap second, large negative time-offset",
91-
"|draft7|time.json|validation of time strings|valid leap second, large negative time-offset",
92-
93-
// Not supported
94-
"|draft2020-12|uri-template.json",
95-
"|draft2019-09|uri-template.json",
96-
"|draft7|uri-template.json",
97-
"|draft6|uri-template.json"
91+
"|draft7|time.json|validation of time strings|valid leap second, large negative time-offset"
9892
]);
9993

10094
const shouldSkip = (path: string[]): boolean => {

formats/handlers/uri-template.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const alpha = `[a-zA-Z]`;
2+
const hexdig = `[\\da-fA-F]`;
3+
const pctEncoded = `%${hexdig}${hexdig}`;
4+
5+
const ucschar = `[\\u{A0}-\\u{D7FF}\\u{F900}-\\u{FDCF}\\u{FDF0}-\\u{FFEF}\\u{10000}-\\u{1FFFD}\\u{20000}-\\u{2FFFD}\\u{30000}-\\u{3FFFD}\\u{40000}-\\u{4FFFD}\\u{50000}-\\u{5FFFD}\\u{60000}-\\u{6FFFD}\\u{70000}-\\u{7FFFD}\\u{80000}-\\u{8FFFD}\\u{90000}-\\u{9FFFD}\\u{A0000}-\\u{AFFFD}\\u{B0000}-\\u{BFFFD}\\u{C0000}-\\u{CFFFD}\\u{D0000}-\\u{DFFFD}\\u{E1000}-\\u{EFFFD}]`;
6+
7+
const iprivate = `[\\u{E000}-\\u{F8FF}\\u{F0000}-\\u{FFFFD}\\u{100000}-\\u{10FFFD}]`;
8+
9+
const opLevel2 = `[+#]`;
10+
const opLevel3 = `[./;?&]`;
11+
const opReserve = `[=,!@|]`;
12+
const operator = `(?:${opLevel2}|${opLevel3}${opReserve})`;
13+
14+
const varchar = `(?:${alpha}|\\d|_|${pctEncoded})`;
15+
const varname = `${varchar}(?:\\.?${varchar})*`;
16+
const maxLength = `(?:[1-9]|\\d{0,3})`; // positive integer < 10000
17+
const prefix = `:${maxLength}`;
18+
const explode = `\\*`;
19+
const modifierLevel4 = `(?:${prefix}|${explode})`;
20+
const varspec = `${varname}${modifierLevel4}?`;
21+
const variableList = `${varspec}(?:,${varspec})*`;
22+
23+
const expression = `\\{${operator}?${variableList}\\}`;
24+
25+
// any Unicode character except: CTL, SP, DQUOTE, "%" (aside from pct-encoded), "<", ">", "\", "^", "`", "{", "|", "}"
26+
const literals = `(?:[\\x21\\x23-\\x24\\x26-\\x3B\\x3D\\x3F-\\x5B\\x5D\\x5F\\x61-\\x7A\\x7E]|${ucschar}|${iprivate}|${pctEncoded})`;
27+
28+
const uriTemplate = `(?:${literals}|${expression})*`;
29+
30+
export const isUriTemplate = RegExp.prototype.test.bind(new RegExp(`^${uriTemplate}$`, "u"));
31+
32+
export default {
33+
id: "https://json-schema.org/format/uri-template",
34+
handler: (uriTemplate) => typeof uriTemplate !== "string" || isUriTemplate(uriTemplate)
35+
};

formats/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import uriReference from "./handlers/uri-reference.js";
1111
import iri from "./handlers/iri.js";
1212
import iriReference from "./handlers/iri-reference.js";
1313
import uuid from "./handlers/uuid.js";
14+
import uriTemplate from "./handlers/uri-template.js";
1415

1516

1617
addFormat(dateTime);
@@ -24,6 +25,7 @@ addFormat(uriReference);
2425
addFormat(iri);
2526
addFormat(iriReference);
2627
addFormat(uuid);
28+
addFormat(uriTemplate);
2729

2830
export {
2931
getShouldValidateFormat,

0 commit comments

Comments
 (0)