@@ -518,28 +518,23 @@ These are available from the `@hyperjump/json-schema/experimental` export.
518
518
is needed for compiling sub-schemas. The ` parentSchema` parameter is
519
519
primarily useful for looking up the value of an adjacent keyword that
520
520
might effect this one.
521
- * interpret: (compiledKeywordValue: any, instance: InstanceDocument , ast: AST, dynamicAnchors: object, quiet: boolean) => boolean
521
+ * interpret: (compiledKeywordValue: any, instance: JsonNode , ast: AST, dynamicAnchors: object, quiet: boolean, schemaLocation: string ) => boolean
522
522
523
523
This function takes the value returned by the ` compile` function and
524
524
the instance value that is being validated and returns whether the
525
525
value is valid or not. The other parameters are only needed for
526
526
validating sub-schemas.
527
- * collectEvaluatedProperties?: (compiledKeywordValue: any, instance: InstanceDocument , ast: AST, dynamicAnchors: object) => Set\< string> | false
527
+ * collectEvaluatedProperties?: (compiledKeywordValue: any, instance: JsonNode , ast: AST, dynamicAnchors: object) => Set\< string> | false
528
528
529
529
If the keyword is an applicator, it will need to implement this
530
530
function for ` unevaluatedProperties` to work as expected.
531
- * collectEvaluatedItems?: (compiledKeywordValue: A, instance: InstanceDocument , ast: AST, dynamicAnchors: object) => Set\< number> | false
531
+ * collectEvaluatedItems?: (compiledKeywordValue: A, instance: JsonNode , ast: AST, dynamicAnchors: object) => Set\< number> | false
532
532
533
533
If the keyword is an applicator, it will need to implement this
534
534
function for ` unevaluatedItems` to work as expected.
535
535
* collectExternalIds?: (visited: Set\< string>, parentSchema: Browser, schema: Browser) => Set\< string>
536
536
If the keyword is an applicator, it will need to implement this
537
537
function to work properly with the [bundle](#bundling) feature.
538
- * annotation?: (compiledKeywordValue: any) => any
539
-
540
- If the keyword is an annotation, it will need to implement this
541
- function to work with the [annotation](#annotations-experimental)
542
- functions.
543
538
* **defineVocabulary**: (id: string, keywords: { [keyword: string]: string }) => void
544
539
545
540
Define a vocabulary that maps keyword name to keyword URIs defined using
@@ -607,68 +602,69 @@ These are available from the `@hyperjump/json-schema/experimental` export.
607
602
608
603
Return a compiled schema. This is useful if you're creating tooling for
609
604
something other than validation.
610
- * **interpret**: (schema: CompiledSchema, instance: Instance , outputFormat: OutputFormat = BASIC) => OutputUnit
605
+ * **interpret**: (schema: CompiledSchema, instance: JsonNode , outputFormat: OutputFormat = BASIC) => OutputUnit
611
606
612
607
A curried function for validating an instance against a compiled schema.
613
608
This can be useful for creating custom output formats.
614
609
615
- * **OutputFormat**: **FLAG** | **BASIC** | **DETAILED** | **VERBOSE**
610
+ * **OutputFormat**: **FLAG** | **BASIC**
616
611
617
612
In addition to the ` FLAG ` output format in the Stable API, the Experimental
618
- API includes support for the ` BASIC ` , ` DETAILED ` , and ` VERBOSE ` formats as
619
- specified in the 2019-09 specification (with some minor customizations).
620
- This implementation doesn't include annotations or human readable error
621
- messages. The output can be processed to create human readable error
622
- messages as needed.
613
+ API includes support for the ` BASIC ` format as specified in the 2019-09
614
+ specification (with some minor customizations). This implementation doesn't
615
+ include annotations or human readable error messages. The output can be
616
+ processed to create human readable error messages as needed.
623
617
624
618
## Instance API (experimental)
625
619
626
620
These functions are available from the
627
621
` @hyperjump/ json- schema/ instance/ experimental` export.
628
622
629
- This library uses InstanceDocument objects to represent a value in an instance.
630
- You'll work with these objects if you create a custom keyword. This module is a
631
- set of functions for working with InstanceDocuments.
623
+ This library uses JsonNode objects to represent instances. You'll work with
624
+ these objects if you create a custom keyword.
632
625
633
626
This API uses generators to iterate over arrays and objects. If you like using
634
627
higher order functions like ` map` /` filter` /` reduce` , see
635
628
[` @hyperjump/ pact` ](https://github.com/hyperjump-io/pact) for utilities for
636
629
working with generators and async generators.
637
630
638
- * **cons **: (instance : any, uri?: string) => InstanceDocument
631
+ * **fromJs **: (value : any, uri?: string) => JsonNode
639
632
640
- Construct an InstanceDocument from a value.
641
- * **get**: (url: string, contextDoc: InstanceDocument ) => InstanceDocument
633
+ Construct a JsonNode from a JavaScript value.
634
+ * **get**: (url: string, instance: JsonNode ) => JsonNode
642
635
643
- Apply a same-resource reference to a InstanceDocument .
644
- * **uri**: (doc: InstanceDocument ) => string
636
+ Apply a same-resource reference to a JsonNode .
637
+ * **uri**: (instance: JsonNode ) => string
645
638
646
- Returns a URI for the value the InstanceDocument represents.
647
- * **value**: (doc: InstanceDocument ) => any
639
+ Returns a URI for the value the JsonNode represents.
640
+ * **value**: (instance: JsonNode ) => any
648
641
649
- Returns the value the InstanceDocument represents.
650
- * **has**: (key: string, doc: InstanceDocument ) => any
642
+ Returns the value the JsonNode represents.
643
+ * **has**: (key: string, instance: JsonNode ) => boolean
651
644
652
- Similar to ` key in instance` .
653
- * **typeOf**: (doc: InstanceDocument) => string
645
+ Returns whether or not "key" is a property name in a JsonNode that
646
+ represents an object.
647
+ * **typeOf**: (instance: JsonNode) => string
654
648
655
- Determines if the JSON type of the given doc matches the given type.
656
- * **step**: (key: string, doc: InstanceDocument) => InstanceDocument
649
+ The JSON type of the JsonNode. In addition to the standard JSON types,
650
+ there's also the ` property` type that indicates a property name/value pair
651
+ in an object.
652
+ * **step**: (key: string, instance: JsonNode) => JsonType
657
653
658
- Similar to ` schema[key] ` , but returns a InstanceDocument .
659
- * **iter**: (doc: InstanceDocument ) => Generator\< InstanceDocument >
654
+ Similar to indexing into a object or array using the ` [] ` operator .
655
+ * **iter**: (instance: JsonNode ) => Generator\< JsonNode >
660
656
661
- Iterate over the items in the array that the SchemaDocument represents.
662
- * **entries**: (doc: InstanceDocument ) => Generator\< [string, InstanceDocument ]>
657
+ Iterate over the items in the array that the JsonNode represents.
658
+ * **entries**: (instance: JsonNode ) => Generator\< [JsonNode, JsonNode ]>
663
659
664
- Similar to ` Object .entries ` , but yields InstanceDocuments for values.
665
- * **values**: (doc: InstanceDocument ) => Generator\< InstanceDocument >
660
+ Similar to ` Object .entries ` , but yields JsonNodes for keys and values.
661
+ * **values**: (instance: JsonNode ) => Generator\< JsonNode >
666
662
667
- Similar to ` Object .values ` , but yields InstanceDocuments for values.
668
- * **keys**: (doc: InstanceDocument ) => Generator\< string >
663
+ Similar to ` Object .values ` , but yields JsonNodes for values.
664
+ * **keys**: (instance: JsonNode ) => Generator\< JsonNode >
669
665
670
- Similar to ` Object .keys ` .
671
- * **length**: (doc: InstanceDocument ) => number
666
+ Similar to ` Object .keys ` , but yields JsonNodes for keys .
667
+ * **length**: (instance: JsonNode ) => number
672
668
673
669
Similar to ` Array .prototype .length ` .
674
670
@@ -678,12 +674,13 @@ module provides utilities for working with JSON documents annotated with JSON
678
674
Schema.
679
675
680
676
### Usage
681
- An annotated JSON document is represented as an AnnotatedInstance object. This
682
- object is a wrapper around your JSON document with functions that allow you to
683
- traverse the data structure and get annotations for the values within .
677
+ An annotated JSON document is represented as a
678
+ (JsonNode)[#/instance-api-experimental] AST. You can use this AST to traverse
679
+ the data structure and get annotations for the values it represents .
684
680
685
681
` ` ` javascript
686
- import { annotate , annotatedWith , registerSchema } from " @hyperjump/json-schema/annotations/experimental" ;
682
+ import { registerSchema } from " @hyperjump/json-schema/draft/2020-12" ;
683
+ import { annotate } from " @hyperjump/json-schema/annotations/experimental" ;
687
684
import * as AnnotatedInstance from " @hyperjump/json-schema/annotated-instance/experimental" ;
688
685
689
686
@@ -736,13 +733,14 @@ const unknowns = AnnotatedInstance.annotation(instance, "unknown", dialectId); /
736
733
const types = AnnotatedInstance .annotation (instance, " type" , dialectId); // => []
737
734
738
735
// Get the title of each of the properties in the object
739
- for (const [propertyName , propertyInstance ] of AnnotatedInstance .entries (instance)) {
740
- console .log (propertyName, Instance .annotation (propertyInstance, " title" , dialectId));
736
+ for (const [propertyNameNode , propertyInstance ] of AnnotatedInstance .entries (instance)) {
737
+ const propertyName = AnnotatedInstance .value (propertyName);
738
+ console .log (propertyName, AnnotatedInstance .annotation (propertyInstance, " title" , dialectId));
741
739
}
742
740
743
741
// List all locations in the instance that are deprecated
744
742
for (const deprecated of AnnotatedInstance .annotatedWith (instance, " deprecated" , dialectId)) {
745
- if (AnnotatedInstance .annotation (instance , " deprecated" , dialectId)[0 ]) {
743
+ if (AnnotatedInstance .annotation (deprecated , " deprecated" , dialectId)[0 ]) {
746
744
logger .warn (` The value at '${ deprecated .pointer } ' has been deprecated.` ); // => (Example) "WARN: The value at '/name' has been deprecated."
747
745
}
748
746
}
@@ -752,13 +750,18 @@ for (const deprecated of AnnotatedInstance.annotatedWith(instance, "deprecated",
752
750
These are available from the ` @hyperjump/ json- schema/ annotations/ experimental`
753
751
export.
754
752
755
- * **annotate**: (schemaUri: string, instance: any, outputFormat: OutputFormat = FLAG ) => Promise\< AnnotatedInstance >
753
+ * **annotate**: (schemaUri: string, instance: any, outputFormat: OutputFormat = BASIC ) => Promise\< JsonNode >
756
754
757
755
Annotate an instance using the given schema. The function is curried to
758
756
allow compiling the schema once and applying it to multiple instances. This
759
757
may throw an [InvalidSchemaError](#api) if there is a problem with the
760
758
schema or a ValidationError if the instance doesn't validate against the
761
759
schema.
760
+ * **interpret**: (compiledSchema: CompiledSchema, instance: JsonNode, outputFormat: OutputFormat = BASIC) => JsonNode
761
+
762
+ Annotate a JsonNode object rather than a plain JavaScript value. This might
763
+ be useful when building tools on top of the annotation functionality, but
764
+ you probably don't need it.
762
765
* **ValidationError**: Error & { output: OutputUnit }
763
766
The ` output` field contains an ` OutputUnit` with information about the
764
767
error.
@@ -769,15 +772,13 @@ These are available from the
769
772
following functions are available in addition to the functions available in the
770
773
[Instance API](#instance-api-experimental).
771
774
772
- * **annotation**: (instance: AnnotatedInstance , keyword: string, dialectId ?: string) => any[]
775
+ * **annotation**: (instance: JsonNode , keyword: string, dialect ?: string): any[];
773
776
774
- Get the annotations for a given keyword at the location represented by the
775
- instance object.
776
- * **annotatedWith**: (instance: AnnotatedInstance, keyword: string, dialectId?: string) => AnnotatedInstance[]
777
+ Get the annotations for a keyword for the value represented by the JsonNode.
778
+ * **annotatedWith**: (instance: JsonNode, keyword: string, dialect?: string): JsonNode[];
777
779
778
- Get an array of instances for all the locations that are annotated with the
779
- given keyword.
780
- * **annotate**: (instance: AnnotatedInstance, keywordId: string, value: any) => AnnotatedInstance
780
+ Get all JsonNodes that are annotated with the given keyword.
781
+ * **setAnnotation**: (instance: JsonNode, keywordId: string, value: any) => JsonNode
781
782
782
783
Add an annotation to an instance. This is used internally, you probably
783
784
don't need it.
0 commit comments