@cleverbrush/schema 1.0.0-beta.5 → 1.1.0
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 +118 -320
- package/dist/builders/AnySchemaBuilder.d.ts +76 -0
- package/dist/builders/AnySchemaBuilder.js +112 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
- package/dist/builders/ArraySchemaBuilder.js +254 -111
- package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
- package/dist/builders/BooleanSchemaBuilder.js +134 -74
- package/dist/builders/DateSchemaBuilder.d.ts +179 -0
- package/dist/builders/DateSchemaBuilder.js +433 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +59 -25
- package/dist/builders/FunctionSchemaBuilder.js +102 -58
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
- package/dist/builders/NumberSchemaBuilder.js +345 -165
- package/dist/builders/ObjectSchemaBuilder.d.ts +310 -81
- package/dist/builders/ObjectSchemaBuilder.js +528 -283
- package/dist/builders/SchemaBuilder.d.ts +157 -34
- package/dist/builders/SchemaBuilder.js +258 -71
- package/dist/builders/StringSchemaBuilder.d.ts +138 -13
- package/dist/builders/StringSchemaBuilder.js +261 -115
- package/dist/builders/UnionSchemaBuilder.d.ts +132 -9
- package/dist/builders/UnionSchemaBuilder.js +196 -75
- package/dist/index.d.ts +20 -40
- package/dist/index.js +19 -32
- package/dist/utils/transaction.d.ts +46 -0
- package/dist/utils/transaction.js +178 -0
- package/package.json +27 -14
- package/dist/defaultSchemas.d.ts +0 -4
- package/dist/defaultSchemas.js +0 -45
- package/dist/schema.d.ts +0 -231
- package/dist/schema.js +0 -2
- package/dist/schemaRegistry.d.ts +0 -49
- package/dist/schemaRegistry.js +0 -278
- package/dist/validators/validateArray.d.ts +0 -3
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -30
- package/dist/validators/validateFunction.d.ts +0 -2
- package/dist/validators/validateFunction.js +0 -21
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -3
- package/dist/validators/validateObject.js +0 -95
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/dist/validators/validateUnion.d.ts +0 -3
- package/dist/validators/validateUnion.js +0 -30
- package/src/builders/ArraySchemaBuilder.test.ts +0 -270
- package/src/builders/ArraySchemaBuilder.ts +0 -381
- package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
- package/src/builders/BooleanSchemaBuilder.ts +0 -155
- package/src/builders/FunctionSchemaBuilder.test.ts +0 -145
- package/src/builders/FunctionSchemaBuilder.ts +0 -117
- package/src/builders/NumberSchemaBuilder.test.ts +0 -493
- package/src/builders/NumberSchemaBuilder.ts +0 -789
- package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
- package/src/builders/ObjectSchemaBuilder.ts +0 -794
- package/src/builders/SchemaBuilder.test.ts +0 -73
- package/src/builders/SchemaBuilder.ts +0 -135
- package/src/builders/StringSchemaBuilder.test.ts +0 -318
- package/src/builders/StringSchemaBuilder.ts +0 -392
- package/src/builders/UnionSchemaBuilder.test.ts +0 -162
- package/src/builders/UnionSchemaBuilder.ts +0 -154
- package/src/defaultSchemas.ts +0 -44
- package/src/index.ts +0 -66
- package/src/schema.ts +0 -849
- package/src/schemaRegistry.builders.test.ts +0 -1393
- package/src/schemaRegistry.test.ts +0 -118
- package/src/schemaRegistry.ts +0 -461
- package/src/validators/validateArray.ts +0 -76
- package/src/validators/validateBoolean.ts +0 -36
- package/src/validators/validateFunction.ts +0 -25
- package/src/validators/validateNumber.ts +0 -82
- package/src/validators/validateObject.ts +0 -119
- package/src/validators/validateString.ts +0 -59
- package/src/validators/validateUnion.ts +0 -36
- package/tsconfig.json +0 -16
package/README.md
CHANGED
|
@@ -1,354 +1,152 @@
|
|
|
1
|
-
|
|
1
|
+
This package resides in `@cleverbrush/schema` and contains utilities to define object schemas. Once created a Schema can be used to infer type and validate objects for satisfaction of the Schema.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The library is covered with unit-tests to make sure it works correctly.
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
There are several schema types available:
|
|
8
|
-
|
|
9
|
-
- object
|
|
10
|
-
- string
|
|
11
|
-
- number
|
|
12
|
-
- array
|
|
13
|
-
- function (_TBD_)
|
|
14
|
-
|
|
15
|
-
Schemas could be either described by shortcusts (string value from the list above) or by more detailed specification.
|
|
16
|
-
|
|
17
|
-
## Schema Definitions
|
|
18
|
-
|
|
19
|
-
### Common for all types
|
|
20
|
-
|
|
21
|
-
Any schema defined by object can contain the following fields:
|
|
22
|
-
|
|
23
|
-
- `type` - schema type (see the list above)
|
|
24
|
-
- `isRequired` - defines if `undefined` value is considered valid
|
|
25
|
-
- `isNullable` - defines if `null` value is considered valid
|
|
26
|
-
- `validators` - optional array of custom validation functions (see Examples section at the bottom of this page)
|
|
5
|
+
## Installation
|
|
27
6
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
`number` schema can be defined as follows:
|
|
31
|
-
|
|
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
|
-
);
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cleverbrush/schema
|
|
46
9
|
```
|
|
47
10
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- `isInteger` - ensures that number is integer
|
|
51
|
-
- `ensureIsFinite` - ensures that nubmer is finite
|
|
52
|
-
- `ensureNotNaN` - ensures that number is not NaN
|
|
53
|
-
- `equals` - checks if a value is equal to provided number
|
|
54
|
-
- `min` - checks if a value is bigger or equal than a provided lower bound
|
|
55
|
-
- `max` - checks if a value is lower or equal than a provided upper bound
|
|
56
|
-
|
|
57
|
-
Also it can be defined either by shortcut:
|
|
11
|
+
### An example:
|
|
58
12
|
|
|
59
13
|
```typescript
|
|
60
|
-
import {
|
|
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
|
-
```
|
|
68
|
-
|
|
69
|
-
which is equal to:
|
|
14
|
+
import { InferType, object, number, string, date } from '@cleverbrush/schema';
|
|
70
15
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
let result = await validator.validate(
|
|
77
|
-
{
|
|
78
|
-
type: 'number',
|
|
79
|
-
isRequired: true,
|
|
80
|
-
isNullable: false,
|
|
81
|
-
ensureNotNaN: true,
|
|
82
|
-
ensureIsFinite: true
|
|
83
|
-
},
|
|
84
|
-
0 / 0
|
|
85
|
-
);
|
|
86
|
-
// { valid: false }
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### String
|
|
90
|
-
|
|
91
|
-
`string` schema can be defined as follows:
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
import { SchemaValidator } from '@cleverbrush/schema';
|
|
95
|
-
|
|
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
|
-
```
|
|
109
|
-
|
|
110
|
-
All fields available (apart of common fields for all schemas) are:
|
|
111
|
-
|
|
112
|
-
- `equals` - checks if value is equal to provided string
|
|
113
|
-
- `minLength` - checks if value is at least `minLength` characters long
|
|
114
|
-
- `maxLength` - checks if value is at most `maxLength` characters long
|
|
115
|
-
|
|
116
|
-
Also it can be defined either by shortcut:
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
import { SchemaValidator } from '@cleverbrush/schema';
|
|
120
|
-
|
|
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
|
-
```
|
|
16
|
+
const UserSchema = object({
|
|
17
|
+
id: number(),
|
|
18
|
+
name: string(),
|
|
19
|
+
dateOfBirth: date().optional()
|
|
20
|
+
});
|
|
128
21
|
|
|
129
|
-
|
|
22
|
+
// user has { id: number; name: string; dateOfBirth?: Date } type
|
|
23
|
+
const user: InferType<typeof UserSchema> = {
|
|
24
|
+
//...
|
|
25
|
+
};
|
|
130
26
|
|
|
131
|
-
|
|
132
|
-
import { SchemaValidator } from '@cleverbrush/schema';
|
|
133
|
-
|
|
134
|
-
const validator = new SchemaValidator();
|
|
135
|
-
|
|
136
|
-
let result = await validator.validate(
|
|
137
|
-
{
|
|
138
|
-
type: 'string',
|
|
139
|
-
isNullable: false,
|
|
140
|
-
isRequired: true
|
|
141
|
-
},
|
|
142
|
-
1230
|
|
143
|
-
);
|
|
144
|
-
// { valid: false }
|
|
27
|
+
const { valid, object: result, errors } = await UserSchema.validate(user);
|
|
145
28
|
```
|
|
146
29
|
|
|
147
|
-
|
|
30
|
+
Type inference can be used even on clean JavaScript (without TypeScript):
|
|
148
31
|
|
|
149
|
-
|
|
32
|
+
```javascript
|
|
33
|
+
const { valid, object, errors } = await UserSchema.validate(someObject);
|
|
150
34
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
//
|
|
156
|
-
|
|
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 }
|
|
35
|
+
if (valid) {
|
|
36
|
+
// someObject satisfies UserSchema.
|
|
37
|
+
// And `object` has { id: number; name: string; dateOfBirth: Date; } type.
|
|
38
|
+
} else {
|
|
39
|
+
// someObject does not satisfy UserSchema which means that
|
|
40
|
+
// `errors` contains a list of errors.
|
|
41
|
+
}
|
|
170
42
|
```
|
|
171
43
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
- `ofType` - here you can pass the `schema` and all array elements will be checked to match this schema
|
|
175
|
-
- `minLength` - checks if array is at least `minLength` elements long
|
|
176
|
-
- `maxLength` - checks if array is at most `maxLength` elements long
|
|
177
|
-
|
|
178
|
-
Also it can be defined either by shortcut:
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
import { SchemaValidator } from '@cleverbrush/schema';
|
|
182
|
-
|
|
183
|
-
const validator = new SchemaValidator();
|
|
44
|
+
Another way to have types using just Javascript is to make use of JSDoc:
|
|
184
45
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
46
|
+
```javascript
|
|
47
|
+
/**
|
|
48
|
+
* @type {import('@cleverbrush/schema').InferType<typeof UserSchema>}
|
|
49
|
+
*/
|
|
50
|
+
const user = {
|
|
51
|
+
// inferred type is { id: number; name: string; dateOfBirth?: Date }
|
|
52
|
+
};
|
|
189
53
|
```
|
|
190
54
|
|
|
191
|
-
or
|
|
55
|
+
See [Documentation](https://docs.cleverbrush.com/v1.0/modules/Schema_Definition_And_Validation.html) or `docs` folder for more information.
|
|
192
56
|
|
|
193
|
-
|
|
194
|
-
import { SchemaValidator } from '@cleverbrush/schema';
|
|
195
|
-
|
|
196
|
-
const validator = new SchemaValidator();
|
|
197
|
-
|
|
198
|
-
let result = await validator.validate(
|
|
199
|
-
{
|
|
200
|
-
type: 'array'
|
|
201
|
-
},
|
|
202
|
-
1230
|
|
203
|
-
);
|
|
204
|
-
// { valid: false, errors: [ 'expected array' ] }
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
### Object
|
|
57
|
+
## Schema Types
|
|
208
58
|
|
|
209
|
-
|
|
59
|
+
There are several schema types available out of the box:
|
|
60
|
+
|
|
61
|
+
- `any` - any object. Similar to the `any` type in TypeScript.
|
|
62
|
+
- `string` - string value.
|
|
63
|
+
- `number` - number value.
|
|
64
|
+
- `boolean` - boolean value.
|
|
65
|
+
- `func` - function value.
|
|
66
|
+
- `object` - object schema (you can define list of properties, along with schemas for every property).
|
|
67
|
+
- `date` - defines object of JavaScript `Date` class.
|
|
68
|
+
- `array` - defines array, you can define a schema for Array emelement.
|
|
69
|
+
- `union` - allows to define unions. e.g. `string | number` types (or any combination off schema types from this list).
|
|
70
|
+
- custom schema types. You just need to inherit `SchemaBuilder` abstract class to implement your own schema type.
|
|
71
|
+
|
|
72
|
+
## What is exported from the library?
|
|
73
|
+
|
|
74
|
+
Library exports several functions used to define schemas:
|
|
75
|
+
|
|
76
|
+
- `any`
|
|
77
|
+
- `string`
|
|
78
|
+
- `number`
|
|
79
|
+
- `boolean`
|
|
80
|
+
- `func`
|
|
81
|
+
- `object`
|
|
82
|
+
- `date`
|
|
83
|
+
- `array`
|
|
84
|
+
- `union`
|
|
85
|
+
|
|
86
|
+
All these functions returns so called schema builders.
|
|
87
|
+
Schema builder classes are also exported (in case if you want to develop your own schema builder based on it):
|
|
88
|
+
|
|
89
|
+
- `AnySchemaBuilder`
|
|
90
|
+
- `ArraySchemaBuilder`
|
|
91
|
+
- `BooleanSchemaBuilder`
|
|
92
|
+
- `DateSchemaBuilder`
|
|
93
|
+
- `FunctionSchemaBuilder`
|
|
94
|
+
- `ObjectSchemaBuilder`
|
|
95
|
+
- `NumberSchemaBuilder`
|
|
96
|
+
- `SchemaBuilder` - abstract class.
|
|
97
|
+
- `StringSchemaBuilder`
|
|
98
|
+
- `UnionSchemaBuilder`
|
|
99
|
+
|
|
100
|
+
## Schema Builders Are Immutable
|
|
101
|
+
|
|
102
|
+
All schema builders listed above are immutable, which means that every call of it's methods should return a new Schema builder.
|
|
103
|
+
For example in the example below call to the `.optional()` method will not affect `UserSchema` or any other schemas using it. Instead it will return a new schema builder:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const UserSchema = object({
|
|
107
|
+
id: number(),
|
|
108
|
+
email: string()
|
|
109
|
+
});
|
|
210
110
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
properties: {
|
|
216
|
-
id: 'number',
|
|
217
|
-
name: {
|
|
218
|
-
type: 'string',
|
|
219
|
-
minLength: 1,
|
|
220
|
-
maxLength: 100
|
|
221
|
-
},
|
|
222
|
-
address: {
|
|
223
|
-
type: 'object',
|
|
224
|
-
properties: {
|
|
225
|
-
city: 'string',
|
|
226
|
-
street: {
|
|
227
|
-
type: 'string',
|
|
228
|
-
isRequired: false
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
id: 10,
|
|
236
|
-
name: 'Andrew',
|
|
237
|
-
address: {
|
|
238
|
-
city: 'Madrid'
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
);
|
|
242
|
-
// {valid: true}
|
|
111
|
+
const OrderSchema = object({
|
|
112
|
+
id: number(),
|
|
113
|
+
createdByUser: UserSchema.optional()
|
|
114
|
+
});
|
|
243
115
|
```
|
|
244
116
|
|
|
245
|
-
|
|
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.
|
|
248
|
-
- `properties` - the list of properties, every property has corresponding schema definition (see example above).
|
|
117
|
+
By combining immutable schema builders with TypeScript type inference you can create very powerful schemas
|
|
118
|
+
which can be used to validate objects and infer their types.
|
|
249
119
|
|
|
250
|
-
|
|
120
|
+
Every schema is built using a chain/superposition of calls to the schema builder methods. For example:
|
|
251
121
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
{
|
|
262
|
-
type: 'object',
|
|
263
|
-
properties: {
|
|
264
|
-
name: 'string',
|
|
265
|
-
value: 'number'
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
]
|
|
269
|
-
},
|
|
270
|
-
[
|
|
271
|
-
10,
|
|
272
|
-
{ name: 'something', value: 1 },
|
|
273
|
-
100,
|
|
274
|
-
{ name: 'another string', value: 1 }
|
|
275
|
-
]
|
|
276
|
-
);
|
|
277
|
-
// {valid: true}
|
|
122
|
+
```ts
|
|
123
|
+
const UserSchema = object({
|
|
124
|
+
id: number(),
|
|
125
|
+
email: string()
|
|
126
|
+
})
|
|
127
|
+
.optional()
|
|
128
|
+
.addProps({
|
|
129
|
+
name: string()
|
|
130
|
+
});
|
|
278
131
|
```
|
|
279
132
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
There is a possibility to register a schema, give it a name and then reuse it:
|
|
133
|
+
In the example above `UserSchema` is an object schema with two properties: `id` and `email`. It is also optional and has an additional property `name`.
|
|
283
134
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
properties: {
|
|
287
|
-
id: {
|
|
288
|
-
type: 'number',
|
|
289
|
-
min: 1
|
|
290
|
-
},
|
|
291
|
-
street: 'string',
|
|
292
|
-
zip: 'number'
|
|
293
|
-
}
|
|
294
|
-
});
|
|
135
|
+
There is also a way to define union schemas. For example example below defines an array of strings or numbers
|
|
136
|
+
having at least two elements, but not more than 5:
|
|
295
137
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
address1: 'Address',
|
|
302
|
-
address2: 'Address'
|
|
303
|
-
}
|
|
304
|
-
},
|
|
305
|
-
{
|
|
306
|
-
name: 'Andrew',
|
|
307
|
-
address1: {
|
|
308
|
-
id: 1,
|
|
309
|
-
street: 'some street',
|
|
310
|
-
zip: 12345
|
|
311
|
-
},
|
|
312
|
-
address2: {
|
|
313
|
-
id: 2,
|
|
314
|
-
street: 'some street 2',
|
|
315
|
-
zip: 3456
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
);
|
|
319
|
-
|
|
320
|
-
// { valid: true }
|
|
138
|
+
```ts
|
|
139
|
+
const StringOrNumberArraySchema = array()
|
|
140
|
+
.minLength(2)
|
|
141
|
+
.maxLength(5)
|
|
142
|
+
.of(union(string()).or(number()));
|
|
321
143
|
```
|
|
322
144
|
|
|
323
|
-
|
|
145
|
+
You can go further and restrict number to be in the range of 0 to 100 by adding more constraints:
|
|
324
146
|
|
|
325
|
-
```
|
|
326
|
-
const
|
|
327
|
-
.
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
type: 'number',
|
|
331
|
-
min: 1
|
|
332
|
-
},
|
|
333
|
-
street: 'string',
|
|
334
|
-
zip: 'number'
|
|
335
|
-
}
|
|
336
|
-
})
|
|
337
|
-
.addSchemaType('Module1.Models.Person', {
|
|
338
|
-
properties: {
|
|
339
|
-
firstName: 'string',
|
|
340
|
-
lastName: 'string'
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
const resultPerson = await validator.schemas.Module1.Models.Person.validate({
|
|
345
|
-
fistName: 'John',
|
|
346
|
-
lastName: 'Smith'
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
const resultAddress = await validator.schemas.Module1.DTOs.Address.validate({});
|
|
147
|
+
```ts
|
|
148
|
+
const StringOrNumberArraySchema = array()
|
|
149
|
+
.minLength(2)
|
|
150
|
+
.maxLength(5)
|
|
151
|
+
.of(union(string()).or(number().min(0).max(100)));
|
|
350
152
|
```
|
|
351
|
-
|
|
352
|
-
## Examples
|
|
353
|
-
|
|
354
|
-
For Examples see unit tests in the schemaValidator.tests.ts
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { SchemaBuilder, ValidationResult, ValidationContext } from './SchemaBuilder.js';
|
|
2
|
+
type AnySchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<AnySchemaBuilder<R>['introspect']>>;
|
|
3
|
+
/**
|
|
4
|
+
* Any schema builder class. Similar to the `any` type
|
|
5
|
+
* in TypeScript. Allows to define a schema for `any` value.
|
|
6
|
+
* Use it when you don't know the type of the value.
|
|
7
|
+
*
|
|
8
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
9
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
10
|
+
* directly. Use `any()` function instead.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const schema = any();
|
|
15
|
+
* const result = await schema.validate(123);
|
|
16
|
+
* // result.valid === true
|
|
17
|
+
* // result.object === 123
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class AnySchemaBuilder<TRequired extends boolean = true, TExplicitType = undefined, TResult = TExplicitType extends undefined ? any : TExplicitType> extends SchemaBuilder<TResult, TRequired> {
|
|
21
|
+
/**
|
|
22
|
+
* @hidden
|
|
23
|
+
*/
|
|
24
|
+
static create(props: AnySchemaBuilderCreateProps<any>): AnySchemaBuilder<true, undefined, any>;
|
|
25
|
+
private constructor();
|
|
26
|
+
/**
|
|
27
|
+
* @hidden
|
|
28
|
+
*/
|
|
29
|
+
hasType<T>(notUsed?: T): AnySchemaBuilder<true, T>;
|
|
30
|
+
/**
|
|
31
|
+
* @hidden
|
|
32
|
+
*/
|
|
33
|
+
clearHasType(): AnySchemaBuilder<TRequired, undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Performs validation of the schema over `object`. Basically runs
|
|
36
|
+
* validators, preprocessors and checks for required (if schema is not optional).
|
|
37
|
+
* @param context Optional `ValidationContext` settings.
|
|
38
|
+
*/
|
|
39
|
+
validate(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
40
|
+
protected createFromProps<TReq extends boolean>(props: AnySchemaBuilderCreateProps<TReq>): this;
|
|
41
|
+
/**
|
|
42
|
+
* @hidden
|
|
43
|
+
*/
|
|
44
|
+
required(): AnySchemaBuilder<true, TExplicitType>;
|
|
45
|
+
/**
|
|
46
|
+
* @hidden
|
|
47
|
+
*/
|
|
48
|
+
optional(): AnySchemaBuilder<false, TExplicitType>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a `any` schema.
|
|
52
|
+
* @example
|
|
53
|
+
* ```
|
|
54
|
+
* const anyObject = any();
|
|
55
|
+
*
|
|
56
|
+
* // null - invalid
|
|
57
|
+
* // undefined - invalid
|
|
58
|
+
* // string - valid
|
|
59
|
+
* // {} - valid
|
|
60
|
+
* // Date -valid
|
|
61
|
+
* // { someProp: 123 } - valid
|
|
62
|
+
* // etc
|
|
63
|
+
* ```
|
|
64
|
+
* @example
|
|
65
|
+
* ```
|
|
66
|
+
* const anyObject = any().optional();
|
|
67
|
+
* // null - valid
|
|
68
|
+
* // undefined - valid
|
|
69
|
+
* // string - valid
|
|
70
|
+
* // {} - valid
|
|
71
|
+
* // Date -valid
|
|
72
|
+
* // { someProp: 123 } - valid
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare const any: () => AnySchemaBuilder<true, undefined, any>;
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { SchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
/**
|
|
3
|
+
* Any schema builder class. Similar to the `any` type
|
|
4
|
+
* in TypeScript. Allows to define a schema for `any` value.
|
|
5
|
+
* Use it when you don't know the type of the value.
|
|
6
|
+
*
|
|
7
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
8
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
9
|
+
* directly. Use `any()` function instead.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const schema = any();
|
|
14
|
+
* const result = await schema.validate(123);
|
|
15
|
+
* // result.valid === true
|
|
16
|
+
* // result.object === 123
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export class AnySchemaBuilder extends SchemaBuilder {
|
|
20
|
+
/**
|
|
21
|
+
* @hidden
|
|
22
|
+
*/
|
|
23
|
+
static create(props) {
|
|
24
|
+
return new AnySchemaBuilder({
|
|
25
|
+
type: 'any',
|
|
26
|
+
...props
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
constructor(props) {
|
|
30
|
+
super(props);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @hidden
|
|
34
|
+
*/
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
+
hasType(notUsed) {
|
|
37
|
+
return this.createFromProps({
|
|
38
|
+
...this.introspect()
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @hidden
|
|
43
|
+
*/
|
|
44
|
+
clearHasType() {
|
|
45
|
+
return this.createFromProps({
|
|
46
|
+
...this.introspect()
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Performs validation of the schema over `object`. Basically runs
|
|
51
|
+
* validators, preprocessors and checks for required (if schema is not optional).
|
|
52
|
+
* @param context Optional `ValidationContext` settings.
|
|
53
|
+
*/
|
|
54
|
+
async validate(object, context) {
|
|
55
|
+
const superResult = await super.preValidate(object, context);
|
|
56
|
+
const { valid, transaction: preValidationTransaction, errors } = superResult;
|
|
57
|
+
if (!valid) {
|
|
58
|
+
return {
|
|
59
|
+
valid,
|
|
60
|
+
errors
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
64
|
+
return {
|
|
65
|
+
valid: true,
|
|
66
|
+
object: objToValidate
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
createFromProps(props) {
|
|
70
|
+
return AnySchemaBuilder.create(props);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @hidden
|
|
74
|
+
*/
|
|
75
|
+
required() {
|
|
76
|
+
return super.required();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @hidden
|
|
80
|
+
*/
|
|
81
|
+
optional() {
|
|
82
|
+
return super.optional();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Creates a `any` schema.
|
|
87
|
+
* @example
|
|
88
|
+
* ```
|
|
89
|
+
* const anyObject = any();
|
|
90
|
+
*
|
|
91
|
+
* // null - invalid
|
|
92
|
+
* // undefined - invalid
|
|
93
|
+
* // string - valid
|
|
94
|
+
* // {} - valid
|
|
95
|
+
* // Date -valid
|
|
96
|
+
* // { someProp: 123 } - valid
|
|
97
|
+
* // etc
|
|
98
|
+
* ```
|
|
99
|
+
* @example
|
|
100
|
+
* ```
|
|
101
|
+
* const anyObject = any().optional();
|
|
102
|
+
* // null - valid
|
|
103
|
+
* // undefined - valid
|
|
104
|
+
* // string - valid
|
|
105
|
+
* // {} - valid
|
|
106
|
+
* // Date -valid
|
|
107
|
+
* // { someProp: 123 } - valid
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export const any = () => AnySchemaBuilder.create({
|
|
111
|
+
isRequired: true
|
|
112
|
+
});
|