@cleverbrush/schema 0.0.17 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +213 -163
- package/package.json +3 -3
- package/src/builders/ArraySchemaBuilder.test.ts +270 -0
- package/src/builders/ArraySchemaBuilder.ts +381 -0
- package/src/builders/BooleanSchemaBuilder.test.ts +196 -0
- package/src/builders/BooleanSchemaBuilder.ts +155 -0
- package/src/builders/FunctionSchemaBuilder.test.ts +134 -0
- package/src/builders/FunctionSchemaBuilder.ts +109 -0
- package/src/builders/NumberSchemaBuilder.test.ts +493 -0
- package/src/builders/NumberSchemaBuilder.ts +789 -0
- package/src/builders/ObjectSchemaBuilder.test.ts +657 -0
- package/src/builders/ObjectSchemaBuilder.ts +794 -0
- package/src/builders/SchemaBuilder.test.ts +73 -0
- package/src/builders/SchemaBuilder.ts +135 -0
- package/src/builders/StringSchemaBuilder.test.ts +318 -0
- package/src/builders/StringSchemaBuilder.ts +392 -0
- package/src/builders/UnionSchemaBuilder.test.ts +162 -0
- package/src/builders/UnionSchemaBuilder.ts +154 -0
- package/src/defaultSchemas.ts +44 -0
- package/src/index.ts +58 -190
- package/src/schema.ts +827 -0
- package/src/schemaRegistry.builders.test.ts +1393 -0
- package/src/schemaRegistry.test.ts +118 -0
- package/src/schemaRegistry.ts +461 -0
- package/src/validators/validateArray.ts +4 -7
- package/src/validators/validateBoolean.ts +2 -11
- package/src/validators/validateFunction.ts +25 -0
- package/src/validators/validateNumber.ts +2 -7
- package/src/validators/validateObject.ts +32 -21
- package/src/validators/validateString.ts +2 -7
- package/src/validators/validateUnion.ts +36 -0
- package/dist/index.d.ts +0 -94
- package/dist/index.js +0 -9
- package/dist/schemaValidator.d.ts +0 -16
- package/dist/schemaValidator.js +0 -234
- package/dist/validators/validateArray.d.ts +0 -2
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -33
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -2
- package/dist/validators/validateObject.js +0 -73
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/src/schemaValidator.test.ts +0 -1656
- package/src/schemaValidator.ts +0 -367
package/README.md
CHANGED
|
@@ -29,19 +29,21 @@ Any schema defined by object can contain the following fields:
|
|
|
29
29
|
|
|
30
30
|
`number` schema can be defined as follows:
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
```typescript
|
|
33
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
34
|
+
const validator = new SchemaValidator();
|
|
35
|
+
|
|
36
|
+
// validates for integer number between 1 and 100
|
|
37
|
+
let result = await validator.validate(
|
|
38
|
+
{
|
|
39
|
+
type: 'number',
|
|
40
|
+
isInteger: true,
|
|
41
|
+
min: 1,
|
|
42
|
+
max: 100
|
|
43
|
+
},
|
|
44
|
+
10
|
|
45
|
+
);
|
|
46
|
+
```
|
|
45
47
|
|
|
46
48
|
All fields available (apart of common fields for all schemas) are:
|
|
47
49
|
|
|
@@ -54,43 +56,56 @@ All fields available (apart of common fields for all schemas) are:
|
|
|
54
56
|
|
|
55
57
|
Also it can be defined either by shortcut:
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
```typescript
|
|
60
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
61
|
+
|
|
62
|
+
const validator = new SchemaValidator();
|
|
63
|
+
let result = await validator.validate('number', 10);
|
|
64
|
+
// { valid: true }
|
|
65
|
+
result = await validator.validate('number', 'some string');
|
|
66
|
+
// { valid: false, errors: [ 'expected type number, but saw string' ] }
|
|
67
|
+
```
|
|
63
68
|
|
|
64
69
|
which is equal to:
|
|
65
70
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
```typescript
|
|
72
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
73
|
+
|
|
74
|
+
const validator = new SchemaValidator();
|
|
75
|
+
|
|
76
|
+
let result = await validator.validate(
|
|
77
|
+
{
|
|
69
78
|
type: 'number',
|
|
70
79
|
isRequired: true,
|
|
71
80
|
isNullable: false,
|
|
72
81
|
ensureNotNaN: true,
|
|
73
82
|
ensureIsFinite: true
|
|
74
|
-
},
|
|
75
|
-
|
|
83
|
+
},
|
|
84
|
+
0 / 0
|
|
85
|
+
);
|
|
86
|
+
// { valid: false }
|
|
87
|
+
```
|
|
76
88
|
|
|
77
89
|
### String
|
|
78
90
|
|
|
79
91
|
`string` schema can be defined as follows:
|
|
80
92
|
|
|
81
|
-
|
|
82
|
-
|
|
93
|
+
```typescript
|
|
94
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
83
95
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
const validator = new SchemaValidator();
|
|
97
|
+
|
|
98
|
+
// validates for non empty string no longer than 100 chars
|
|
99
|
+
let result = await validator.validate(
|
|
100
|
+
{
|
|
101
|
+
type: 'string',
|
|
102
|
+
minLength: 1,
|
|
103
|
+
maxLength: 100
|
|
104
|
+
},
|
|
105
|
+
'some value here'
|
|
106
|
+
);
|
|
107
|
+
// { valid: true }
|
|
108
|
+
```
|
|
94
109
|
|
|
95
110
|
All fields available (apart of common fields for all schemas) are:
|
|
96
111
|
|
|
@@ -100,46 +115,59 @@ All fields available (apart of common fields for all schemas) are:
|
|
|
100
115
|
|
|
101
116
|
Also it can be defined either by shortcut:
|
|
102
117
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
let result = await validator.validate('string', 'something');
|
|
106
|
-
// { valid: true }
|
|
107
|
-
result = await validator.validate('string', 10);
|
|
108
|
-
// { valid: false, errors: [ 'expected type string, but saw number' ] }
|
|
118
|
+
```typescript
|
|
119
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
109
120
|
|
|
110
|
-
|
|
121
|
+
const validator = new SchemaValidator();
|
|
122
|
+
|
|
123
|
+
let result = await validator.validate('string', 'something');
|
|
124
|
+
// { valid: true }
|
|
125
|
+
result = await validator.validate('string', 10);
|
|
126
|
+
// { valid: false, errors: [ 'expected type string, but saw number' ] }
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
or by detailed descriptor:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
111
133
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
134
|
+
const validator = new SchemaValidator();
|
|
135
|
+
|
|
136
|
+
let result = await validator.validate(
|
|
137
|
+
{
|
|
115
138
|
type: 'string',
|
|
116
139
|
isNullable: false,
|
|
117
140
|
isRequired: true
|
|
118
|
-
},
|
|
119
|
-
|
|
141
|
+
},
|
|
142
|
+
1230
|
|
143
|
+
);
|
|
144
|
+
// { valid: false }
|
|
145
|
+
```
|
|
120
146
|
|
|
121
147
|
### Array
|
|
122
148
|
|
|
123
149
|
`array` schema can be defined as follows:
|
|
124
150
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
151
|
+
```typescript
|
|
152
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
153
|
+
const validator = new SchemaValidator();
|
|
154
|
+
|
|
155
|
+
// validates for non empty string no longer than 100 chars
|
|
156
|
+
let result = await validator.validate(
|
|
157
|
+
{
|
|
158
|
+
type: 'array',
|
|
159
|
+
minLength: 5,
|
|
160
|
+
maxLength: 10,
|
|
161
|
+
ofType: {
|
|
162
|
+
type: 'number',
|
|
163
|
+
min: 1,
|
|
164
|
+
max: 10
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
[5, 6, 7, 8, 9]
|
|
168
|
+
);
|
|
169
|
+
// { valid: true }
|
|
170
|
+
```
|
|
143
171
|
|
|
144
172
|
All fields available (apart of common fields for all schemas) are:
|
|
145
173
|
|
|
@@ -149,60 +177,74 @@ All fields available (apart of common fields for all schemas) are:
|
|
|
149
177
|
|
|
150
178
|
Also it can be defined either by shortcut:
|
|
151
179
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
let result = await validator.validate('array', []);
|
|
155
|
-
// { valid: true }
|
|
156
|
-
result = await validator.validate('array', 10);
|
|
157
|
-
// { valid: false, errors: [ 'expected array' ] }
|
|
180
|
+
```typescript
|
|
181
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
158
182
|
|
|
159
|
-
|
|
183
|
+
const validator = new SchemaValidator();
|
|
184
|
+
|
|
185
|
+
let result = await validator.validate('array', []);
|
|
186
|
+
// { valid: true }
|
|
187
|
+
result = await validator.validate('array', 10);
|
|
188
|
+
// { valid: false, errors: [ 'expected array' ] }
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
or by detailed descriptor:
|
|
160
192
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
193
|
+
```typescript
|
|
194
|
+
import { SchemaValidator } from '@cleverbrush/schema';
|
|
195
|
+
|
|
196
|
+
const validator = new SchemaValidator();
|
|
197
|
+
|
|
198
|
+
let result = await validator.validate(
|
|
199
|
+
{
|
|
164
200
|
type: 'array'
|
|
165
|
-
},
|
|
166
|
-
|
|
201
|
+
},
|
|
202
|
+
1230
|
|
203
|
+
);
|
|
204
|
+
// { valid: false, errors: [ 'expected array' ] }
|
|
205
|
+
```
|
|
167
206
|
|
|
168
207
|
### Object
|
|
169
208
|
|
|
170
209
|
`object` type allows to define a complex object schema. For example:
|
|
171
210
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
address: {
|
|
183
|
-
type: "object",
|
|
184
|
-
properties: {
|
|
185
|
-
city: "string",
|
|
186
|
-
street: {
|
|
187
|
-
type: "string",
|
|
188
|
-
isRequired: false,
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
211
|
+
```typescript
|
|
212
|
+
await validator.validate(
|
|
213
|
+
{
|
|
214
|
+
type: 'object',
|
|
215
|
+
properties: {
|
|
216
|
+
id: 'number',
|
|
217
|
+
name: {
|
|
218
|
+
type: 'string',
|
|
219
|
+
minLength: 1,
|
|
220
|
+
maxLength: 100
|
|
192
221
|
},
|
|
193
|
-
},
|
|
194
|
-
{
|
|
195
|
-
id: 10,
|
|
196
|
-
name: "Andrew",
|
|
197
222
|
address: {
|
|
198
|
-
|
|
199
|
-
|
|
223
|
+
type: 'object',
|
|
224
|
+
properties: {
|
|
225
|
+
city: 'string',
|
|
226
|
+
street: {
|
|
227
|
+
type: 'string',
|
|
228
|
+
isRequired: false
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
200
232
|
}
|
|
201
|
-
|
|
202
|
-
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
id: 10,
|
|
236
|
+
name: 'Andrew',
|
|
237
|
+
address: {
|
|
238
|
+
city: 'Madrid'
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
// {valid: true}
|
|
243
|
+
```
|
|
203
244
|
|
|
204
245
|
All fields available are:
|
|
205
246
|
|
|
247
|
+
- `noUnknownProperties` - Validator will return an error if this field is set to `true` and validated objects contain fields not defined in the object schema. Equals to `false` by default.
|
|
206
248
|
- `properties` - the list of properties, every property has corresponding schema definition (see example above).
|
|
207
249
|
|
|
208
250
|
## Alternative schema
|
|
@@ -210,94 +252,102 @@ All fields available are:
|
|
|
210
252
|
Everywhere you pass a `schema` object to the library, you may pass an array of `schema` objects which will validate the object to match at least one schema from this list.
|
|
211
253
|
For example, if you want to check that array is consisting either from number or from `{ name: string, value: number }` objects, you can do the following:
|
|
212
254
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
255
|
+
```typescript
|
|
256
|
+
await validator.validate(
|
|
257
|
+
{
|
|
258
|
+
type: 'array',
|
|
216
259
|
ofType: [
|
|
217
|
-
|
|
260
|
+
'number',
|
|
218
261
|
{
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
]
|
|
226
|
-
|
|
227
|
-
|
|
262
|
+
type: 'object',
|
|
263
|
+
properties: {
|
|
264
|
+
name: 'string',
|
|
265
|
+
value: 'number'
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
]
|
|
269
|
+
},
|
|
270
|
+
[
|
|
228
271
|
10,
|
|
229
|
-
{ name:
|
|
272
|
+
{ name: 'something', value: 1 },
|
|
230
273
|
100,
|
|
231
|
-
{ name:
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
274
|
+
{ name: 'another string', value: 1 }
|
|
275
|
+
]
|
|
276
|
+
);
|
|
277
|
+
// {valid: true}
|
|
278
|
+
```
|
|
235
279
|
|
|
236
280
|
## Named schemas
|
|
237
281
|
|
|
238
282
|
There is a possibility to register a schema, give it a name and then reuse it:
|
|
239
283
|
|
|
240
|
-
|
|
284
|
+
```typescript
|
|
285
|
+
const validator = new SchemaValidator().addSchemaType('Address', {
|
|
241
286
|
properties: {
|
|
242
287
|
id: {
|
|
243
|
-
|
|
244
|
-
|
|
288
|
+
type: 'number',
|
|
289
|
+
min: 1
|
|
245
290
|
},
|
|
246
|
-
street:
|
|
247
|
-
zip:
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
type:
|
|
291
|
+
street: 'string',
|
|
292
|
+
zip: 'number'
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
await validator.validate(
|
|
297
|
+
{
|
|
298
|
+
type: 'object',
|
|
254
299
|
properties: {
|
|
255
|
-
name:
|
|
256
|
-
address1:
|
|
257
|
-
address2:
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
name:
|
|
300
|
+
name: 'string',
|
|
301
|
+
address1: 'Address',
|
|
302
|
+
address2: 'Address'
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
name: 'Andrew',
|
|
262
307
|
address1: {
|
|
263
308
|
id: 1,
|
|
264
|
-
street:
|
|
265
|
-
zip: 12345
|
|
309
|
+
street: 'some street',
|
|
310
|
+
zip: 12345
|
|
266
311
|
},
|
|
267
312
|
address2: {
|
|
268
313
|
id: 2,
|
|
269
|
-
street:
|
|
270
|
-
zip: 3456
|
|
271
|
-
},
|
|
314
|
+
street: 'some street 2',
|
|
315
|
+
zip: 3456
|
|
272
316
|
}
|
|
273
|
-
|
|
317
|
+
}
|
|
318
|
+
);
|
|
274
319
|
|
|
275
|
-
|
|
320
|
+
// { valid: true }
|
|
321
|
+
```
|
|
276
322
|
|
|
277
323
|
Also there is a possibility to organize schemas in modules (or even submodules):
|
|
278
324
|
|
|
279
|
-
|
|
325
|
+
```typescript
|
|
326
|
+
const validator = new SchemaValidator()
|
|
327
|
+
.addSchemaType('Module1.DTOs.Address', {
|
|
280
328
|
properties: {
|
|
281
329
|
id: {
|
|
282
|
-
|
|
283
|
-
|
|
330
|
+
type: 'number',
|
|
331
|
+
min: 1
|
|
284
332
|
},
|
|
285
|
-
street:
|
|
286
|
-
zip:
|
|
333
|
+
street: 'string',
|
|
334
|
+
zip: 'number'
|
|
287
335
|
}
|
|
288
|
-
})
|
|
336
|
+
})
|
|
337
|
+
.addSchemaType('Module1.Models.Person', {
|
|
289
338
|
properties: {
|
|
290
|
-
firstName:
|
|
291
|
-
lastName:
|
|
292
|
-
}
|
|
339
|
+
firstName: 'string',
|
|
340
|
+
lastName: 'string'
|
|
341
|
+
}
|
|
293
342
|
});
|
|
294
343
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
344
|
+
const resultPerson = await validator.schemas.Module1.Models.Person.validate({
|
|
345
|
+
fistName: 'John',
|
|
346
|
+
lastName: 'Smith'
|
|
347
|
+
});
|
|
299
348
|
|
|
300
|
-
|
|
349
|
+
const resultAddress = await validator.schemas.Module1.DTOs.Address.validate({});
|
|
350
|
+
```
|
|
301
351
|
|
|
302
352
|
## Examples
|
|
303
353
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cleverbrush/schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"object schema validator",
|
|
6
6
|
"schema",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"watch": "tsc --watch",
|
|
19
19
|
"build": "tsc"
|
|
20
20
|
},
|
|
21
|
-
"
|
|
22
|
-
"@cleverbrush/deep": "0.0.
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@cleverbrush/deep": "1.0.0-beta.1"
|
|
23
23
|
},
|
|
24
24
|
"types": "./dist/index.d.ts"
|
|
25
25
|
}
|