1
1
import { randomUUID } from "node:crypto" ;
2
2
import { existsSync } from "node:fs" ;
3
- import { writeFile } from "node:fs/promises" ;
4
- import { registerSchema , unregisterSchema , validate } from "@hyperjump/json-schema/draft-2020-12" ;
3
+ import { readFile , writeFile } from "node:fs/promises" ;
4
+ import {
5
+ registerSchema as register ,
6
+ unregisterSchema ,
7
+ validate
8
+ } from "@hyperjump/json-schema/draft-2020-12" ;
5
9
import "@hyperjump/json-schema/draft-2019-09" ;
6
10
import "@hyperjump/json-schema/draft-07" ;
7
11
import "@hyperjump/json-schema/draft-06" ;
@@ -13,10 +17,10 @@ import { TestCoverageEvaluationPlugin } from "../test-coverage-evaluation-plugin
13
17
14
18
/**
15
19
* @import { OutputUnit, SchemaObject } from "@hyperjump/json-schema"
16
- * @import { AsyncExpectationResult } from "@vitest/expect "
20
+ * @import * as API from "./json-schema-matcher.d.ts "
17
21
*/
18
22
19
- /** @type (instance: any, uriOrSchema: string | SchemaObject | boolean) => AsyncExpectationResult */
23
+ /** @type API.matchJsonSchema */
20
24
export const matchJsonSchema = async ( instance , uriOrSchema ) => {
21
25
/** @type OutputUnit */
22
26
let output ;
@@ -43,7 +47,7 @@ export const matchJsonSchema = async (instance, uriOrSchema) => {
43
47
} else {
44
48
const schema = uriOrSchema ;
45
49
const uri = `urn:uuid:${ randomUUID ( ) } ` ;
46
- registerSchema ( schema , uri , "https://json-schema.org/draft/2020-12/schema" ) ;
50
+ register ( schema , uri , "https://json-schema.org/draft/2020-12/schema" ) ;
47
51
try {
48
52
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
49
53
output = await validate ( uri , instance , BASIC ) ;
@@ -59,5 +63,15 @@ export const matchJsonSchema = async (instance, uriOrSchema) => {
59
63
} ;
60
64
61
65
export const toMatchJsonSchema = matchJsonSchema ;
62
- export { registerSchema , unregisterSchema } from "@hyperjump/json-schema/draft-2020-12" ;
66
+
67
+ /** @type API.registerSchema */
68
+ export const registerSchema = async ( filePath ) => {
69
+ const json = await readFile ( filePath , "utf-8" ) ;
70
+ /** @type SchemaObject */
71
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
72
+ const schema = JSON . parse ( json ) ;
73
+ register ( schema ) ;
74
+ } ;
75
+
76
+ export { unregisterSchema } from "@hyperjump/json-schema/draft-2020-12" ;
63
77
export { loadDialect , defineVocabulary , addKeyword } from "@hyperjump/json-schema/experimental" ;
0 commit comments