@@ -24,9 +24,14 @@ import { Output } from "./output.js";
24
24
25
25
/** @type (schema: Json, instance: Json) => Output */
26
26
export const validate = ( schema , instance ) => {
27
- registerSchema ( schema , "" ) ;
28
- const schemaNode = /** @type NonNullable<JsonNode> */ ( schemaRegistry . get ( "" ) ) ;
27
+ // Determine schema identifier
28
+ const uri = typeof schema === "object" && schema !== null && ! Array . isArray ( schema )
29
+ && typeof schema . $id === "string" ? schema . $id : "" ;
30
+ registerSchema ( schema , uri ) ;
29
31
32
+ const schemaNode = /** @type NonNullable<JsonNode> */ ( schemaRegistry . get ( uri ) ) ;
33
+
34
+ // Verify the dialect is supported
30
35
if ( schemaNode . jsonType === "object" && jsonObjectHas ( "$schema" , schemaNode ) ) {
31
36
const $schema = jsonPointerStep ( "$schema" , schemaNode ) ;
32
37
if ( $schema . jsonType === "string" && $schema . value !== "https://json-schema.org/draft/2020-12/schema" ) {
@@ -36,7 +41,7 @@ export const validate = (schema, instance) => {
36
41
37
42
const output = validateSchema ( schemaNode , toJsonNode ( instance ) ) ;
38
43
39
- schemaRegistry . delete ( "" ) ;
44
+ schemaRegistry . delete ( uri ) ;
40
45
41
46
return output ;
42
47
} ;
@@ -625,3 +630,31 @@ keywordHandlers.set("uniqueItems", (uniqueItemsNode, instanceNode) => {
625
630
const isValid = new Set ( normalizedItems ) . size === normalizedItems . length ;
626
631
return new Output ( isValid , uniqueItemsNode , instanceNode ) ;
627
632
} ) ;
633
+
634
+ keywordHandlers . set ( "$id" , ( idNode , instanceNode , schemaNode ) => {
635
+ if ( ! idNode . location . endsWith ( "#/$id" ) ) {
636
+ throw Error ( `Embedded schemas are not supported. Found at ${ schemaNode . location } ` ) ;
637
+ }
638
+
639
+ return new Output ( true , idNode , instanceNode ) ;
640
+ } ) ;
641
+
642
+ keywordHandlers . set ( "$anchor" , ( anchorNode ) => {
643
+ throw Error ( `The '$anchor' keyword is not supported. Found at ${ anchorNode . location } ` ) ;
644
+ } ) ;
645
+
646
+ keywordHandlers . set ( "$dynamicAnchor" , ( dynamicAnchorNode ) => {
647
+ throw Error ( `The '$dynamicAnchor' keyword is not supported. Found at ${ dynamicAnchorNode . location } ` ) ;
648
+ } ) ;
649
+
650
+ keywordHandlers . set ( "$dynamicRef" , ( dynamicRefNode ) => {
651
+ throw Error ( `The '$dynamicRef' keyword is not supported. Found at ${ dynamicRefNode . location } ` ) ;
652
+ } ) ;
653
+
654
+ keywordHandlers . set ( "unevaluatedProperties" , ( unevaluatedPropertiesNode ) => {
655
+ throw Error ( `The 'unevaluatedProperties' keyword is not supported. Found at ${ unevaluatedPropertiesNode . location } ` ) ;
656
+ } ) ;
657
+
658
+ keywordHandlers . set ( "unevaluatedItems" , ( unevaluatedItemsNode ) => {
659
+ throw Error ( `The 'unevaluatedItems' keyword is not supported. Found at ${ unevaluatedItemsNode . location } ` ) ;
660
+ } ) ;
0 commit comments