@@ -187,6 +187,57 @@ addMediaTypePlugin("application/schema+yaml", {
187187});
188188` ` `
189189
190+ **EvaluationPlugins**
191+
192+ EvaluationPlugins allow you to hook into the validation process for various
193+ purposes. There are hooks for before an after schema evaluation and before and
194+ after keyword evaluation. (See the API section for the full interface) The
195+ following is a simple example to record all the schema locations that were
196+ evaluated. This could be used as part of a solution for determining test
197+ coverage for a schema.
198+
199+ ` ` ` JavaScript
200+ import { registerSchema , validate } from " @hyperjump/json-schema/draft-2020-12" ;
201+ import { BASIC } from " @hyperjump/json-schema/experimental.js" ;
202+
203+ class EvaluatedKeywordsPlugin {
204+ constructor () {
205+ this .schemaLocations = new Set ();
206+ }
207+
208+ beforeKeyword ([, schemaUri ]) {
209+ this .schemaLocations .add (schemaUri);
210+ }
211+ }
212+
213+ registerSchema ({
214+ $schema: " https://json-schema.org/draft/2020-12/schema" ,
215+ type: " object" ,
216+ properties: {
217+ foo: { type: " number" },
218+ bar: { type: " boolean" }
219+ },
220+ required: [" foo" ]
221+ }, " https://schemas.hyperjump.io/main" );
222+
223+ const evaluatedKeywordPlugin = new EvaluatedKeywordsPlugin ();
224+
225+ await validate (" https://schemas.hyperjump.io/main" , { foo: 42 }, {
226+ outputFormat: BASIC ,
227+ plugins: [evaluatedKeywordPlugin]
228+ });
229+
230+ console .log (evaluatedKeywordPlugin .schemaLocations );
231+ // Set(4) {
232+ // 'https://schemas.hyperjump.io/main#/type',
233+ // 'https://schemas.hyperjump.io/main#/properties',
234+ // 'https://schemas.hyperjump.io/main#/properties/foo/type',
235+ // 'https://schemas.hyperjump.io/main#/required'
236+ // }
237+
238+ // NOTE: #/properties/bar is not in the list because the instance doesn't include that property.
239+ ` ` `
240+
190241### API
191242
192243These are available from any of the exports that refer to a version of JSON
@@ -212,11 +263,11 @@ Schema, such as `@hyperjump/json-schema/draft-2020-12`.
212263 Load a schema manually rather than fetching it from the filesystem or over
213264 the network. Any schema already registered with the same identifier will be
214265 replaced with no warning.
215- * **validate**: (schemaURI: string, instance: any, outputFormat: OutputFormat = FLAG) => Promise\< OutputUnit>
266+ * **validate**: (schemaURI: string, instance: any, outputFormat: ValidationOptions | OutputFormat = FLAG) => Promise\< OutputUnit>
216267
217268 Validate an instance against a schema. This function is curried to allow
218269 compiling the schema once and applying it to multiple instances.
219- * **validate**: (schemaURI: string) => Promise\< (instance: any, outputFormat: OutputFormat = FLAG) => OutputUnit>
270+ * **validate**: (schemaURI: string) => Promise\< (instance: any, outputFormat: ValidationOptions | OutputFormat = FLAG) => OutputUnit>
220271
221272 Compiling a schema to a validation function.
222273* **FLAG**: "FLAG"
@@ -255,6 +306,16 @@ The following types are used in the above definitions
255306 Output is an experimental feature of the JSON Schema specification. There
256307 may be additional fields present in the OutputUnit, but only the ` valid`
257308 property should be considered part of the Stable API.
309+ * **ValidationOptions**:
310+
311+ * outputFormat?: OutputFormat
312+ * plugins?: EvaluationPlugin[]
313+
314+ * **EvaluationPlugin**: object
315+ * beforeSchema?(url: string, instance: JsonNode, context: Context): void
316+ * beforeKeyword?(keywordNode: Node<any>, instance: JsonNode, context: Context, schemaContext: Context, keyword: Keyword): void
317+ * afterKeyword?(keywordNode: Node<any>, instance: JsonNode, context: Context, valid: boolean, schemaContext: Context, keyword: Keyword): void
318+ * afterSchema?(url: string, instance: JsonNode, context: Context, valid: boolean): void
258319
259320## Bundling
260321
@@ -546,12 +607,6 @@ These are available from the `@hyperjump/json-schema/experimental` export.
546607 * **ValidationContext**: object
547608 * ast: AST
548609 * plugins: EvaluationPlugins[]
549-
550- * **EvaluationPlugin**: object
551- * beforeSchema(url: string, instance: JsonNode, context: Context): void
552- * beforeKeyword(keywordNode: Node<any>, instance: JsonNode, context: Context, schemaContext: Context, keyword: Keyword): void
553- * afterKeyword(keywordNode: Node<any>, instance: JsonNode, context: Context, valid: boolean, schemaContext: Context, keyword: Keyword): void
554- * afterSchema(url: string, instance: JsonNode, context: Context, valid: boolean): void
555610* **defineVocabulary**: (id: string, keywords: { [keyword: string]: string }) => void
556611
557612 Define a vocabulary that maps keyword name to keyword URIs defined using
0 commit comments