1
1
import { describe , expect , test } from "vitest" ;
2
2
import { normalizeOutputFormat } from "./normalizeOutput.js" ;
3
3
import { betterJsonSchemaErrors } from "../index.js" ;
4
-
5
4
/**
6
5
* @import { OutputFormat, OutputUnit, SchemaObject } from "../index.d.ts"
7
6
*/
8
7
9
8
describe ( "Error Output Normalization" , ( ) => {
10
- test ( "Simple keyword with a standard Basic output format" , ( ) => {
9
+ test ( "Simple keyword with a standard Basic output format" , async ( ) => {
11
10
/** @type SchemaObject */
12
11
const schema = {
13
12
minLength : 3
@@ -26,7 +25,7 @@ describe("Error Output Normalization", () => {
26
25
]
27
26
} ;
28
27
29
- const result = betterJsonSchemaErrors ( instance , schema , output ) ;
28
+ const result = await betterJsonSchemaErrors ( instance , schema , output ) ;
30
29
expect ( result . errors ) . to . eql ( [ {
31
30
schemaLocation : "https://example.com/main#/minLength" ,
32
31
instanceLocation : "#" ,
@@ -35,9 +34,11 @@ describe("Error Output Normalization", () => {
35
34
] ) ;
36
35
} ) ;
37
36
38
- test ( "Checking when output contain only instanceLocation and keywordLocation " , ( ) => {
37
+ test ( "Checking when output contain only instanceLocation and keywordLocation " , async ( ) => {
39
38
/** @type SchemaObject */
40
39
const schema = {
40
+ $id : "https://example.com/main" ,
41
+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
41
42
minLength : 3
42
43
} ;
43
44
@@ -54,17 +55,18 @@ describe("Error Output Normalization", () => {
54
55
]
55
56
} ;
56
57
57
- const result = betterJsonSchemaErrors ( instance , schema , output ) ;
58
+ const result = await betterJsonSchemaErrors ( instance , schema , output ) ;
58
59
expect ( result . errors ) . to . eql ( [ {
59
60
schemaLocation : "https://example.com/main#/minLength" ,
60
61
instanceLocation : "#" ,
61
62
message : "The instance should be at least 3 characters"
62
63
} ] ) ;
63
64
} ) ;
64
65
65
- test ( "adding # if instanceLocation doesn't have it" , ( ) => {
66
+ test ( "adding # if instanceLocation doesn't have it" , async ( ) => {
66
67
/** @type SchemaObject */
67
68
const schema = {
69
+ $id : "https://example.com/main" ,
68
70
minlength : 3
69
71
} ;
70
72
@@ -76,13 +78,13 @@ describe("Error Output Normalization", () => {
76
78
errors : [
77
79
{
78
80
valid : false ,
79
- keywordLocation : "" ,
81
+ absoluteKeywordLocation : "https://example.com/main#/minLength " ,
80
82
instanceLocation : ""
81
83
}
82
84
]
83
85
} ;
84
86
85
- const result = betterJsonSchemaErrors ( instance , schema , output ) ;
87
+ const result = await betterJsonSchemaErrors ( instance , schema , output ) ;
86
88
expect ( result . errors ) . to . eql ( [ {
87
89
schemaLocation : "https://example.com/main#/minLength" ,
88
90
instanceLocation : "#" ,
@@ -103,7 +105,10 @@ describe("Error Output Normalization", () => {
103
105
// const absoluteKeywordLocation = "/$defs/foo/type";
104
106
// const keywordLocation = "/properties/foo/$ref/type";
105
107
106
- test ( "checking for the basic output format" , ( ) => {
108
+ test ( "checking for the basic output format" , async ( ) => {
109
+ const schema = {
110
+ $id : "https://example.com/polygon"
111
+ } ;
107
112
const errorOutput = {
108
113
valid : false ,
109
114
errors : [
@@ -131,12 +136,7 @@ describe("Error Output Normalization", () => {
131
136
]
132
137
} ;
133
138
134
- expect ( normalizeOutputFormat ( errorOutput ) ) . to . eql ( [
135
- {
136
- valid : false ,
137
- absoluteKeywordLocation : "https://example.com/polygon#/$defs/point" ,
138
- instanceLocation : "#/1"
139
- } ,
139
+ expect ( await normalizeOutputFormat ( errorOutput , schema ) ) . to . eql ( [
140
140
{
141
141
valid : false ,
142
142
absoluteKeywordLocation : "https://example.com/polygon#/$defs/point/required" ,
@@ -150,7 +150,10 @@ describe("Error Output Normalization", () => {
150
150
] ) ;
151
151
} ) ;
152
152
153
- test ( "checking for the detailed output format" , ( ) => {
153
+ test ( "checking for the detailed output format" , async ( ) => {
154
+ const schema = {
155
+ $id : "https://example.com/polygon"
156
+ } ;
154
157
const errorOutput = {
155
158
valid : false ,
156
159
keywordLocation : "#" ,
@@ -181,12 +184,7 @@ describe("Error Output Normalization", () => {
181
184
]
182
185
} ;
183
186
184
- expect ( normalizeOutputFormat ( errorOutput ) ) . to . eql ( [
185
- {
186
- valid : false ,
187
- absoluteKeywordLocation : "https://example.com/polygon#/$defs/point" ,
188
- instanceLocation : "#/1"
189
- } ,
187
+ expect ( await normalizeOutputFormat ( errorOutput , schema ) ) . to . eql ( [
190
188
{
191
189
valid : false ,
192
190
absoluteKeywordLocation : "https://example.com/polygon#/$defs/point/required" ,
@@ -200,7 +198,10 @@ describe("Error Output Normalization", () => {
200
198
] ) ;
201
199
} ) ;
202
200
203
- test ( "checking for the verbose output format" , ( ) => {
201
+ test ( "checking for the verbose output format" , async ( ) => {
202
+ const schema = {
203
+ $id : "https://example.com/polygon"
204
+ } ;
204
205
const errorOutput = {
205
206
valid : false ,
206
207
keywordLocation : "#" ,
@@ -232,7 +233,7 @@ describe("Error Output Normalization", () => {
232
233
]
233
234
} ;
234
235
235
- expect ( normalizeOutputFormat ( errorOutput ) ) . to . eql ( [
236
+ expect ( await normalizeOutputFormat ( errorOutput , schema ) ) . to . eql ( [
236
237
{
237
238
valid : false ,
238
239
absoluteKeywordLocation : "https://example.com/schema#/additionalProperties" ,
@@ -246,7 +247,10 @@ describe("Error Output Normalization", () => {
246
247
] ) ;
247
248
} ) ;
248
249
249
- test ( "when error output doesnot contain any of these three keyword (valid, absoluteKeywordLocation, instanceLocation)" , ( ) => {
250
+ test ( "when error output doesnot contain any of these three keyword (valid, absoluteKeywordLocation, instanceLocation)" , async ( ) => {
251
+ const schema = {
252
+ $id : "https://example.com/polygon"
253
+ } ;
250
254
const errorOutput = {
251
255
valid : false ,
252
256
errors : [
@@ -256,6 +260,75 @@ describe("Error Output Normalization", () => {
256
260
}
257
261
]
258
262
} ;
259
- expect ( ( ) => normalizeOutputFormat ( /** @type any */ ( errorOutput ) ) ) . to . throw ( "error Output must follow Draft 2019-09" ) ;
263
+ await expect ( async ( ) => normalizeOutputFormat ( /** @type any */ ( errorOutput ) , schema ) ) . to . rejects . toThrow ( "error Output must follow Draft 2019-09" ) ;
264
+ } ) ;
265
+
266
+ test ( "correctly resolves keywordLocation through $ref in $defs" , async ( ) => {
267
+ /** @type SchemaObject */
268
+ const schema = {
269
+ $id : "https://example.com/main" ,
270
+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
271
+ properties : {
272
+ foo : { $ref : "#/$defs/lengthDefinition" }
273
+ } ,
274
+ $defs : {
275
+ lengthDefinition : {
276
+ minLength : 3
277
+ }
278
+ }
279
+ } ;
280
+ const instance = { foo : "aa" } ;
281
+ /** @type OutputFormat */
282
+ const output = {
283
+ valid : false ,
284
+ errors : [
285
+ {
286
+ keywordLocation : "/properties/foo/$ref/minLength" ,
287
+ instanceLocation : "#"
288
+ }
289
+ ]
290
+ } ;
291
+
292
+ const result = await betterJsonSchemaErrors ( instance , schema , output ) ;
293
+ expect ( result . errors ) . to . eql ( [
294
+ {
295
+ schemaLocation : "https://example.com/main#/$defs/lengthDefinition/minLength" ,
296
+ instanceLocation : "#" ,
297
+ message : "The instance should be at least 3 characters"
298
+ }
299
+ ] ) ;
300
+ } ) ;
301
+
302
+ test ( "removes schemaLocation nodes from the error output" , async ( ) => {
303
+ const schema = {
304
+ $id : "https://example.com/polygon"
305
+ } ;
306
+ const errorOutput = {
307
+ valid : false ,
308
+ errors : [
309
+ {
310
+ valid : false ,
311
+ keywordLocation : "#/items/$ref" ,
312
+ absoluteKeywordLocation : "https://example.com/polygon#/$defs/point" ,
313
+ instanceLocation : "#/1" ,
314
+ error : "A subschema had errors."
315
+ } ,
316
+ {
317
+ valid : false ,
318
+ keywordLocation : "#/items/$ref/required" ,
319
+ absoluteKeywordLocation : "https://example.com/polygon#/$defs/point/required" ,
320
+ instanceLocation : "#/1" ,
321
+ error : "Required property 'y' not found."
322
+ }
323
+ ]
324
+ } ;
325
+
326
+ expect ( await normalizeOutputFormat ( errorOutput , schema ) ) . to . eql ( [
327
+ {
328
+ valid : false ,
329
+ absoluteKeywordLocation : "https://example.com/polygon#/$defs/point/required" ,
330
+ instanceLocation : "#/1"
331
+ }
332
+ ] ) ;
260
333
} ) ;
261
334
} ) ;
0 commit comments