@tstdl/base 0.90.92 → 0.91.0-beta1
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 +21 -22
- 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 +3 -3
- 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 +83 -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 +6 -4
- 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 +3 -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 +10 -12
- package/schema/schemas/array.js +27 -19
- package/schema/schemas/bigint.d.ts +8 -0
- package/schema/schemas/bigint.js +19 -0
- package/schema/schemas/boolean.d.ts +15 -5
- package/schema/schemas/boolean.js +47 -6
- package/schema/schemas/date.d.ts +9 -7
- package/schema/schemas/date.js +25 -19
- 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/index.d.ts +4 -6
- package/schema/schemas/index.js +4 -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 +10 -6
- package/schema/schemas/nullable.js +24 -7
- package/schema/schemas/number.d.ts +13 -7
- package/schema/schemas/number.js +34 -23
- package/schema/schemas/object.d.ts +75 -7
- package/schema/schemas/object.js +171 -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 +10 -7
- package/schema/schemas/optional.js +21 -3
- package/schema/schemas/readable-stream.d.ts +8 -6
- package/schema/schemas/readable-stream.js +11 -6
- package/schema/schemas/regexp.d.ts +8 -6
- package/schema/schemas/regexp.js +25 -7
- package/schema/schemas/simple.d.ts +27 -0
- package/schema/schemas/simple.js +57 -0
- package/schema/schemas/string.d.ts +13 -21
- package/schema/schemas/string.js +26 -34
- package/schema/schemas/symbol.d.ts +12 -0
- package/schema/schemas/symbol.js +25 -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 +30 -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/schemas/object.js
CHANGED
|
@@ -1,12 +1,177 @@
|
|
|
1
|
-
/* eslint-disable
|
|
2
|
-
import {
|
|
1
|
+
/* eslint-disable max-depth */
|
|
2
|
+
import { reflectionRegistry } from '../../reflection/index.js';
|
|
3
|
+
import { SchemaError } from '../../schema/schema.error.js';
|
|
4
|
+
import { toArray } from '../../utils/array/array.js';
|
|
5
|
+
import { memoizeSingle } from '../../utils/function/memoize.js';
|
|
6
|
+
import { filterObject, mapObjectValues, objectKeys } from '../../utils/object/object.js';
|
|
7
|
+
import { assert, isDefined, isFunction, isLiteralObject, isNotNull, isNotNullOrUndefined, isNull, isObject, isUndefined } from '../../utils/type-guards.js';
|
|
8
|
+
import { typeOf } from '../../utils/type-of.js';
|
|
9
|
+
import { Schema } from '../schema.js';
|
|
10
|
+
import { schemaTestableToSchema } from '../testable.js';
|
|
11
|
+
import { array } from './array.js';
|
|
12
|
+
import { nullable } from './nullable.js';
|
|
13
|
+
import { optional } from './optional.js';
|
|
14
|
+
export const tryGetSchemaFromReflection = memoizeSingle(_tryGetSchemaFromReflection, { weak: true });
|
|
15
|
+
export class ObjectSchema extends Schema {
|
|
16
|
+
propertyKeys;
|
|
17
|
+
allowUnknownProperties;
|
|
18
|
+
properties;
|
|
19
|
+
mask;
|
|
20
|
+
unknownProperties;
|
|
21
|
+
unknownPropertiesKey;
|
|
22
|
+
factory;
|
|
23
|
+
constructor(properties, options = {}) {
|
|
24
|
+
super();
|
|
25
|
+
this.properties = mapObjectValues(properties, (value) => schemaTestableToSchema(value));
|
|
26
|
+
this.mask = options.mask ?? null;
|
|
27
|
+
this.unknownProperties = isNotNullOrUndefined(options.unknownProperties) ? schemaTestableToSchema(options.unknownProperties) : null;
|
|
28
|
+
this.unknownPropertiesKey = isNotNullOrUndefined(options.unknownPropertiesKey) ? schemaTestableToSchema(options.unknownPropertiesKey) : null;
|
|
29
|
+
this.factory = options.factory ?? null;
|
|
30
|
+
this.allowUnknownProperties = isNotNull(this.unknownProperties) || isNotNull(this.unknownPropertiesKey);
|
|
31
|
+
this.propertyKeys = new Set(objectKeys(properties));
|
|
32
|
+
}
|
|
33
|
+
_test(value, path, options) {
|
|
34
|
+
if (!isObject(value)) {
|
|
35
|
+
return { valid: false, error: SchemaError.expectedButGot('object', typeOf(value), path) };
|
|
36
|
+
}
|
|
37
|
+
const mask = this.mask ?? options.mask ?? false;
|
|
38
|
+
const resultValue = isLiteralObject(this.factory) ? new this.factory.type() : {};
|
|
39
|
+
const valueKeys = new Set(objectKeys(value));
|
|
40
|
+
const unknownValuePropertyKeys = valueKeys.difference(this.propertyKeys);
|
|
41
|
+
if ((unknownValuePropertyKeys.size > 0) && !mask && !this.allowUnknownProperties) {
|
|
42
|
+
return { valid: false, error: new SchemaError('Unexpected property', { path: path.add(unknownValuePropertyKeys.values().next().value), fast: options.fastErrors }) };
|
|
43
|
+
}
|
|
44
|
+
for (const key of this.propertyKeys) {
|
|
45
|
+
const propertyResult = this.properties[key]._test(value[key], path.add(key), options);
|
|
46
|
+
if (!propertyResult.valid) {
|
|
47
|
+
return propertyResult;
|
|
48
|
+
}
|
|
49
|
+
resultValue[key] = propertyResult.value;
|
|
50
|
+
}
|
|
51
|
+
if (this.allowUnknownProperties) {
|
|
52
|
+
for (const key of unknownValuePropertyKeys) {
|
|
53
|
+
const propertyPath = path.add(key);
|
|
54
|
+
const keyResult = this.unknownPropertiesKey?._test(key, propertyPath, options) ?? { valid: true };
|
|
55
|
+
if (!keyResult.valid) {
|
|
56
|
+
if (mask) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
return { valid: false, error: new SchemaError('Invalid property key.', { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
|
|
60
|
+
}
|
|
61
|
+
const innerValueResult = this.unknownProperties?._test(value[key], propertyPath, options) ?? { valid: true, value: value[key] };
|
|
62
|
+
if (!innerValueResult.valid) {
|
|
63
|
+
return innerValueResult;
|
|
64
|
+
}
|
|
65
|
+
resultValue[key] = innerValueResult.value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const testResultValue = isFunction(this.factory) ? this.factory(resultValue) : resultValue;
|
|
69
|
+
return { valid: true, value: testResultValue };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function object(properties, options) {
|
|
73
|
+
return new ObjectSchema(properties, options);
|
|
74
|
+
}
|
|
3
75
|
export function explicitObject(properties, options) {
|
|
4
76
|
return object(properties, options);
|
|
5
77
|
}
|
|
6
|
-
export function
|
|
7
|
-
return
|
|
8
|
-
|
|
9
|
-
|
|
78
|
+
export function record(key, value, options) {
|
|
79
|
+
return object({}, { ...options, unknownPropertiesKey: key, unknownProperties: value });
|
|
80
|
+
}
|
|
81
|
+
export function assign(...schemasOrTypes) {
|
|
82
|
+
const schemas = schemasOrTypes.map((schemaOrType) => {
|
|
83
|
+
if (schemaOrType instanceof ObjectSchema) {
|
|
84
|
+
return schemaOrType;
|
|
85
|
+
}
|
|
86
|
+
const typeSchema = getSchemaFromReflection(schemaOrType);
|
|
87
|
+
assert(typeSchema instanceof ObjectSchema, 'assign() only works with object schemas.');
|
|
88
|
+
return typeSchema;
|
|
89
|
+
});
|
|
90
|
+
return object(schemas.reduce((result, schema) => ({ ...result, ...schema.properties }), {}), {
|
|
91
|
+
mask: schemas.findLast((schema) => isNotNull(schema.mask))?.mask,
|
|
92
|
+
unknownProperties: schemas.findLast((schema) => isNotNull(schema.unknownProperties))?.unknownProperties,
|
|
93
|
+
unknownPropertiesKey: schemas.findLast((schema) => isNotNull(schema.unknownPropertiesKey))?.unknownPropertiesKey
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export function partial(schema, keyOrKeys) {
|
|
97
|
+
const keys = isUndefined(keyOrKeys) ? undefined : toArray(keyOrKeys);
|
|
98
|
+
const mapper = isUndefined(keys)
|
|
99
|
+
? (propertySchema) => optional(propertySchema)
|
|
100
|
+
: (propertySchema, key) => keys.includes(key) ? optional(propertySchema) : propertySchema;
|
|
101
|
+
return object(mapObjectValues(schema.properties, mapper), {
|
|
102
|
+
mask: schema.mask,
|
|
103
|
+
unknownProperties: schema.unknownProperties,
|
|
104
|
+
unknownPropertiesKey: schema.unknownPropertiesKey
|
|
10
105
|
});
|
|
11
106
|
}
|
|
107
|
+
export function pick(schema, keys) {
|
|
108
|
+
const keyArray = toArray(keys);
|
|
109
|
+
return object(filterObject(schema.properties, (_, key) => keyArray.includes(key)), {
|
|
110
|
+
mask: schema.mask,
|
|
111
|
+
unknownProperties: schema.unknownProperties,
|
|
112
|
+
unknownPropertiesKey: schema.unknownPropertiesKey
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
export function omit(schema, keys) {
|
|
116
|
+
const keysArray = toArray(keys);
|
|
117
|
+
return object(filterObject(schema.properties, (_, key) => !keysArray.includes(key)), {
|
|
118
|
+
mask: schema.mask,
|
|
119
|
+
unknownProperties: schema.unknownProperties,
|
|
120
|
+
unknownPropertiesKey: schema.unknownPropertiesKey
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
export function getSchemaFromReflection(type) {
|
|
124
|
+
const schema = tryGetSchemaFromReflection(type);
|
|
125
|
+
if (isNull(schema)) {
|
|
126
|
+
throw new Error(`Could not get schema for ${type.name} from reflection data.`);
|
|
127
|
+
}
|
|
128
|
+
return schema;
|
|
129
|
+
}
|
|
130
|
+
function _tryGetSchemaFromReflection(type) {
|
|
131
|
+
const metadata = reflectionRegistry.getMetadata(type);
|
|
132
|
+
if (isUndefined(metadata)) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
const typeData = metadata.data.tryGet('schema') ?? {};
|
|
136
|
+
if (typeData.schema instanceof Schema) {
|
|
137
|
+
return typeData.schema;
|
|
138
|
+
}
|
|
139
|
+
if (isFunction(typeData.schema)) {
|
|
140
|
+
return schemaTestableToSchema(typeData.schema);
|
|
141
|
+
}
|
|
142
|
+
const schema = object(getObjectSchemaPropertiesFromReflection(metadata, type), {
|
|
143
|
+
factory: isDefined(typeData.factory) ? typeData.factory : { type: type },
|
|
144
|
+
mask: typeData.mask,
|
|
145
|
+
unknownProperties: typeData.unknownProperties,
|
|
146
|
+
unknownPropertiesKey: typeData.unknownPropertiesKey
|
|
147
|
+
});
|
|
148
|
+
const prototype = Reflect.getPrototypeOf(type);
|
|
149
|
+
if (isNotNull(prototype) && reflectionRegistry.hasType(prototype)) {
|
|
150
|
+
const parentSchema = getSchemaFromReflection(prototype);
|
|
151
|
+
assert(parentSchema instanceof ObjectSchema, 'Can not infer an ObjectSchema from reflection when parent class has an explicit non-object schema defined.');
|
|
152
|
+
return assign(parentSchema, schema);
|
|
153
|
+
}
|
|
154
|
+
return schema;
|
|
155
|
+
}
|
|
156
|
+
function getObjectSchemaPropertiesFromReflection(metadata, type) {
|
|
157
|
+
const properties = {};
|
|
158
|
+
for (const [key, propertyMetadata] of metadata.properties) {
|
|
159
|
+
const reflectionData = propertyMetadata.data.tryGet('schema');
|
|
160
|
+
if (isUndefined(reflectionData?.schema) && (propertyMetadata.type == Object)) {
|
|
161
|
+
throw new Error(`Schema of property "${String(key)}" on type ${type.name} is inferred as Object. This is most likely unwanted and happens when the property is defined as partial or the type is an union. Use an explicit @Property(Object) if wanted.`);
|
|
162
|
+
}
|
|
163
|
+
let propertySchema = reflectionData?.schema ?? propertyMetadata.type;
|
|
164
|
+
if (reflectionData?.array == true) {
|
|
165
|
+
propertySchema = array(propertySchema);
|
|
166
|
+
}
|
|
167
|
+
if (reflectionData?.nullable == true) {
|
|
168
|
+
propertySchema = nullable(propertySchema);
|
|
169
|
+
}
|
|
170
|
+
if (reflectionData?.optional == true) {
|
|
171
|
+
propertySchema = optional(propertySchema);
|
|
172
|
+
}
|
|
173
|
+
properties[key] = propertySchema;
|
|
174
|
+
}
|
|
175
|
+
return properties;
|
|
176
|
+
}
|
|
12
177
|
export const emptyObjectSchema = explicitObject({});
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
2
|
import type { OneOrMany as OneOrManyType } from '../../types.js';
|
|
3
|
-
import type
|
|
4
|
-
import type
|
|
5
|
-
import type { ArrayOptions } from './array.js';
|
|
3
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
4
|
+
import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
6
5
|
export type OneOrMany<T> = OneOrManyType<T>;
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
export declare class OneOrManySchema<T> extends Schema<T | T[]> {
|
|
7
|
+
readonly schema: Schema<T | T[]>;
|
|
8
|
+
constructor(schema: SchemaTestable<T>);
|
|
9
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T | T[]>;
|
|
10
|
+
}
|
|
11
|
+
export declare function oneOrMany<T>(schema: SchemaTestable<T>): OneOrManySchema<T>;
|
|
12
|
+
export declare function OneOrMany(schema: SchemaTestable, options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Property } from '../decorators/index.js';
|
|
2
|
+
import { Schema } from '../schema.js';
|
|
3
|
+
import { schemaTestableToSchema } from '../testable.js';
|
|
4
4
|
import { array } from './array.js';
|
|
5
5
|
import { union } from './union.js';
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
export class OneOrManySchema extends Schema {
|
|
7
|
+
schema;
|
|
8
|
+
constructor(schema) {
|
|
9
|
+
super();
|
|
10
|
+
const oneSchema = schemaTestableToSchema(schema);
|
|
11
|
+
this.schema = union(oneSchema, array(oneSchema));
|
|
12
|
+
}
|
|
13
|
+
_test(value, path, options) {
|
|
14
|
+
return this.schema._test(value, path, options);
|
|
15
|
+
}
|
|
11
16
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
export function oneOrMany(schema) {
|
|
18
|
+
return new OneOrManySchema(schema);
|
|
19
|
+
}
|
|
20
|
+
export function OneOrMany(schema, options) {
|
|
21
|
+
return Property(oneOrMany(schema), options);
|
|
15
22
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import type { SchemaPropertyDecorator } from '../decorators/types.js';
|
|
3
|
+
import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
4
|
+
export declare class OptionalSchema<T> extends Schema<T | undefined> {
|
|
5
|
+
readonly schema: Schema<T>;
|
|
6
|
+
constructor(schema: SchemaTestable<T>);
|
|
7
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T | undefined>;
|
|
8
|
+
}
|
|
9
|
+
export declare function optional<T>(schema: SchemaTestable<T>): OptionalSchema<T>;
|
|
10
|
+
export declare function Optional(schema?: SchemaTestable): SchemaPropertyDecorator;
|
|
@@ -1,7 +1,25 @@
|
|
|
1
|
+
import { isUndefined } from '../../utils/type-guards.js';
|
|
1
2
|
import { createSchemaPropertyDecorator } from '../decorators/utils.js';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { Schema } from '../schema.js';
|
|
4
|
+
import { schemaTestableToSchema } from '../testable.js';
|
|
5
|
+
export class OptionalSchema extends Schema {
|
|
6
|
+
schema;
|
|
7
|
+
constructor(schema) {
|
|
8
|
+
if ((schema instanceof OptionalSchema) && (schema == schema.schema)) {
|
|
9
|
+
return schema; // eslint-disable-line no-constructor-return
|
|
10
|
+
}
|
|
11
|
+
super();
|
|
12
|
+
this.schema = schemaTestableToSchema(schema);
|
|
13
|
+
}
|
|
14
|
+
_test(value, path, options) {
|
|
15
|
+
if (isUndefined(value)) {
|
|
16
|
+
return { valid: true, value: undefined };
|
|
17
|
+
}
|
|
18
|
+
return this.schema._test(value, path, options);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function optional(schema) {
|
|
22
|
+
return new OptionalSchema(schema);
|
|
5
23
|
}
|
|
6
24
|
export function Optional(schema) {
|
|
7
25
|
return createSchemaPropertyDecorator({ schema, optional: true });
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
2
|
+
import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
|
|
3
|
+
export type ReadableStreamSchemaOptions = SimpleSchemaOptions;
|
|
4
|
+
export declare class ReadableStreamSchema extends SimpleSchema<ReadableStream> {
|
|
5
|
+
constructor(options?: ReadableStreamSchemaOptions);
|
|
6
|
+
}
|
|
7
|
+
export declare function readableStream(options?: ReadableStreamSchemaOptions): ReadableStreamSchema;
|
|
8
|
+
export declare function ReadableStream(options?: SchemaPropertyDecoratorOptions & ReadableStreamSchemaOptions): SchemaPropertyDecorator;
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { isReadableStream } from '../../utils/type-guards.js';
|
|
2
|
+
import { Property } from '../decorators/index.js';
|
|
3
|
+
import { SimpleSchema } from './simple.js';
|
|
4
|
+
export class ReadableStreamSchema extends SimpleSchema {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super('ReadableStream', isReadableStream, options);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
4
9
|
export function readableStream(options) {
|
|
5
|
-
return
|
|
10
|
+
return new ReadableStreamSchema(options);
|
|
6
11
|
}
|
|
7
|
-
export function
|
|
8
|
-
return
|
|
12
|
+
export function ReadableStream(options) {
|
|
13
|
+
return Property(readableStream(options), options);
|
|
9
14
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
2
|
+
import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
|
|
3
|
+
export type RegExpSchemaOptions = SimpleSchemaOptions;
|
|
4
|
+
export declare class RegExpSchema extends SimpleSchema<RegExp> {
|
|
5
|
+
constructor(options?: RegExpSchemaOptions);
|
|
6
|
+
}
|
|
7
|
+
export declare function regExp(options?: RegExpSchemaOptions): RegExpSchema;
|
|
8
|
+
export declare function RegExp(options?: SchemaPropertyDecoratorOptions & RegExpSchemaOptions): SchemaPropertyDecorator;
|
package/schema/schemas/regexp.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { isRegExp } from '../../utils/type-guards.js';
|
|
2
|
+
import { Property } from '../decorators/index.js';
|
|
3
|
+
import { SchemaError } from '../schema.error.js';
|
|
4
|
+
import { SimpleSchema } from './simple.js';
|
|
5
|
+
export class RegExpSchema extends SimpleSchema {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super('RegExp', isRegExp, options, {
|
|
8
|
+
coercers: {
|
|
9
|
+
string: (value, path, options) => {
|
|
10
|
+
try {
|
|
11
|
+
const regex = globalThis.RegExp(value, 'u');
|
|
12
|
+
return ({ success: true, value: regex, valid: true });
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return { success: false, error: SchemaError.couldNotCoerce(globalThis.RegExp, 'string', path, { fast: options.fastErrors, customMessage: error.message }) };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
6
21
|
}
|
|
7
|
-
export function
|
|
8
|
-
return
|
|
22
|
+
export function regExp(options) {
|
|
23
|
+
return new RegExpSchema(options);
|
|
24
|
+
}
|
|
25
|
+
export function RegExp(options) {
|
|
26
|
+
return Property(regExp(options), options);
|
|
9
27
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import type { AbstractConstructor, OneOrMany } from '../../types.js';
|
|
3
|
+
import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
4
|
+
import type { CoerceResult, Coercible, ConstraintResult } from '../types.js';
|
|
5
|
+
export type SimpleSchemaOptions = Coercible;
|
|
6
|
+
type SimpleSchemaRefinements<T> = {
|
|
7
|
+
coercers?: SimpleSchemaCoercers<T>;
|
|
8
|
+
constraints?: (SimpleSchemaConstraint<T> | null)[];
|
|
9
|
+
gotValueFormatter?: SimpleSchemaGotValueFormatter;
|
|
10
|
+
};
|
|
11
|
+
export type SimpleSchemaCoercers<T> = {
|
|
12
|
+
string?: (value: string, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
13
|
+
number?: (value: number, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
14
|
+
boolean?: (value: boolean, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
15
|
+
bigint?: (value: bigint, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
16
|
+
undefined?: (value: undefined, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
17
|
+
null?: (value: null, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
18
|
+
all?: (value: unknown, path: JsonPath, options: SchemaTestOptions) => CoerceResult<T>;
|
|
19
|
+
};
|
|
20
|
+
export type SimpleSchemaConstraint<T> = (value: T) => ConstraintResult;
|
|
21
|
+
export type SimpleSchemaGotValueFormatter = (value: unknown) => string;
|
|
22
|
+
export declare abstract class SimpleSchema<T> extends Schema<T> {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(expected: OneOrMany<string | AbstractConstructor>, guardFn: (value: any) => value is T, options?: SimpleSchemaOptions, refinements?: SimpleSchemaRefinements<T>);
|
|
25
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T>;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isDefined, isError, isNotNull, isNull, isUndefined } from '../../utils/type-guards.js';
|
|
2
|
+
import { typeOf } from '../../utils/type-of.js';
|
|
3
|
+
import { SchemaError } from '../schema.error.js';
|
|
4
|
+
import { Schema } from '../schema.js';
|
|
5
|
+
export class SimpleSchema extends Schema {
|
|
6
|
+
#guardFn;
|
|
7
|
+
#expected;
|
|
8
|
+
#options;
|
|
9
|
+
#coercers;
|
|
10
|
+
#constraints;
|
|
11
|
+
#gotValueFormatter;
|
|
12
|
+
constructor(expected, guardFn, options = {}, refinements = {}) {
|
|
13
|
+
super();
|
|
14
|
+
this.#expected = expected;
|
|
15
|
+
this.#guardFn = guardFn;
|
|
16
|
+
this.#options = options;
|
|
17
|
+
this.#coercers = refinements.coercers ?? {};
|
|
18
|
+
this.#constraints = refinements.constraints?.filter(isNotNull) ?? [];
|
|
19
|
+
this.#gotValueFormatter = refinements.gotValueFormatter ?? typeOf;
|
|
20
|
+
}
|
|
21
|
+
_test(value, path, options) {
|
|
22
|
+
let result;
|
|
23
|
+
if (this.#guardFn(value)) {
|
|
24
|
+
result = { valid: true, value };
|
|
25
|
+
}
|
|
26
|
+
else if ((this.#options.coerce ?? options.coerce) == true) {
|
|
27
|
+
const type = typeof value;
|
|
28
|
+
const coerceType = isNull(value) ? 'null' : type;
|
|
29
|
+
const coerceFn = (this.#coercers[coerceType] ?? this.#coercers.all);
|
|
30
|
+
if (isDefined(coerceFn)) {
|
|
31
|
+
const coerceResult = coerceFn(value, path, options);
|
|
32
|
+
if (coerceResult.success) {
|
|
33
|
+
if (!coerceResult.valid) {
|
|
34
|
+
return this._test(coerceResult.value, path, options);
|
|
35
|
+
}
|
|
36
|
+
result = { valid: true, value: coerceResult.value };
|
|
37
|
+
}
|
|
38
|
+
else if (isDefined(coerceResult.error)) {
|
|
39
|
+
result = { valid: false, error: coerceResult.error };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (isUndefined(result)) {
|
|
44
|
+
return { valid: false, error: SchemaError.expectedButGot(this.#expected, this.#gotValueFormatter(value), path, { fast: options.fastErrors }) };
|
|
45
|
+
}
|
|
46
|
+
for (const constraint of this.#constraints) {
|
|
47
|
+
const constraintResult = constraint(result.value);
|
|
48
|
+
if (!constraintResult.success) {
|
|
49
|
+
if (isError(constraintResult.error)) {
|
|
50
|
+
return { valid: false, error: constraintResult.error };
|
|
51
|
+
}
|
|
52
|
+
return { valid: false, error: new SchemaError(`Constraint validation failed: ${constraintResult.error}`, { path, fast: options.fastErrors }) };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
trim?: boolean;
|
|
6
|
-
/** lowercase */
|
|
7
|
-
lowercase?: boolean;
|
|
8
|
-
/** uppercase */
|
|
9
|
-
uppercase?: boolean;
|
|
10
|
-
/** minimum length */
|
|
11
|
-
minimumLength?: number;
|
|
12
|
-
/** maximum length */
|
|
13
|
-
maximumLength?: number;
|
|
14
|
-
/** regular expression */
|
|
15
|
-
pattern?: string | RegExp;
|
|
16
|
-
/** regular expression flags */
|
|
17
|
-
patternFlags?: string;
|
|
18
|
-
/** name for errors */
|
|
19
|
-
patternName?: string;
|
|
1
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
2
|
+
import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
|
|
3
|
+
export type StringSchemaOptions = SimpleSchemaOptions & {
|
|
4
|
+
pattern?: RegExp | string;
|
|
20
5
|
};
|
|
21
|
-
export declare
|
|
22
|
-
|
|
6
|
+
export declare class StringSchema extends SimpleSchema<string> {
|
|
7
|
+
constructor(options?: StringSchemaOptions);
|
|
8
|
+
}
|
|
9
|
+
export declare function string(options?: StringSchemaOptions): StringSchema;
|
|
10
|
+
export declare function String(options?: SchemaPropertyDecoratorOptions & StringSchemaOptions): SchemaPropertyDecorator;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated use {@link String()} instead
|
|
13
|
+
*/
|
|
14
|
+
export declare function StringProperty(options?: SchemaPropertyDecoratorOptions & StringSchemaOptions): SchemaPropertyDecorator;
|
package/schema/schemas/string.js
CHANGED
|
@@ -1,38 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { isDefined, isRegExp, isString } from '../../utils/type-guards.js';
|
|
2
|
+
import { Property } from '../decorators/index.js';
|
|
3
|
+
import { SimpleSchema } from './simple.js';
|
|
4
|
+
export class StringSchema extends SimpleSchema {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const pattern = isDefined(options?.pattern) ? isString(options.pattern) ? RegExp(options.pattern, 'u') : isRegExp(options.pattern) ? options.pattern : undefined : undefined;
|
|
7
|
+
super('string', isString, options, {
|
|
8
|
+
coercers: {
|
|
9
|
+
number: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
|
|
10
|
+
boolean: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
|
|
11
|
+
bigint: (value) => ({ success: true, value: globalThis.String(value), valid: true })
|
|
12
|
+
},
|
|
13
|
+
constraints: [
|
|
14
|
+
isDefined(pattern) ? ((value) => pattern.test(value) ? ({ success: true }) : ({ success: false, error: 'Value did not match pattern.' })) : null
|
|
15
|
+
]
|
|
16
|
+
});
|
|
13
17
|
}
|
|
14
|
-
if (isDefined(options.maximumLength)) {
|
|
15
|
-
valueConstraints.push(new MaximumLengthConstraint(options.maximumLength));
|
|
16
|
-
}
|
|
17
|
-
if (isDefined(options.pattern)) {
|
|
18
|
-
const pattern = RegExp(options.pattern, options.patternFlags);
|
|
19
|
-
valueConstraints.push(new PatternConstraint(pattern, options.patternName));
|
|
20
|
-
}
|
|
21
|
-
if (isDefined(options.trim)) {
|
|
22
|
-
transformers.push(new TrimTransformer());
|
|
23
|
-
}
|
|
24
|
-
if (isDefined(options.lowercase)) {
|
|
25
|
-
transformers.push(new LowercaseTransformer());
|
|
26
|
-
}
|
|
27
|
-
if (isDefined(options.uppercase)) {
|
|
28
|
-
transformers.push(new UppercaseTransformer());
|
|
29
|
-
}
|
|
30
|
-
return valueSchema(String, {
|
|
31
|
-
...options,
|
|
32
|
-
valueConstraints,
|
|
33
|
-
transformers
|
|
34
|
-
});
|
|
35
18
|
}
|
|
19
|
+
export function string(options) {
|
|
20
|
+
return new StringSchema(options);
|
|
21
|
+
}
|
|
22
|
+
export function String(options) {
|
|
23
|
+
return Property(string(options), options);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated use {@link String()} instead
|
|
27
|
+
*/
|
|
36
28
|
export function StringProperty(options) {
|
|
37
|
-
return
|
|
29
|
+
return String(options);
|
|
38
30
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import { type SchemaPropertyDecorator } from '../decorators/index.js';
|
|
3
|
+
import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
4
|
+
export declare class SymbolSchema extends Schema<symbol> {
|
|
5
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<symbol>;
|
|
6
|
+
}
|
|
7
|
+
export declare function symbol(): SymbolSchema;
|
|
8
|
+
export declare function Symbol(): SchemaPropertyDecorator;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated use {@link Symbol()} instead
|
|
11
|
+
*/
|
|
12
|
+
export declare function SymbolProperty(): SchemaPropertyDecorator;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SchemaError } from '../../schema/schema.error.js';
|
|
2
|
+
import { isSymbol } 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 SymbolSchema extends Schema {
|
|
7
|
+
_test(value, path, options) {
|
|
8
|
+
if (isSymbol(value)) {
|
|
9
|
+
return { valid: true, value };
|
|
10
|
+
}
|
|
11
|
+
return { valid: false, error: SchemaError.expectedButGot('symbol', typeOf(value), path, { fast: options.fastErrors }) };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function symbol() {
|
|
15
|
+
return new SymbolSchema();
|
|
16
|
+
}
|
|
17
|
+
export function Symbol() {
|
|
18
|
+
return Property(symbol());
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated use {@link Symbol()} instead
|
|
22
|
+
*/
|
|
23
|
+
export function SymbolProperty() {
|
|
24
|
+
return Symbol();
|
|
25
|
+
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
3
|
+
export declare class TransformSchema<I, O> extends Schema<O> {
|
|
4
|
+
readonly schema: Schema<I>;
|
|
5
|
+
readonly transformFn: (value: I) => O;
|
|
6
|
+
constructor(schema: Schema<I>, transformFn: (value: I) => O);
|
|
7
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<O>;
|
|
8
|
+
}
|
|
9
|
+
export declare function transform<I, O>(schema: Schema<I>, transformFn: (value: I) => O): TransformSchema<I, O>;
|