Skip to content

Commit ddf6ff5

Browse files
committed
Add uuid format handler
1 parent 561c3ec commit ddf6ff5

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

formats/formats-test-suite.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ const skip = new Set<string>([
9494
"|draft2020-12|uri-template.json",
9595
"|draft2019-09|uri-template.json",
9696
"|draft7|uri-template.json",
97-
"|draft6|uri-template.json",
98-
99-
// Not supported
100-
"|draft2020-12|uuid.json",
101-
"|draft2019-09|uuid.json"
97+
"|draft6|uri-template.json"
10298
]);
10399

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

formats/handlers/uuid.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const hexDigit = `[0-9a-fA-F]`;
2+
const hexOctet = `(?:${hexDigit}{2})`;
3+
const timeLow = `${hexOctet}{4}`;
4+
const timeMid = `${hexOctet}{2}`;
5+
const timeHighAndVersion = `${hexOctet}{2}`;
6+
const clockSeqAndReserved = hexOctet;
7+
const clockSeqLow = hexOctet;
8+
const node = `${hexOctet}{6}`;
9+
10+
const uuid = `${timeLow}\\-${timeMid}\\-${timeHighAndVersion}\\-${clockSeqAndReserved}${clockSeqLow}\\-${node}`;
11+
12+
export const isUuid = RegExp.prototype.test.bind(new RegExp(`^${uuid}$`));
13+
14+
export default {
15+
id: "https://json-schema.org/format/uuid",
16+
handler: (uuid) => typeof uuid !== "string" || isUuid(uuid)
17+
};

formats/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import uri from "./handlers/uri.js";
1010
import uriReference from "./handlers/uri-reference.js";
1111
import iri from "./handlers/iri.js";
1212
import iriReference from "./handlers/iri-reference.js";
13+
import uuid from "./handlers/uuid.js";
1314

1415

1516
addFormat(dateTime);
@@ -22,6 +23,7 @@ addFormat(uri);
2223
addFormat(uriReference);
2324
addFormat(iri);
2425
addFormat(iriReference);
26+
addFormat(uuid);
2527

2628
export {
2729
getShouldValidateFormat,

0 commit comments

Comments
 (0)