@hyperjump/json-schema 1.17.0 → 1.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -17
- package/draft-2020-12/format.js +44 -0
- package/draft-2020-12/index.js +2 -2
- package/formats/index.js +1 -2
- package/formats/lite.js +0 -5
- package/lib/experimental.d.ts +3 -2
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -507,32 +507,31 @@ associate that format handler with the format keyword that applies to the
|
|
|
507
507
|
dialects you're targeting.
|
|
508
508
|
|
|
509
509
|
```JavaScript
|
|
510
|
-
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
|
|
511
|
-
import {
|
|
512
|
-
setShouldValiateFormat,
|
|
513
|
-
addFormat,
|
|
514
|
-
setFormatHandler
|
|
515
|
-
} from "@hyperjump/json-schema/formats";
|
|
510
|
+
import { registerSchema, validate, setShouldValidateFormat } from "@hyperjump/json-schema/draft-2020-12";
|
|
511
|
+
import { addFormat, setFormatHandler } from "@hyperjump/json-schema/experimental";
|
|
516
512
|
|
|
517
|
-
const isoDateFormatUri = "https://example.com/format/iso-8601-date";
|
|
513
|
+
const isoDateFormatUri = "https://example.com/format/iso-8601-date";
|
|
518
514
|
|
|
515
|
+
// Add the iso-date format handler
|
|
519
516
|
addFormat({
|
|
520
517
|
id: isoDateFormatUri,
|
|
521
|
-
handler: (date) => Date
|
|
518
|
+
handler: (date) => new Date(date).toISOString() === date
|
|
522
519
|
});
|
|
523
520
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
521
|
+
// Add the "iso-date" format to the 2020-12 version of `format`
|
|
522
|
+
setFormatHandler("https://json-schema.org/keyword/draft-2020-12/format", "iso-date", isoDateFormatUri);
|
|
523
|
+
setFormatHandler("https://json-schema.org/keyword/draft-2020-12/format-assertion", "iso-date", isoDateFormatUri);
|
|
524
|
+
|
|
525
|
+
// Optional: Add the "iso-date" format to other dialects
|
|
526
|
+
setFormatHandler("https://json-schema.org/keyword/draft-2019-09/format", "iso-date", isoDateFormatUri);
|
|
527
|
+
setFormatHandler("https://json-schema.org/keyword/draft-2019-09/format-assertion", "iso-date", isoDateFormatUri);
|
|
528
|
+
setFormatHandler("https://json-schema.org/keyword/draft-07/format", "iso-date", isoDateFormatUri);
|
|
529
|
+
setFormatHandler("https://json-schema.org/keyword/draft-06/format", "iso-date", isoDateFormatUri);
|
|
530
|
+
setFormatHandler("https://json-schema.org/keyword/draft-04/format", "iso-date", isoDateFormatUri);
|
|
532
531
|
|
|
533
532
|
const schemaUri = "https://example.com/main";
|
|
534
533
|
registerSchema({
|
|
535
|
-
"$schema": "https://json-schema.org/draft/
|
|
534
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
536
535
|
|
|
537
536
|
"type": "string",
|
|
538
537
|
"format": "iso-date"
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as Browser from "@hyperjump/browser";
|
|
2
|
+
import * as Instance from "../lib/instance.js";
|
|
3
|
+
import { getShouldValidateFormat } from "../lib/configuration.js";
|
|
4
|
+
import { getFormatHandler } from "../lib/keywords.js";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const id = "https://json-schema.org/keyword/draft-2020-12/format";
|
|
8
|
+
|
|
9
|
+
const compile = (schema) => Browser.value(schema);
|
|
10
|
+
|
|
11
|
+
const interpret = (format, instance) => {
|
|
12
|
+
if (!getShouldValidateFormat()) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const handler = getFormatHandler(formats[format]);
|
|
17
|
+
return handler?.(Instance.value(instance)) ?? true;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const annotation = (format) => format;
|
|
21
|
+
|
|
22
|
+
const formats = {
|
|
23
|
+
"date-time": "https://json-schema.org/format/date-time",
|
|
24
|
+
"date": "https://json-schema.org/format/date",
|
|
25
|
+
"time": "https://json-schema.org/format/time",
|
|
26
|
+
"duration": "https://json-schema.org/format/duration",
|
|
27
|
+
"email": "https://json-schema.org/format/email",
|
|
28
|
+
"idn-email": "https://json-schema.org/format/idn-email",
|
|
29
|
+
"hostname": "https://json-schema.org/format/hostname",
|
|
30
|
+
"idn-hostname": "https://json-schema.org/format/idn-hostname",
|
|
31
|
+
"ipv4": "https://json-schema.org/format/ipv4",
|
|
32
|
+
"ipv6": "https://json-schema.org/format/ipv6",
|
|
33
|
+
"uri": "https://json-schema.org/format/uri",
|
|
34
|
+
"uri-reference": "https://json-schema.org/format/uri-reference",
|
|
35
|
+
"iri": "https://json-schema.org/format/iri",
|
|
36
|
+
"iri-reference": "https://json-schema.org/format/iri-reference",
|
|
37
|
+
"uuid": "https://json-schema.org/format/uuid",
|
|
38
|
+
"uri-template": "https://json-schema.org/format/uri-template",
|
|
39
|
+
"json-pointer": "https://json-schema.org/format/json-pointer",
|
|
40
|
+
"relative-json-pointer": "https://json-schema.org/format/relative-json-pointer",
|
|
41
|
+
"regex": "https://json-schema.org/format/regex"
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default { id, compile, interpret, annotation, formats };
|
package/draft-2020-12/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import unevaluatedMetaSchema from "./meta/unevaluated.js";
|
|
|
13
13
|
|
|
14
14
|
import dynamicAnchor from "./dynamicAnchor.js";
|
|
15
15
|
import dynamicRef from "./dynamicRef.js";
|
|
16
|
-
import format from "
|
|
16
|
+
import format from "./format.js";
|
|
17
17
|
import formatAssertion from "./format-assertion.js";
|
|
18
18
|
|
|
19
19
|
|
|
@@ -85,7 +85,7 @@ defineVocabulary("https://json-schema.org/draft/2020-12/vocab/meta-data", {
|
|
|
85
85
|
});
|
|
86
86
|
|
|
87
87
|
defineVocabulary("https://json-schema.org/draft/2020-12/vocab/format-annotation", {
|
|
88
|
-
"format": "https://json-schema.org/keyword/draft-
|
|
88
|
+
"format": "https://json-schema.org/keyword/draft-2020-12/format"
|
|
89
89
|
});
|
|
90
90
|
|
|
91
91
|
defineVocabulary("https://json-schema.org/draft/2020-12/vocab/format-assertion", {
|
package/formats/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { addFormat } from "../lib/keywords.js";
|
|
2
|
+
import "./lite.js";
|
|
2
3
|
|
|
3
4
|
import idnEmail from "./handlers/idn-email.js";
|
|
4
5
|
import hostname from "./handlers/hostname.js";
|
|
@@ -8,5 +9,3 @@ import idnHostname from "./handlers/idn-hostname.js";
|
|
|
8
9
|
addFormat(idnEmail);
|
|
9
10
|
addFormat(hostname);
|
|
10
11
|
addFormat(idnHostname);
|
|
11
|
-
|
|
12
|
-
export * from "./lite.js";
|
package/formats/lite.js
CHANGED
package/lib/experimental.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Browser, Document } from "@hyperjump/browser";
|
|
2
|
+
import type { Json } from "@hyperjump/json-pointer";
|
|
2
3
|
import type { Validator, Output, OutputUnit, OutputFormat, SchemaObject } from "./index.js";
|
|
3
4
|
import type { JsonNode } from "./instance.js";
|
|
4
5
|
|
|
@@ -91,8 +92,8 @@ export type ValidationContext = {
|
|
|
91
92
|
// Evaluation Plugins
|
|
92
93
|
export type EvaluationPlugin<Context extends ValidationContext = ValidationContext> = {
|
|
93
94
|
beforeSchema?(url: string, instance: JsonNode, context: Context): void;
|
|
94
|
-
beforeKeyword?(keywordNode: Node<unknown>, instance: JsonNode, context: Context, schemaContext: Context, keyword: Keyword): void;
|
|
95
|
-
afterKeyword?(keywordNode: Node<unknown>, instance: JsonNode, context: Context, valid: boolean, schemaContext: Context, keyword: Keyword): void;
|
|
95
|
+
beforeKeyword?(keywordNode: Node<unknown>, instance: JsonNode, context: Context, schemaContext: Context, keyword: Keyword<unknown>): void;
|
|
96
|
+
afterKeyword?(keywordNode: Node<unknown>, instance: JsonNode, context: Context, valid: boolean, schemaContext: Context, keyword: Keyword<unknown>): void;
|
|
96
97
|
afterSchema?(url: string, instance: JsonNode, context: Context, valid: boolean): void;
|
|
97
98
|
};
|
|
98
99
|
|
package/lib/index.d.ts
CHANGED
|
@@ -54,6 +54,8 @@ export const setMetaSchemaOutputFormat: (format: OutputFormat) => void;
|
|
|
54
54
|
export const getMetaSchemaOutputFormat: () => OutputFormat;
|
|
55
55
|
export const setShouldValidateSchema: (isEnabled: boolean) => void;
|
|
56
56
|
export const getShouldValidateSchema: () => boolean;
|
|
57
|
+
export const setShouldValidateFormat: (isEnabled: boolean | undefined) => void;
|
|
58
|
+
export const getShouldValidateFormat: () => boolean | undefined;
|
|
57
59
|
|
|
58
60
|
export class InvalidSchemaError extends Error {
|
|
59
61
|
public output: Output & { valid: false };
|
package/lib/index.js
CHANGED
|
@@ -135,6 +135,8 @@ export {
|
|
|
135
135
|
getMetaSchemaOutputFormat,
|
|
136
136
|
setMetaSchemaOutputFormat,
|
|
137
137
|
getShouldValidateSchema,
|
|
138
|
-
setShouldValidateSchema
|
|
138
|
+
setShouldValidateSchema,
|
|
139
|
+
getShouldValidateFormat,
|
|
140
|
+
setShouldValidateFormat
|
|
139
141
|
} from "./configuration.js";
|
|
140
142
|
export { InvalidSchemaError } from "./invalid-schema-error.js";
|