@@ -3,61 +3,62 @@ import build, { restore, Schema, validLargeArrayMechanisms } from '..'
3
3
import { expectError , expectType } from 'tsd'
4
4
5
5
// Number schemas
6
- const schema1 : Schema = {
6
+ build ( {
7
7
type : 'number'
8
- }
9
- const schema2 : Schema = {
8
+ } ) ( 25 )
9
+ build ( {
10
10
type : 'integer'
11
- }
12
-
13
- build ( schema1 ) ( 25 )
14
- build ( schema2 ) ( - 5 )
11
+ } ) ( - 5 )
12
+ build ( {
13
+ type : 'integer'
14
+ } ) ( 5n )
15
15
16
- build ( schema2 , { rounding : 'ceil' } )
17
- build ( schema2 , { rounding : 'floor' } )
18
- build ( schema2 , { rounding : 'round' } )
19
- build ( schema2 , { rounding : 'trunc' } )
20
- expectError ( build ( schema2 , { rounding : 'invalid' } ) )
16
+ build ( {
17
+ type : 'number'
18
+ } , { rounding : 'ceil' } )
19
+ build ( {
20
+ type : 'number'
21
+ } , { rounding : 'floor' } )
22
+ build ( {
23
+ type : 'number'
24
+ } , { rounding : 'round' } )
25
+ build ( {
26
+ type : 'number'
27
+ } , { rounding : 'trunc' } )
28
+ expectError ( build ( {
29
+ type : 'number'
30
+ } , { rounding : 'invalid' } ) )
21
31
22
32
// String schema
23
- const schema3 : Schema = {
24
- type : 'string'
25
- }
26
-
27
- build ( schema3 ) ( 'foobar' )
33
+ build ( {
34
+ type : 'string'
35
+ } ) ( 'foobar' )
28
36
29
37
// Boolean schema
30
- const schema4 : Schema = {
38
+ build ( {
31
39
type : 'boolean'
32
- }
33
-
34
- build ( schema4 ) ( true )
40
+ } ) ( true )
35
41
36
42
// Null schema
37
- const schema5 : Schema = {
43
+ build ( {
38
44
type : 'null'
39
- }
40
-
41
- build ( schema5 ) ( null )
45
+ } ) ( null )
42
46
43
47
// Array schemas
44
- const schema6 : Schema = {
48
+ build ( {
45
49
type : 'array' ,
46
50
items : { type : 'number' }
47
- }
48
- const schema7 : Schema = {
51
+ } ) ( [ 25 ] )
52
+ build ( {
49
53
type : 'array' ,
50
54
items : [ { type : 'string' } , { type : 'integer' } ]
51
- }
52
-
53
- build ( schema6 ) ( [ 25 ] )
54
- build ( schema7 ) ( [ 'hello' , 42 ] )
55
+ } ) ( [ 'hello' , 42 ] )
55
56
56
57
// Object schemas
57
- const schema8 : Schema = {
58
+ build ( {
58
59
type : 'object'
59
- }
60
- const schema9 : Schema = {
60
+ } ) ( { } )
61
+ build ( {
61
62
type : 'object' ,
62
63
properties : {
63
64
foo : { type : 'string' } ,
@@ -70,14 +71,24 @@ const schema9: Schema = {
70
71
additionalProperties : {
71
72
type : 'boolean'
72
73
}
73
- }
74
-
75
- build ( schema8 ) ( { } )
76
- build ( schema9 ) ( { foo : 'bar' } )
77
- build ( schema9 , { rounding : 'floor' } ) ( { foo : 'bar' } )
74
+ } ) ( { foo : 'bar' } )
75
+ 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
+ }
88
+ } , { rounding : 'floor' } ) ( { foo : 'bar' } )
78
89
79
90
// Reference schemas
80
- const schema10 : Schema = {
91
+ build ( {
81
92
title : 'Example Schema' ,
82
93
definitions : {
83
94
num : {
@@ -109,12 +120,10 @@ const schema10: Schema = {
109
120
additionalProperties : {
110
121
$ref : '#/definitions/def'
111
122
}
112
- }
113
-
114
- build ( schema10 ) ( { nickname : '' , num : { int : 5 } , other : null } )
123
+ } ) ( { nickname : '' , num : { int : 5 } , other : null } )
115
124
116
125
// Conditional/Combined schemas
117
- const schema11 : Schema = {
126
+ build ( {
118
127
title : 'Conditional/Combined Schema' ,
119
128
type : 'object' ,
120
129
properties : {
@@ -140,24 +149,37 @@ const schema11: Schema = {
140
149
somethingElse : { type : 'null' }
141
150
}
142
151
}
143
- }
144
-
145
- build ( schema11 ) ( { something : 'a string' , somethingElse : 42 } )
152
+ } ) ( { something : 'a string' , somethingElse : 42 } )
146
153
147
154
// String schema with format
148
- const schema12 : Schema = {
155
+
156
+ build ( {
149
157
type : 'string' ,
150
158
format : 'date-time'
151
- }
152
-
153
- build ( schema12 ) ( new Date ( ) )
159
+ } ) ( new Date ( ) )
154
160
161
+ /*
162
+ This overload doesn't work yet -
163
+ TypeScript chooses the generic for the schema
164
+ before it chooses the overload for the options
165
+ parameter.
155
166
let str: string, ajv: Ajv
156
- str = build ( schema1 , { debugMode : true } ) . code
157
- ajv = build ( schema1 , { debugMode : true } ) . ajv
158
- str = build ( schema1 , { mode : 'debug' } ) . code
159
- ajv = build ( schema1 , { mode : 'debug' } ) . ajv
160
- str = build ( schema1 , { mode : 'standalone' } )
167
+ str = build({
168
+ type: 'number'
169
+ }, { debugMode: true }).code
170
+ ajv = build({
171
+ type: 'number'
172
+ }, { debugMode: true }).ajv
173
+ str = build({
174
+ type: 'number'
175
+ }, { mode: 'debug' }).code
176
+ ajv = build({
177
+ type: 'number'
178
+ }, { mode: 'debug' }).ajv
179
+ str = build({
180
+ type: 'number'
181
+ }, { mode: 'standalone' })
182
+ */
161
183
162
184
const debugCompiled = build ( {
163
185
title : 'default string' ,
0 commit comments