@tstdl/base 0.90.92 → 0.91.0-beta10
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/api/types.d.ts +1 -1
- package/authentication/authentication.api.d.ts +56 -57
- package/authentication/authentication.api.js +1 -6
- package/authentication/client/api.client.d.ts +4 -5
- package/authentication/client/api.client.js +1 -2
- package/authentication/client/http-client.middleware.d.ts +1 -1
- package/browser/pdf-options.js +8 -9
- package/errors/custom.error.d.ts +5 -5
- package/examples/api/basic-overview.js +4 -4
- package/http/client/http-client-request.d.ts +1 -2
- package/http/client/module.d.ts +1 -1
- package/image-service/image-service.js +4 -7
- package/mail/mail.client.d.ts +2 -2
- package/mail/mail.client.js +3 -5
- package/orm/decorators.d.ts +1 -0
- package/orm/decorators.js +4 -0
- package/orm/entity.js +1 -1
- package/orm/index.d.ts +4 -0
- package/orm/index.js +4 -0
- package/orm/repository.d.ts +6 -8
- package/orm/repository.js +7 -0
- package/orm/schema-converter.d.ts +99 -0
- package/orm/schema-converter.js +74 -0
- package/orm/schema.d.ts +3 -0
- package/orm/schema.js +1 -0
- package/orm/types.d.ts +8 -0
- package/orm/types.js +6 -0
- package/package.json +7 -5
- package/reflection/registry.d.ts +1 -1
- package/reflection/utils.d.ts +2 -2
- package/schema/decorators/class.d.ts +1 -1
- package/schema/decorators/index.d.ts +0 -2
- package/schema/decorators/index.js +0 -2
- package/schema/decorators/property.d.ts +6 -5
- package/schema/decorators/property.js +10 -14
- package/schema/decorators/types.d.ts +7 -12
- package/schema/decorators/utils.d.ts +2 -15
- package/schema/decorators/utils.js +4 -43
- package/schema/index.d.ts +1 -6
- package/schema/index.js +15 -6
- package/schema/schema.d.ts +89 -14
- package/schema/schema.error.d.ts +4 -6
- package/schema/schema.error.js +6 -10
- package/schema/schema.js +74 -276
- package/schema/schemas/any.d.ts +7 -5
- package/schema/schemas/any.js +10 -6
- package/schema/schemas/array.d.ts +11 -12
- package/schema/schemas/array.js +29 -19
- package/schema/schemas/bigint.d.ts +9 -0
- package/schema/schemas/bigint.js +19 -0
- package/schema/schemas/boolean.d.ts +11 -5
- package/schema/schemas/boolean.js +41 -6
- package/schema/schemas/date.d.ts +9 -7
- package/schema/schemas/date.js +24 -18
- package/schema/schemas/defaulted.d.ts +11 -6
- package/schema/schemas/defaulted.js +23 -8
- package/schema/schemas/enumeration.d.ts +10 -5
- package/schema/schemas/enumeration.js +25 -15
- package/schema/schemas/function.d.ts +8 -0
- package/schema/schemas/function.js +14 -0
- package/schema/schemas/index.d.ts +5 -6
- package/schema/schemas/index.js +5 -6
- package/schema/schemas/instance.d.ts +11 -3
- package/schema/schemas/instance.js +25 -4
- package/schema/schemas/literal.d.ts +10 -5
- package/schema/schemas/literal.js +25 -15
- package/schema/schemas/never.d.ts +6 -0
- package/schema/schemas/never.js +11 -0
- package/schema/schemas/nullable.d.ts +11 -6
- package/schema/schemas/nullable.js +24 -7
- package/schema/schemas/number.d.ts +9 -7
- package/schema/schemas/number.js +28 -23
- package/schema/schemas/object.d.ts +84 -7
- package/schema/schemas/object.js +180 -6
- package/schema/schemas/one-or-many.d.ts +10 -7
- package/schema/schemas/one-or-many.js +18 -11
- package/schema/schemas/optional.d.ts +11 -7
- package/schema/schemas/optional.js +24 -6
- package/schema/schemas/readable-stream.d.ts +8 -6
- package/schema/schemas/readable-stream.js +10 -5
- package/schema/schemas/regexp.d.ts +8 -6
- package/schema/schemas/regexp.js +24 -6
- package/schema/schemas/simple.d.ts +27 -0
- package/schema/schemas/simple.js +57 -0
- package/schema/schemas/string.d.ts +9 -21
- package/schema/schemas/string.js +20 -34
- package/schema/schemas/symbol.d.ts +8 -0
- package/schema/schemas/symbol.js +19 -0
- package/schema/schemas/transform.d.ts +9 -5
- package/schema/schemas/transform.js +19 -6
- package/schema/schemas/uint8-array.d.ts +9 -5
- package/schema/schemas/uint8-array.js +17 -26
- package/schema/schemas/union.d.ts +14 -7
- package/schema/schemas/union.js +30 -11
- package/schema/schemas/unknown.d.ts +7 -5
- package/schema/schemas/unknown.js +11 -7
- package/schema/testable.d.ts +3 -0
- package/schema/testable.js +33 -0
- package/schema/types.d.ts +31 -0
- package/schema/types.js +1 -0
- package/schema-old/decorators/class.d.ts +3 -0
- package/schema-old/decorators/class.js +5 -0
- package/schema-old/decorators/index.d.ts +6 -0
- package/schema-old/decorators/index.js +6 -0
- package/schema-old/decorators/property.d.ts +6 -0
- package/schema-old/decorators/property.js +17 -0
- package/schema-old/decorators/types.d.ts +19 -0
- package/schema-old/decorators/types.js +1 -0
- package/schema-old/decorators/utils.d.ts +15 -0
- package/schema-old/decorators/utils.js +48 -0
- package/schema-old/index.d.ts +10 -0
- package/schema-old/index.js +10 -0
- package/schema-old/schema.d.ts +15 -0
- package/schema-old/schema.error.d.ts +26 -0
- package/schema-old/schema.error.js +64 -0
- package/schema-old/schema.js +283 -0
- package/schema-old/schemas/any.d.ts +5 -0
- package/schema-old/schemas/any.js +9 -0
- package/schema-old/schemas/array.d.ts +12 -0
- package/schema-old/schemas/array.js +23 -0
- package/schema-old/schemas/boolean.d.ts +5 -0
- package/schema-old/schemas/boolean.js +9 -0
- package/schema-old/schemas/date.d.ts +8 -0
- package/schema-old/schemas/date.js +22 -0
- package/schema-old/schemas/defaulted.d.ts +6 -0
- package/schema-old/schemas/defaulted.js +10 -0
- package/schema-old/schemas/enumeration.d.ts +6 -0
- package/schema-old/schemas/enumeration.js +17 -0
- package/schema-old/schemas/index.d.ts +26 -0
- package/schema-old/schemas/index.js +26 -0
- package/schema-old/schemas/instance.d.ts +3 -0
- package/schema-old/schemas/instance.js +5 -0
- package/schema-old/schemas/literal.d.ts +5 -0
- package/schema-old/schemas/literal.js +17 -0
- package/schema-old/schemas/nullable.d.ts +6 -0
- package/schema-old/schemas/nullable.js +9 -0
- package/schema-old/schemas/number.d.ts +9 -0
- package/schema-old/schemas/number.js +27 -0
- package/schema-old/schemas/object.d.ts +7 -0
- package/schema-old/schemas/object.js +12 -0
- package/schema-old/schemas/one-or-many.d.ts +9 -0
- package/schema-old/schemas/one-or-many.js +15 -0
- package/schema-old/schemas/optional.d.ts +7 -0
- package/schema-old/schemas/optional.js +8 -0
- package/schema-old/schemas/readable-stream.d.ts +6 -0
- package/schema-old/schemas/readable-stream.js +9 -0
- package/schema-old/schemas/regexp.d.ts +6 -0
- package/schema-old/schemas/regexp.js +9 -0
- package/schema-old/schemas/string.d.ts +22 -0
- package/schema-old/schemas/string.js +38 -0
- package/schema-old/schemas/transform.d.ts +5 -0
- package/schema-old/schemas/transform.js +7 -0
- package/schema-old/schemas/uint8-array.d.ts +10 -0
- package/schema-old/schemas/uint8-array.js +29 -0
- package/schema-old/schemas/union.d.ts +7 -0
- package/schema-old/schemas/union.js +13 -0
- package/schema-old/schemas/unknown.d.ts +5 -0
- package/schema-old/schemas/unknown.js +9 -0
- package/templates/resolvers/string.template-resolver.d.ts +0 -1
- package/templates/resolvers/string.template-resolver.js +1 -5
- package/templates/template.model.js +1 -1
- package/types.d.ts +2 -0
- package/utils/type-guards.js +1 -1
- /package/{schema → schema-old}/array-constraints/index.d.ts +0 -0
- /package/{schema → schema-old}/array-constraints/index.js +0 -0
- /package/{schema → schema-old}/array-constraints/maximum-length.d.ts +0 -0
- /package/{schema → schema-old}/array-constraints/maximum-length.js +0 -0
- /package/{schema → schema-old}/array-constraints/minimum-length.d.ts +0 -0
- /package/{schema → schema-old}/array-constraints/minimum-length.js +0 -0
- /package/{schema → schema-old}/coercers/boolean.coercer.d.ts +0 -0
- /package/{schema → schema-old}/coercers/boolean.coercer.js +0 -0
- /package/{schema → schema-old}/coercers/date.coercer.d.ts +0 -0
- /package/{schema → schema-old}/coercers/date.coercer.js +0 -0
- /package/{schema → schema-old}/coercers/index.d.ts +0 -0
- /package/{schema → schema-old}/coercers/index.js +0 -0
- /package/{schema → schema-old}/coercers/number.coercer.d.ts +0 -0
- /package/{schema → schema-old}/coercers/number.coercer.js +0 -0
- /package/{schema → schema-old}/coercers/regexp.coercer.d.ts +0 -0
- /package/{schema → schema-old}/coercers/regexp.coercer.js +0 -0
- /package/{schema → schema-old}/coercers/string.coercer.d.ts +0 -0
- /package/{schema → schema-old}/coercers/string.coercer.js +0 -0
- /package/{schema → schema-old}/coercers/uint8-array.coercer.d.ts +0 -0
- /package/{schema → schema-old}/coercers/uint8-array.coercer.js +0 -0
- /package/{schema → schema-old}/constraints/enumeration.d.ts +0 -0
- /package/{schema → schema-old}/constraints/enumeration.js +0 -0
- /package/{schema → schema-old}/constraints/generic.d.ts +0 -0
- /package/{schema → schema-old}/constraints/generic.js +0 -0
- /package/{schema → schema-old}/constraints/index.d.ts +0 -0
- /package/{schema → schema-old}/constraints/index.js +0 -0
- /package/{schema → schema-old}/constraints/integer.d.ts +0 -0
- /package/{schema → schema-old}/constraints/integer.js +0 -0
- /package/{schema → schema-old}/constraints/length.d.ts +0 -0
- /package/{schema → schema-old}/constraints/length.js +0 -0
- /package/{schema → schema-old}/constraints/literal.d.ts +0 -0
- /package/{schema → schema-old}/constraints/literal.js +0 -0
- /package/{schema → schema-old}/constraints/maximum-date.d.ts +0 -0
- /package/{schema → schema-old}/constraints/maximum-date.js +0 -0
- /package/{schema → schema-old}/constraints/maximum-length.d.ts +0 -0
- /package/{schema → schema-old}/constraints/maximum-length.js +0 -0
- /package/{schema → schema-old}/constraints/maximum.d.ts +0 -0
- /package/{schema → schema-old}/constraints/maximum.js +0 -0
- /package/{schema → schema-old}/constraints/minimum-date.d.ts +0 -0
- /package/{schema → schema-old}/constraints/minimum-date.js +0 -0
- /package/{schema → schema-old}/constraints/minimum-length.d.ts +0 -0
- /package/{schema → schema-old}/constraints/minimum-length.js +0 -0
- /package/{schema → schema-old}/constraints/minimum.d.ts +0 -0
- /package/{schema → schema-old}/constraints/minimum.js +0 -0
- /package/{schema → schema-old}/constraints/pattern.d.ts +0 -0
- /package/{schema → schema-old}/constraints/pattern.js +0 -0
- /package/{schema → schema-old}/decorators/coerce.d.ts +0 -0
- /package/{schema → schema-old}/decorators/coerce.js +0 -0
- /package/{schema → schema-old}/decorators/constraint.d.ts +0 -0
- /package/{schema → schema-old}/decorators/constraint.js +0 -0
- /package/{schema → schema-old}/schemas/assign.d.ts +0 -0
- /package/{schema → schema-old}/schemas/assign.js +0 -0
- /package/{schema → schema-old}/schemas/constraint.d.ts +0 -0
- /package/{schema → schema-old}/schemas/constraint.js +0 -0
- /package/{schema → schema-old}/schemas/omit.d.ts +0 -0
- /package/{schema → schema-old}/schemas/omit.js +0 -0
- /package/{schema → schema-old}/schemas/partial.d.ts +0 -0
- /package/{schema → schema-old}/schemas/partial.js +0 -0
- /package/{schema → schema-old}/schemas/pick.d.ts +0 -0
- /package/{schema → schema-old}/schemas/pick.js +0 -0
- /package/{schema → schema-old}/schemas/record.d.ts +0 -0
- /package/{schema → schema-old}/schemas/record.js +0 -0
- /package/{schema → schema-old}/transformers/generic.d.ts +0 -0
- /package/{schema → schema-old}/transformers/generic.js +0 -0
- /package/{schema → schema-old}/transformers/index.d.ts +0 -0
- /package/{schema → schema-old}/transformers/index.js +0 -0
- /package/{schema → schema-old}/transformers/lowercase.d.ts +0 -0
- /package/{schema → schema-old}/transformers/lowercase.js +0 -0
- /package/{schema → schema-old}/transformers/trim.d.ts +0 -0
- /package/{schema → schema-old}/transformers/trim.js +0 -0
- /package/{schema → schema-old}/transformers/uppercase.d.ts +0 -0
- /package/{schema → schema-old}/transformers/uppercase.js +0 -0
- /package/{schema → schema-old}/types/index.d.ts +0 -0
- /package/{schema → schema-old}/types/index.js +0 -0
- /package/{schema → schema-old}/types/schema-array-constraint.d.ts +0 -0
- /package/{schema → schema-old}/types/schema-array-constraint.js +0 -0
- /package/{schema → schema-old}/types/schema-value-coercer.d.ts +0 -0
- /package/{schema → schema-old}/types/schema-value-coercer.js +0 -0
- /package/{schema → schema-old}/types/schema-value-constraint.d.ts +0 -0
- /package/{schema → schema-old}/types/schema-value-constraint.js +0 -0
- /package/{schema → schema-old}/types/schema-value-transformer.d.ts +0 -0
- /package/{schema → schema-old}/types/schema-value-transformer.js +0 -0
- /package/{schema → schema-old}/types/types.d.ts +0 -0
- /package/{schema → schema-old}/types/types.js +0 -0
- /package/{schema → schema-old}/utils/index.d.ts +0 -0
- /package/{schema → schema-old}/utils/index.js +0 -0
- /package/{schema → schema-old}/utils/schema.d.ts +0 -0
- /package/{schema → schema-old}/utils/schema.js +0 -0
- /package/{schema → schema-old}/utils/value-type.d.ts +0 -0
- /package/{schema → schema-old}/utils/value-type.js +0 -0
package/schema/schema.error.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { CustomError } from '../errors/custom.error.js';
|
|
1
|
+
import { CustomError, type CustomErrorOptions } from '../errors/custom.error.js';
|
|
3
2
|
import type { JsonPath } from '../json-path/index.js';
|
|
4
|
-
import type { OneOrMany, TypedOmit, UndefinableJson } from '../types.js';
|
|
3
|
+
import type { AbstractConstructor, OneOrMany, TypedOmit, UndefinableJson } from '../types.js';
|
|
5
4
|
import type { ErrorExtraInfo } from '../utils/format-error.js';
|
|
6
|
-
import type { ValueType } from './types/index.js';
|
|
7
5
|
export type SchemaErrorOptions = Pick<CustomErrorOptions, 'fast'> & {
|
|
8
6
|
path: string | JsonPath;
|
|
9
7
|
details?: UndefinableJson;
|
|
@@ -16,10 +14,10 @@ export declare class SchemaError extends CustomError implements ErrorExtraInfo {
|
|
|
16
14
|
readonly details?: UndefinableJson;
|
|
17
15
|
readonly inner?: OneOrMany<SchemaError>;
|
|
18
16
|
constructor(message: string, options: SchemaErrorOptions, cause?: any);
|
|
19
|
-
static expectedButGot(expected: OneOrMany<string |
|
|
17
|
+
static expectedButGot(expected: OneOrMany<string | AbstractConstructor>, got: string, path: string | JsonPath, options?: TypedOmit<SchemaErrorOptions, 'path'> & {
|
|
20
18
|
customMessage?: string;
|
|
21
19
|
}): SchemaError;
|
|
22
|
-
static couldNotCoerce(expected: OneOrMany<string |
|
|
20
|
+
static couldNotCoerce(expected: OneOrMany<string | AbstractConstructor>, got: string, path: string | JsonPath, options: TypedOmit<SchemaErrorOptions, 'path'> & {
|
|
23
21
|
customMessage?: string;
|
|
24
22
|
}): SchemaError;
|
|
25
23
|
getExtraInfo(includeMessage?: boolean): UndefinableJson | undefined;
|
package/schema/schema.error.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { CustomError } from '../errors/custom.error.js';
|
|
2
2
|
import { toArray } from '../utils/array/array.js';
|
|
3
|
-
import { isArray, isDefined, isNotNullOrUndefined, isString } from '../utils/type-guards.js';
|
|
4
|
-
import { getExpectString } from './schema.js';
|
|
5
|
-
import { getValueTypeName } from './utils/index.js';
|
|
3
|
+
import { isArray, isDefined, isFunction, isNotNullOrUndefined, isString } from '../utils/type-guards.js';
|
|
6
4
|
export class SchemaError extends CustomError {
|
|
7
5
|
static errorName = 'SchemaError';
|
|
8
6
|
path;
|
|
@@ -24,24 +22,22 @@ export class SchemaError extends CustomError {
|
|
|
24
22
|
}
|
|
25
23
|
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
26
24
|
static expectedButGot(expected, got, path, options) {
|
|
27
|
-
const expectedNames = toArray(expected).map((
|
|
28
|
-
const gotName = isString(got) ? got : getValueTypeName(got);
|
|
25
|
+
const expectedNames = toArray(expected).map((e) => isFunction(e) ? e.name : e);
|
|
29
26
|
const expectedString = expectedNames.length == 1
|
|
30
27
|
? expectedNames[0]
|
|
31
28
|
: `(${expectedNames.join(' | ')})`;
|
|
32
|
-
const customMessage = isDefined(options
|
|
33
|
-
const message = `Expected ${expectedString} but got ${
|
|
29
|
+
const customMessage = isDefined(options?.customMessage) ? `: ${options.customMessage}` : '.';
|
|
30
|
+
const message = `Expected ${expectedString} but got ${got}${customMessage}`;
|
|
34
31
|
return new SchemaError(message, { path, ...options });
|
|
35
32
|
}
|
|
36
33
|
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
37
34
|
static couldNotCoerce(expected, got, path, options) {
|
|
38
|
-
const expectedNames = toArray(expected).map((
|
|
39
|
-
const gotText = isString(got) ? got : getValueTypeName(got);
|
|
35
|
+
const expectedNames = toArray(expected).map((e) => isFunction(e) ? e.name : e);
|
|
40
36
|
const expectedString = expectedNames.length == 1
|
|
41
37
|
? expectedNames[0]
|
|
42
38
|
: `[${expectedNames.join(', ')}]`;
|
|
43
39
|
const customMessageString = isDefined(options.customMessage) ? `: ${options.customMessage}` : '.';
|
|
44
|
-
const errorMessage = `Could not coerce ${
|
|
40
|
+
const errorMessage = `Could not coerce ${got} to ${expectedString}${customMessageString}`;
|
|
45
41
|
return new SchemaError(errorMessage, { path, ...options });
|
|
46
42
|
}
|
|
47
43
|
getExtraInfo(includeMessage = false) {
|
package/schema/schema.js
CHANGED
|
@@ -1,283 +1,81 @@
|
|
|
1
|
-
import { JsonPath } from '../json-path/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
import { JsonPath } from '../json-path/json-path.js';
|
|
2
|
+
export class Schema {
|
|
3
|
+
/**
|
|
4
|
+
* Test an unknown value to see whether it corresponds to the schema.
|
|
5
|
+
* @param schema schema to test against
|
|
6
|
+
* @param value value to test
|
|
7
|
+
* @param options validation options
|
|
8
|
+
* @returns test result
|
|
9
|
+
*/
|
|
10
|
+
static test;
|
|
11
|
+
/**
|
|
12
|
+
* Validate an unknown value to see whether it corresponds to the schema.
|
|
13
|
+
* @param schema schema to validate against
|
|
14
|
+
* @param value value to validate
|
|
15
|
+
* @param options validation options
|
|
16
|
+
* @returns validation result
|
|
17
|
+
*/
|
|
18
|
+
static validate;
|
|
19
|
+
/**
|
|
20
|
+
* Asserts an unknown value to be valid according to the schema.
|
|
21
|
+
* @param schema schema to validate against
|
|
22
|
+
* @param value value to validate
|
|
23
|
+
* @param options validation options
|
|
24
|
+
*/
|
|
25
|
+
static assert;
|
|
26
|
+
/**
|
|
27
|
+
* Parse an unknown value to comply with the scheme.
|
|
28
|
+
* @param schema schema to validate against
|
|
29
|
+
* @param value value to validate
|
|
30
|
+
* @param options validation options
|
|
31
|
+
* @returns validation result
|
|
32
|
+
*/
|
|
33
|
+
static parse;
|
|
34
|
+
/**
|
|
35
|
+
* Test an unknown value to see whether it corresponds to the schema.
|
|
36
|
+
* @param schema schema to test against
|
|
37
|
+
* @param value value to test
|
|
38
|
+
* @param options validation options
|
|
39
|
+
* @returns test result with either the value or validation error
|
|
40
|
+
*/
|
|
41
|
+
test(value, options = {}) {
|
|
42
|
+
return this._test(value, JsonPath.ROOT, options);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Validate an unknown value to see whether it corresponds to the schema.
|
|
46
|
+
* @param schema schema to validate against
|
|
47
|
+
* @param value value to validate
|
|
48
|
+
* @param options validation options
|
|
49
|
+
* @returns validation result. Throws if validation fails
|
|
50
|
+
*/
|
|
51
|
+
validate(value, options = {}) {
|
|
52
|
+
const result = this._test(value, JsonPath.ROOT, options);
|
|
46
53
|
return result.valid;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Asserts an unknown value to be valid according to the schema.
|
|
57
|
+
* @param schema schema to validate against
|
|
58
|
+
* @param value value to validate
|
|
59
|
+
* @param options validation options
|
|
60
|
+
*/
|
|
61
|
+
assert(value, options = {}) {
|
|
62
|
+
const result = this._test(value, JsonPath.ROOT, options);
|
|
63
|
+
if (!result.valid) {
|
|
64
|
+
throw result.error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse an unknown value to comply with the scheme.
|
|
69
|
+
* @param schema schema to parse against
|
|
70
|
+
* @param value value to parse
|
|
71
|
+
* @param options validation options
|
|
72
|
+
* @returns parsed value
|
|
73
|
+
*/
|
|
74
|
+
parse(value, options = {}) {
|
|
75
|
+
const result = this._test(value, JsonPath.ROOT, options);
|
|
51
76
|
if (result.valid) {
|
|
52
77
|
return result.value;
|
|
53
78
|
}
|
|
54
79
|
throw result.error;
|
|
55
|
-
},
|
|
56
|
-
function(argumentSchemas, returnSchema, handler) {
|
|
57
|
-
const name = `validated${handler.name.slice(0, 1).toUpperCase()}${handler.name.slice(1)}`;
|
|
58
|
-
const schema = getFunctionParametersSchema(argumentSchemas);
|
|
59
|
-
return {
|
|
60
|
-
[name](...unsafeArgs) {
|
|
61
|
-
const safeArgs = Schema.parse(schema, unsafeArgs, { mask: true }); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
|
|
62
|
-
const unsafeResult = handler(...safeArgs);
|
|
63
|
-
return Schema.parse(returnSchema, unsafeResult); // eslint-disable-line @typescript-eslint/no-unsafe-return
|
|
64
|
-
}
|
|
65
|
-
}[name];
|
|
66
|
-
},
|
|
67
|
-
asyncFunction(argumentSchemas, returnSchema, handler) {
|
|
68
|
-
const name = `validated${handler.name.slice(0, 1).toUpperCase()}${handler.name.slice(1)}`;
|
|
69
|
-
const schema = getFunctionParametersSchema(argumentSchemas);
|
|
70
|
-
return {
|
|
71
|
-
async [name](...unsafeArgs) {
|
|
72
|
-
const safeArgs = Schema.parse(schema, unsafeArgs, { mask: true }); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
|
|
73
|
-
const unsafeResult = await handler(...safeArgs);
|
|
74
|
-
return Schema.parse(returnSchema, unsafeResult); // eslint-disable-line @typescript-eslint/no-unsafe-return
|
|
75
|
-
}
|
|
76
|
-
}[name];
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
function getFunctionParametersSchema(argumentSchemas) {
|
|
80
|
-
const schema = {
|
|
81
|
-
factory: { type: Array },
|
|
82
|
-
properties: {}
|
|
83
|
-
};
|
|
84
|
-
argumentSchemas.forEach((arg, index) => (schema.properties[index] = arg));
|
|
85
|
-
return schema;
|
|
86
|
-
}
|
|
87
|
-
export function testSchema(schema, value, options, path = JsonPath.ROOT) {
|
|
88
|
-
initialize();
|
|
89
|
-
if (isValueSchema(schema)) {
|
|
90
|
-
return testValue(schema, value, options, path);
|
|
91
|
-
}
|
|
92
|
-
if (isTypeSchema(schema)) {
|
|
93
|
-
return testType(schema, value, options, path);
|
|
94
|
-
}
|
|
95
|
-
if (isObjectSchema(schema)) {
|
|
96
|
-
return testObject(schema, value, options, path);
|
|
97
|
-
}
|
|
98
|
-
throw new Error('Unsupported schema');
|
|
99
|
-
}
|
|
100
|
-
function testType(schema, value, options = {}, path = JsonPath.ROOT) {
|
|
101
|
-
const resolvedValueType = resolveValueType(schema.type);
|
|
102
|
-
if (isFunction(resolvedValueType)) {
|
|
103
|
-
if ((value instanceof resolvedValueType) || (getValueType(value) == resolvedValueType)) {
|
|
104
|
-
return { valid: true, value: value };
|
|
105
|
-
}
|
|
106
|
-
const objectSchema = tryGetObjectSchemaFromReflection(resolvedValueType);
|
|
107
|
-
if (isNotNull(objectSchema)) {
|
|
108
|
-
return testObject(objectSchema, value, options, path);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
else if ((resolvedValueType == 'any') || ((resolvedValueType == 'null') && isNull(value)) || ((resolvedValueType == 'undefined') && isUndefined(value))) {
|
|
112
|
-
return { valid: true, value: value };
|
|
113
|
-
}
|
|
114
|
-
return { valid: false, error: SchemaError.expectedButGot(resolvedValueType, getValueType(value), path, { fast: options.fastErrors }) };
|
|
115
|
-
}
|
|
116
|
-
// eslint-disable-next-line complexity
|
|
117
|
-
function testObject(objectSchema, value, options = {}, path = JsonPath.ROOT) {
|
|
118
|
-
if (!(value instanceof Object)) {
|
|
119
|
-
return { valid: false, error: SchemaError.expectedButGot(objectSchema.sourceType ?? 'object', getValueType(value), path, { fast: options.fastErrors }) };
|
|
120
|
-
}
|
|
121
|
-
const schema = normalizeObjectSchema(objectSchema);
|
|
122
|
-
const mask = schema.mask ?? options.mask ?? false;
|
|
123
|
-
const resultValue = isDefined(schema.factory?.type) ? new schema.factory.type() : {};
|
|
124
|
-
const schemaPropertyKeys = objectKeys(schema.properties);
|
|
125
|
-
const valuePropertyKeys = objectKeys(value);
|
|
126
|
-
const unknownValuePropertyKeys = differenceSets(new Set(valuePropertyKeys), new Set(schemaPropertyKeys));
|
|
127
|
-
if ((unknownValuePropertyKeys.length > 0) && !mask && !schema.allowUnknownProperties) {
|
|
128
|
-
return { valid: false, error: new SchemaError('Unknown property', { path: path.add(unknownValuePropertyKeys[0]), fast: options.fastErrors }) };
|
|
129
|
-
}
|
|
130
|
-
for (const key of schemaPropertyKeys) {
|
|
131
|
-
const propertyResult = testSchema(schema.properties[key], value[key], options, path.add(key));
|
|
132
|
-
if (!propertyResult.valid) {
|
|
133
|
-
return propertyResult;
|
|
134
|
-
}
|
|
135
|
-
resultValue[key] = propertyResult.value;
|
|
136
|
-
}
|
|
137
|
-
if (schema.allowUnknownProperties) {
|
|
138
|
-
for (const key of unknownValuePropertyKeys) {
|
|
139
|
-
const propertyPath = path.add(key);
|
|
140
|
-
if (isDefined(schema.unknownPropertiesKey)) {
|
|
141
|
-
const keyResult = testSchema(schema.unknownPropertiesKey, key, options);
|
|
142
|
-
if (!keyResult.valid && !mask) {
|
|
143
|
-
return { valid: false, error: new SchemaError('Invalid property key.', { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
const propertyResult = testSchema(schema.unknownProperties, value[key], options, path.add(key));
|
|
147
|
-
if (!propertyResult.valid && !mask) {
|
|
148
|
-
return propertyResult;
|
|
149
|
-
}
|
|
150
|
-
resultValue[key] = propertyResult.value;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const testResultValue = isUndefined(schema.factory) ? resultValue : isDefined(schema.factory.type) ? resultValue : schema.factory.builder(resultValue);
|
|
154
|
-
return { valid: true, value: testResultValue };
|
|
155
|
-
}
|
|
156
|
-
// eslint-disable-next-line max-lines-per-function, max-statements, complexity
|
|
157
|
-
function testValue(schema, value, options = {}, path = JsonPath.ROOT) {
|
|
158
|
-
const normalizedValueSchema = normalizeValueSchema(schema);
|
|
159
|
-
if (normalizedValueSchema.optional && isUndefined(value)) {
|
|
160
|
-
return { valid: true, value: value };
|
|
161
80
|
}
|
|
162
|
-
if (normalizedValueSchema.nullable && isNull(value)) {
|
|
163
|
-
return { valid: true, value: value };
|
|
164
|
-
}
|
|
165
|
-
const context = {
|
|
166
|
-
schema: normalizedValueSchema,
|
|
167
|
-
options
|
|
168
|
-
};
|
|
169
|
-
/** handle arrays */
|
|
170
|
-
if (normalizedValueSchema.array) {
|
|
171
|
-
if (!isArray(value)) {
|
|
172
|
-
if (normalizedValueSchema.coerce) {
|
|
173
|
-
return testValue(schema, [value], options, path);
|
|
174
|
-
}
|
|
175
|
-
return { valid: false, error: SchemaError.expectedButGot(Array, getValueType(value), path, { fast: options.fastErrors }) };
|
|
176
|
-
}
|
|
177
|
-
for (const arrayConstraint of normalizedValueSchema.arrayConstraints) {
|
|
178
|
-
const result = arrayConstraint.validate(value, path, context);
|
|
179
|
-
if (!result.valid) {
|
|
180
|
-
return { valid: false, error: result.error };
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
const itemSchema = getArrayItemSchema(schema);
|
|
184
|
-
const validatedItems = [];
|
|
185
|
-
for (let i = 0; i < value.length; i++) {
|
|
186
|
-
const result = testValue(itemSchema, value[i], options, path.add(i));
|
|
187
|
-
if (!result.valid) {
|
|
188
|
-
return result;
|
|
189
|
-
}
|
|
190
|
-
validatedItems.push(result.value);
|
|
191
|
-
}
|
|
192
|
-
return { valid: true, value: validatedItems };
|
|
193
|
-
}
|
|
194
|
-
let valueTestResult;
|
|
195
|
-
let resultValue;
|
|
196
|
-
let valueType;
|
|
197
|
-
function updateCurrentState(newValue) {
|
|
198
|
-
valueTestResult = isValidValue(normalizedValueSchema.schema, newValue, options, path);
|
|
199
|
-
resultValue = valueTestResult.valid ? valueTestResult.value : newValue;
|
|
200
|
-
valueType = getValueType(resultValue);
|
|
201
|
-
}
|
|
202
|
-
updateCurrentState(value);
|
|
203
|
-
/** try to coerce */
|
|
204
|
-
if (!valueTestResult.valid) {
|
|
205
|
-
const targetTypes = getSchemaValueTypes(schema);
|
|
206
|
-
const coercers = [
|
|
207
|
-
...(normalizedValueSchema.coercers.get(valueType) ?? []),
|
|
208
|
-
...((normalizedValueSchema.coerce || options.coerce == true) ? (defaultCoercers.get(valueType) ?? []) : [])
|
|
209
|
-
]
|
|
210
|
-
.filter((coercer) => includesValueType(coercer.targetType, targetTypes));
|
|
211
|
-
const errors = [];
|
|
212
|
-
let success = false;
|
|
213
|
-
for (const coercer of coercers) {
|
|
214
|
-
const coerceResult = coercer.coerce(resultValue, path, context);
|
|
215
|
-
if (!coerceResult.success) {
|
|
216
|
-
errors.push(coerceResult.error);
|
|
217
|
-
continue;
|
|
218
|
-
}
|
|
219
|
-
success = true;
|
|
220
|
-
updateCurrentState(coerceResult.value);
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
if (!success && (errors.length > 0)) {
|
|
224
|
-
return { valid: false, error: (errors.length == 1) ? errors[0] : new SchemaError('Value could not be coerced.', { inner: errors, path, fast: options.fastErrors }) };
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (!valueTestResult.valid) {
|
|
228
|
-
return valueTestResult;
|
|
229
|
-
}
|
|
230
|
-
if (normalizedValueSchema.valueConstraints.length > 0) {
|
|
231
|
-
const errors = [];
|
|
232
|
-
for (const constraint of normalizedValueSchema.valueConstraints) {
|
|
233
|
-
const result = constraint.validate(resultValue, path, context);
|
|
234
|
-
if (!result.valid) {
|
|
235
|
-
errors.push(result.error);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (errors.length > 0) {
|
|
239
|
-
return { valid: false, error: (errors.length == 1) ? errors[0] : new SchemaError('Value did not match schema.', { path, inner: errors, fast: options.fastErrors }) };
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
if (normalizedValueSchema.transformers.length > 0) {
|
|
243
|
-
for (const transformer of normalizedValueSchema.transformers) {
|
|
244
|
-
if (isDefined(transformer.sourceType) && !includesValueType(valueType, resolveValueTypes(transformer.sourceType))) {
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
const transformResult = transformer.transform(resultValue, path, context);
|
|
248
|
-
if (isTransformErrorResult(transformResult)) {
|
|
249
|
-
return { valid: false, error: transformResult[transformErrorResultSymbol] };
|
|
250
|
-
}
|
|
251
|
-
return { valid: true, value: transformResult };
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return { valid: true, value: resultValue };
|
|
255
|
-
}
|
|
256
|
-
function isValidValue(validSchemas, value, options, path) {
|
|
257
|
-
const errorResults = [];
|
|
258
|
-
for (const schema of validSchemas) {
|
|
259
|
-
const result = testSchema(schema, value, options, path);
|
|
260
|
-
if (result.valid) {
|
|
261
|
-
return result;
|
|
262
|
-
}
|
|
263
|
-
errorResults.push(result);
|
|
264
|
-
}
|
|
265
|
-
if (errorResults.length == 1) {
|
|
266
|
-
return errorResults[0];
|
|
267
|
-
}
|
|
268
|
-
const errors = errorResults.map((result) => result.error);
|
|
269
|
-
const expectStrings = [];
|
|
270
|
-
for (const schema of validSchemas) {
|
|
271
|
-
expectStrings.push(getExpectString(schema));
|
|
272
|
-
}
|
|
273
|
-
const expectString = expectStrings.length > 1 ? `(${expectStrings.join(' | ')})` : expectStrings[0];
|
|
274
|
-
return { valid: false, error: SchemaError.expectedButGot(expectString, getValueType(value), path, { inner: errors, fast: options.fastErrors }) };
|
|
275
|
-
}
|
|
276
|
-
export function getExpectString(schema) {
|
|
277
|
-
const expectedNames = getSchemaTypeNames(schema);
|
|
278
|
-
const arraySuffix = (isValueSchema(schema) && (schema.array == true)) ? '[]' : '';
|
|
279
|
-
const expectedTypeString = (expectedNames.length == 1) ? `${expectedNames[0]}${arraySuffix}` : `(${expectedNames.join(' | ')})${arraySuffix}`;
|
|
280
|
-
const expects = isValueSchema(schema) ? toArray(schema.valueConstraints ?? []).map((constraint) => constraint.expects) : [];
|
|
281
|
-
const expectsString = (expects.length > 0) ? `[${expects.join(', ')}]` : '';
|
|
282
|
-
return `${expectedTypeString}${expectsString}`;
|
|
283
81
|
}
|
package/schema/schemas/any.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
2
|
+
import { Schema, type SchemaTestResult } from '../schema.js';
|
|
3
|
+
export declare class AnySchema extends Schema<any> {
|
|
4
|
+
_test(value: any): SchemaTestResult<any>;
|
|
5
|
+
}
|
|
6
|
+
export declare function any(): AnySchema;
|
|
7
|
+
export declare function Any(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
package/schema/schemas/any.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Property } from '../decorators/index.js';
|
|
2
|
+
import { Schema } from '../schema.js';
|
|
3
|
+
export class AnySchema extends Schema {
|
|
4
|
+
_test(value) {
|
|
5
|
+
return { valid: true, value };
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function any() {
|
|
9
|
+
return new AnySchema();
|
|
6
10
|
}
|
|
7
11
|
export function Any(options) {
|
|
8
|
-
return
|
|
12
|
+
return Property(any(), options);
|
|
9
13
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type
|
|
4
|
-
import type
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export declare function
|
|
12
|
-
export declare function Array(innerValues: OneOrMany<SchemaTestable>, options?: ArrayOptions): Decorator<'property' | 'accessor'>;
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import type { TypedOmit } from '../../types.js';
|
|
3
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
4
|
+
import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
5
|
+
export declare class ArraySchema<T> extends Schema<T[]> {
|
|
6
|
+
readonly itemSchema: Schema<T>;
|
|
7
|
+
constructor(itemSchema: SchemaTestable<T>);
|
|
8
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T[]>;
|
|
9
|
+
}
|
|
10
|
+
export declare function array<T>(schema: SchemaTestable<T>): ArraySchema<T>;
|
|
11
|
+
export declare function Array(schema: SchemaTestable, options?: TypedOmit<SchemaPropertyDecoratorOptions, 'array'>): SchemaPropertyDecorator;
|
package/schema/schemas/array.js
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { SchemaError } from '../../schema/schema.error.js';
|
|
2
|
+
import { isArray } from '../../utils/type-guards.js';
|
|
3
|
+
import { typeOf } from '../../utils/type-of.js';
|
|
4
|
+
import { Property } from '../decorators/index.js';
|
|
5
|
+
import { Schema } from '../schema.js';
|
|
6
|
+
import { schemaTestableToSchema } from '../testable.js';
|
|
7
|
+
export class ArraySchema extends Schema {
|
|
8
|
+
itemSchema;
|
|
9
|
+
constructor(itemSchema) {
|
|
10
|
+
super();
|
|
11
|
+
this.itemSchema = schemaTestableToSchema(itemSchema);
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
_test(value, path, options) {
|
|
14
|
+
if (!isArray(value)) {
|
|
15
|
+
return { valid: false, error: SchemaError.expectedButGot('array', typeOf(value), path) };
|
|
16
|
+
}
|
|
17
|
+
const values = [];
|
|
18
|
+
for (let i = 0; i < value.length; i++) {
|
|
19
|
+
const result = this.itemSchema._test(value[i], path.add(i), options);
|
|
20
|
+
if (!result.valid) {
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
values.push(result.value);
|
|
24
|
+
}
|
|
25
|
+
return { valid: true, value: values };
|
|
14
26
|
}
|
|
15
|
-
return valueSchema(innerValues, {
|
|
16
|
-
...options,
|
|
17
|
-
array: true,
|
|
18
|
-
arrayConstraints
|
|
19
|
-
});
|
|
20
27
|
}
|
|
21
|
-
export function
|
|
22
|
-
return
|
|
28
|
+
export function array(schema) {
|
|
29
|
+
return new ArraySchema(schema);
|
|
30
|
+
}
|
|
31
|
+
export function Array(schema, options) {
|
|
32
|
+
return Property(schema, { ...options, array: true });
|
|
23
33
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import { type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
3
|
+
import type { SchemaPropertyDecorator } from '../decorators/types.js';
|
|
4
|
+
import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
5
|
+
export declare class BigIntSchema extends Schema<bigint> {
|
|
6
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<bigint>;
|
|
7
|
+
}
|
|
8
|
+
export declare function bigint(): BigIntSchema;
|
|
9
|
+
export declare function BigIntProperty(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { SchemaError } from '../../schema/schema.error.js';
|
|
2
|
+
import { isBigInt } from '../../utils/type-guards.js';
|
|
3
|
+
import { typeOf } from '../../utils/type-of.js';
|
|
4
|
+
import { Property } from '../decorators/index.js';
|
|
5
|
+
import { Schema } from '../schema.js';
|
|
6
|
+
export class BigIntSchema extends Schema {
|
|
7
|
+
_test(value, path, options) {
|
|
8
|
+
if (isBigInt(value)) {
|
|
9
|
+
return { valid: true, value };
|
|
10
|
+
}
|
|
11
|
+
return { valid: false, error: SchemaError.expectedButGot('bigint', typeOf(value), path, { fast: options.fastErrors }) };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function bigint() {
|
|
15
|
+
return new BigIntSchema();
|
|
16
|
+
}
|
|
17
|
+
export function BigIntProperty(options) {
|
|
18
|
+
return Property(bigint(), options);
|
|
19
|
+
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
3
|
+
import type { SchemaTestOptions, SchemaTestResult } from '../schema.js';
|
|
4
|
+
import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
|
|
5
|
+
export type BooleanSchemaOptions = SimpleSchemaOptions;
|
|
6
|
+
export declare class BooleanSchema extends SimpleSchema<boolean> {
|
|
7
|
+
constructor(options?: BooleanSchemaOptions);
|
|
8
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<boolean>;
|
|
9
|
+
}
|
|
10
|
+
export declare function boolean(options?: BooleanSchemaOptions): BooleanSchema;
|
|
11
|
+
export declare function BooleanProperty(options?: BooleanSchemaOptions & SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -1,9 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { SchemaError } from '../../schema/schema.error.js';
|
|
2
|
+
import { isBoolean, isString } from '../../utils/type-guards.js';
|
|
3
|
+
import { typeOf } from '../../utils/type-of.js';
|
|
4
|
+
import { Property } from '../decorators/index.js';
|
|
5
|
+
import { SimpleSchema } from './simple.js';
|
|
6
|
+
export class BooleanSchema extends SimpleSchema {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
super('boolean', isBoolean, options, {
|
|
9
|
+
coercers: {
|
|
10
|
+
all: (value) => {
|
|
11
|
+
const normalizedValue = isString(value) ? value.toLowerCase().trim() : value;
|
|
12
|
+
switch (normalizedValue) {
|
|
13
|
+
case 1:
|
|
14
|
+
case 1n:
|
|
15
|
+
case 'true':
|
|
16
|
+
case '1':
|
|
17
|
+
case 'yes':
|
|
18
|
+
return { success: true, value: true, valid: true };
|
|
19
|
+
case 0:
|
|
20
|
+
case 0n:
|
|
21
|
+
case 'false':
|
|
22
|
+
case '0':
|
|
23
|
+
case 'no':
|
|
24
|
+
return { success: true, value: false, valid: true };
|
|
25
|
+
default:
|
|
26
|
+
return { success: false };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
_test(value, path, options) {
|
|
33
|
+
if (isBoolean(value)) {
|
|
34
|
+
return { valid: true, value };
|
|
35
|
+
}
|
|
36
|
+
return { valid: false, error: SchemaError.expectedButGot('boolean', typeOf(value), path, { fast: options.fastErrors }) };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function boolean(options) {
|
|
40
|
+
return new BooleanSchema(options);
|
|
6
41
|
}
|
|
7
42
|
export function BooleanProperty(options) {
|
|
8
|
-
return
|
|
43
|
+
return Property(boolean(options), options);
|
|
9
44
|
}
|