1
1
import { randomUUID } from "node:crypto" ;
2
2
import { existsSync } from "node:fs" ;
3
- import { readFile , writeFile } from "node:fs/promises" ;
4
- import { extname , join } from "node:path" ;
5
- import { pathToFileURL } from "node:url" ;
6
- import { registerSchema as register , unregisterSchema as unregister , validate } from "./json-schema.js" ;
7
- import { getKeywordId , getKeywordName , BASIC } from "@hyperjump/json-schema/experimental" ;
8
- import YAML from "yaml" ;
3
+ import { writeFile } from "node:fs/promises" ;
4
+ import { join } from "node:path" ;
5
+ import { registerSchema , unregisterSchema , validate } from "./json-schema.js" ;
6
+ import { BASIC } from "@hyperjump/json-schema/experimental" ;
9
7
import { FileCoverageMapService } from "./file-coverage-map-service.js" ;
10
8
import { TestCoverageEvaluationPlugin } from "../test-coverage-evaluation-plugin.js" ;
11
- import { toAbsoluteIri } from "@hyperjump/uri" ;
12
9
13
10
/**
14
- * @import { OutputUnit, SchemaObject } from "@hyperjump/json-schema"
11
+ * @import { OutputUnit } from "@hyperjump/json-schema"
15
12
* @import * as API from "./index.d.ts"
16
13
*/
17
14
@@ -51,12 +48,12 @@ export const matchJsonSchema = async (instance, uriOrSchema) => {
51
48
} else {
52
49
const schema = uriOrSchema ;
53
50
const uri = `urn:uuid:${ randomUUID ( ) } ` ;
54
- register ( schema , uri , "https://json-schema.org/draft/2020-12/schema" ) ;
51
+ registerSchema ( schema , uri , "https://json-schema.org/draft/2020-12/schema" ) ;
55
52
try {
56
53
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
57
54
output = await validate ( uri , instance , BASIC ) ;
58
55
} finally {
59
- unregister ( uri ) ;
56
+ unregisterSchema ( uri ) ;
60
57
}
61
58
}
62
59
@@ -67,56 +64,3 @@ export const matchJsonSchema = async (instance, uriOrSchema) => {
67
64
} ;
68
65
69
66
export const toMatchJsonSchema = matchJsonSchema ;
70
-
71
- /** @type API.registerSchema */
72
- export const registerSchema = async ( schemaPath ) => {
73
- const text = await readFile ( schemaPath , "utf-8" ) ;
74
- const extension = extname ( schemaPath ) ;
75
-
76
- /** @type SchemaObject */
77
- let schema ;
78
- switch ( extension ) {
79
- case ".json" :
80
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
81
- schema = JSON . parse ( text ) ;
82
- break ;
83
- case ".yaml" :
84
- case ".yml" :
85
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
86
- schema = YAML . parse ( text ) ;
87
- break ;
88
- default :
89
- throw Error ( `File of type '${ extension } ' is not supported.` ) ;
90
- }
91
-
92
- register ( schema ) ;
93
- } ;
94
-
95
- /** @type (schemaPath: string) => Promise<void> */
96
- export const unregisterSchema = async ( schemaPath ) => {
97
- const text = await readFile ( schemaPath , "utf-8" ) ;
98
- const extension = extname ( schemaPath ) ;
99
-
100
- /** @type SchemaObject */
101
- let schema ;
102
- switch ( extension ) {
103
- case ".json" :
104
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
105
- schema = JSON . parse ( text ) ;
106
- break ;
107
- case ".yaml" :
108
- case ".yml" :
109
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
110
- schema = YAML . parse ( text ) ;
111
- break ;
112
- default :
113
- throw Error ( `File of type '${ extension } ' is not supported.` ) ;
114
- }
115
-
116
- const dialectUri = toAbsoluteIri ( /** @type string */ ( schema . $schema ) ) ;
117
- const idToken = getKeywordName ( dialectUri , "https://json-schema.org/keyword/id" )
118
- ?? getKeywordId ( "https://json-schema.org/keyword/draft-04/id" , dialectUri ) ;
119
- const schemaUri = /** @type string */ ( schema [ idToken ] ) ?? pathToFileURL ( schemaPath ) . toString ( ) ;
120
-
121
- unregister ( schemaUri ) ;
122
- } ;
0 commit comments