1
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars -- Test using this disabled, see https://github.com/fastify/fast-json-stringify/pull/683
1
2
import Ajv from 'ajv'
2
3
import build , { restore , Schema , validLargeArrayMechanisms } from '..'
3
4
import { expectError , expectType } from 'tsd'
4
5
5
6
// Number schemas
6
7
build ( {
7
- type : 'number'
8
+ type : 'number'
8
9
} ) ( 25 )
9
10
build ( {
10
- type : 'integer'
11
+ type : 'integer'
11
12
} ) ( - 5 )
12
13
build ( {
13
- type : 'integer'
14
+ type : 'integer'
14
15
} ) ( 5n )
15
16
16
17
build ( {
17
- type : 'number'
18
+ type : 'number'
18
19
} , { rounding : 'ceil' } )
19
20
build ( {
20
- type : 'number'
21
+ type : 'number'
21
22
} , { rounding : 'floor' } )
22
23
build ( {
23
- type : 'number'
24
+ type : 'number'
24
25
} , { rounding : 'round' } )
25
26
build ( {
26
- type : 'number'
27
+ type : 'number'
27
28
} , { rounding : 'trunc' } )
28
29
expectError ( build ( {
29
- type : 'number'
30
+ type : 'number'
30
31
} , { rounding : 'invalid' } ) )
31
32
32
33
// String schema
@@ -36,55 +37,55 @@ build({
36
37
37
38
// Boolean schema
38
39
build ( {
39
- type : 'boolean'
40
+ type : 'boolean'
40
41
} ) ( true )
41
42
42
43
// Null schema
43
44
build ( {
44
- type : 'null'
45
+ type : 'null'
45
46
} ) ( null )
46
47
47
48
// Array schemas
48
49
build ( {
49
- type : 'array' ,
50
- items : { type : 'number' }
50
+ type : 'array' ,
51
+ items : { type : 'number' }
51
52
} ) ( [ 25 ] )
52
53
build ( {
53
- type : 'array' ,
54
- items : [ { type : 'string' } , { type : 'integer' } ]
54
+ type : 'array' ,
55
+ items : [ { type : 'string' } , { type : 'integer' } ]
55
56
} ) ( [ 'hello' , 42 ] )
56
57
57
58
// Object schemas
58
59
build ( {
59
- type : 'object'
60
+ type : 'object'
60
61
} ) ( { } )
61
62
build ( {
62
- type : 'object' ,
63
- properties : {
64
- foo : { type : 'string' } ,
65
- bar : { type : 'integer' }
66
- } ,
67
- required : [ 'foo' ] ,
68
- patternProperties : {
69
- 'baz*' : { type : 'null' }
70
- } ,
71
- additionalProperties : {
72
- type : 'boolean'
73
- }
63
+ type : 'object' ,
64
+ properties : {
65
+ foo : { type : 'string' } ,
66
+ bar : { type : 'integer' }
67
+ } ,
68
+ required : [ 'foo' ] ,
69
+ patternProperties : {
70
+ 'baz*' : { type : 'null' }
71
+ } ,
72
+ additionalProperties : {
73
+ type : 'boolean'
74
+ }
74
75
} ) ( { foo : 'bar' } )
75
76
build ( {
76
- type : 'object' ,
77
- properties : {
78
- foo : { type : 'string' } ,
79
- bar : { type : 'integer' }
80
- } ,
81
- required : [ 'foo' ] ,
82
- patternProperties : {
83
- 'baz*' : { type : 'null' }
84
- } ,
85
- additionalProperties : {
86
- type : 'boolean'
87
- }
77
+ type : 'object' ,
78
+ properties : {
79
+ foo : { type : 'string' } ,
80
+ bar : { type : 'integer' }
81
+ } ,
82
+ required : [ 'foo' ] ,
83
+ patternProperties : {
84
+ 'baz*' : { type : 'null' }
85
+ } ,
86
+ additionalProperties : {
87
+ type : 'boolean'
88
+ }
88
89
} , { rounding : 'floor' } ) ( { foo : 'bar' } )
89
90
90
91
// Reference schemas
@@ -113,7 +114,7 @@ build({
113
114
}
114
115
} ,
115
116
patternProperties : {
116
- ' num' : {
117
+ num : {
117
118
$ref : '#/definitions/num'
118
119
}
119
120
} ,
@@ -207,52 +208,52 @@ interface InferenceSchema {
207
208
}
208
209
209
210
const stringify3 = build ( {
210
- type : " object" ,
211
- properties : { a : { type : " string" } } ,
212
- } ) ;
213
- stringify3 < InferenceSchema > ( { id : " 123" } ) ;
214
- stringify3 < InferenceSchema > ( { a : 123 , id : " 123" } ) ;
215
- expectError ( stringify3 < InferenceSchema > ( { anotherOne : " bar" } ) ) ;
216
- expectError ( stringify3 < Schema > ( { a : " bar" } ) ) ;
211
+ type : ' object' ,
212
+ properties : { a : { type : ' string' } } ,
213
+ } )
214
+ stringify3 < InferenceSchema > ( { id : ' 123' } )
215
+ stringify3 < InferenceSchema > ( { a : 123 , id : ' 123' } )
216
+ expectError ( stringify3 < InferenceSchema > ( { anotherOne : ' bar' } ) )
217
+ expectError ( stringify3 < Schema > ( { a : ' bar' } ) )
217
218
218
219
// Without inference
219
220
const stringify4 = build ( {
220
- type : " object" ,
221
- properties : { a : { type : " string" } } ,
222
- } ) ;
223
- stringify4 ( { id : " 123" } ) ;
224
- stringify4 ( { a : 123 , id : " 123" } ) ;
225
- stringify4 ( { anotherOne : " bar" } ) ;
226
- stringify4 ( { a : " bar" } ) ;
221
+ type : ' object' ,
222
+ properties : { a : { type : ' string' } } ,
223
+ } )
224
+ stringify4 ( { id : ' 123' } )
225
+ stringify4 ( { a : 123 , id : ' 123' } )
226
+ stringify4 ( { anotherOne : ' bar' } )
227
+ stringify4 ( { a : ' bar' } )
227
228
228
229
// Without inference - string type
229
230
const stringify5 = build ( {
230
- type : " string" ,
231
- } ) ;
232
- stringify5 ( " foo" ) ;
233
- expectError ( stringify5 ( { id : " 123" } ) ) ;
231
+ type : ' string' ,
232
+ } )
233
+ stringify5 ( ' foo' )
234
+ expectError ( stringify5 ( { id : ' 123' } ) )
234
235
235
236
// Without inference - null type
236
237
const stringify6 = build ( {
237
- type : " null" ,
238
- } ) ;
239
- stringify6 ( null ) ;
240
- expectError ( stringify6 ( " a string" ) ) ;
238
+ type : ' null' ,
239
+ } )
240
+ stringify6 ( null )
241
+ expectError ( stringify6 ( ' a string' ) )
241
242
242
243
// Without inference - boolean type
243
244
const stringify7 = build ( {
244
- type : " boolean" ,
245
- } ) ;
246
- stringify7 ( true ) ;
247
- expectError ( stringify7 ( " a string" ) ) ;
245
+ type : ' boolean' ,
246
+ } )
247
+ stringify7 ( true )
248
+ expectError ( stringify7 ( ' a string' ) )
248
249
249
250
// largeArrayMechanism
250
251
251
- build ( { } , { largeArrayMechanism : 'json-stringify' } )
252
- build ( { } , { largeArrayMechanism : 'default' } )
253
- expectError ( build ( { } as Schema , { largeArrayMechanism : 'invalid' } ) )
252
+ build ( { } , { largeArrayMechanism : 'json-stringify' } )
253
+ build ( { } , { largeArrayMechanism : 'default' } )
254
+ expectError ( build ( { } as Schema , { largeArrayMechanism : 'invalid' } ) )
254
255
255
- build ( { } , { largeArraySize : 2000 } )
256
- build ( { } , { largeArraySize : '2e4' } )
257
- build ( { } , { largeArraySize : 2n } )
258
- expectError ( build ( { } as Schema , { largeArraySize : [ 'asdf' ] } ) )
256
+ build ( { } , { largeArraySize : 2000 } )
257
+ build ( { } , { largeArraySize : '2e4' } )
258
+ build ( { } , { largeArraySize : 2n } )
259
+ expectError ( build ( { } as Schema , { largeArraySize : [ 'asdf' ] } ) )
0 commit comments