@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
|
@@ -1,7 +1,84 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import type { EmptyObject, Merge } from 'type-fest';
|
|
2
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
3
|
+
import type { AbstractConstructor, OneOrMany, PartialProperty, PropertiesOfType, Record as RecordType, SimplifyObject, Type, TypedOmit } from '../../types.js';
|
|
4
|
+
import { type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
5
|
+
import type { SchemaPropertyDecorator } from '../decorators/types.js';
|
|
6
|
+
import { Schema, type SchemaOutput, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
7
|
+
import { type OptionalSchema } from './optional.js';
|
|
8
|
+
export type Record<K extends PropertyKey = PropertyKey, V = any> = RecordType<K, V>;
|
|
9
|
+
export type ObjectSchemaFactoryFunction<T> = (data: T) => T;
|
|
10
|
+
export type ObjectSchemaFactory<T> = {
|
|
11
|
+
type: Type<T>;
|
|
12
|
+
} | ObjectSchemaFactoryFunction<T>;
|
|
13
|
+
export type ObjectSchemaProperties<T extends Record = Record> = {
|
|
14
|
+
[P in keyof T]-?: SchemaTestable<T[P]>;
|
|
15
|
+
};
|
|
16
|
+
export type NormalizedObjectSchemaProperties<T extends Record> = {
|
|
17
|
+
[P in keyof T]-?: Schema<T[P]>;
|
|
18
|
+
};
|
|
19
|
+
export type ObjectSchemaOptions<T extends Record = Record, K extends PropertyKey = PropertyKey, V = unknown> = {
|
|
20
|
+
mask?: boolean | null;
|
|
21
|
+
unknownProperties?: SchemaTestable<V> | null;
|
|
22
|
+
unknownPropertiesKey?: SchemaTestable<K> | null;
|
|
23
|
+
factory?: ObjectSchemaFactory<T> | null;
|
|
24
|
+
};
|
|
25
|
+
export type ObjectSchemaOrType<T extends Record = any> = ObjectSchema<T> | AbstractConstructor<T>;
|
|
26
|
+
export type ObjectSchemaPropertiesType<TP extends ObjectSchemaProperties> = SimplifyObject<{
|
|
27
|
+
[P in keyof PartialProperty<TP, PropertiesOfType<TP, OptionalSchema<any>>>]: SchemaOutput<TP[P]>;
|
|
28
|
+
}>;
|
|
29
|
+
export declare const tryGetSchemaFromReflection: typeof _tryGetSchemaFromReflection;
|
|
30
|
+
export declare class ObjectSchema<T extends Record = Record> extends Schema<T> {
|
|
31
|
+
private readonly propertyKeys;
|
|
32
|
+
private readonly allowUnknownProperties;
|
|
33
|
+
readonly properties: NormalizedObjectSchemaProperties<T>;
|
|
34
|
+
readonly mask: boolean | null;
|
|
35
|
+
readonly unknownProperties: Schema | null;
|
|
36
|
+
readonly unknownPropertiesKey: Schema<PropertyKey> | null;
|
|
37
|
+
readonly factory: ObjectSchemaFactory<T> | null;
|
|
38
|
+
constructor(properties: ObjectSchemaProperties<T>, options?: ObjectSchemaOptions<T>);
|
|
39
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T>;
|
|
40
|
+
}
|
|
41
|
+
export declare function object<const K extends PropertyKey, const V>(properties: Record<never>, options: ObjectSchemaOptions<Record<K, V>> & {
|
|
42
|
+
unknownProperties: SchemaTestable<V>;
|
|
43
|
+
unknownPropertiesKey: SchemaTestable<K>;
|
|
44
|
+
}): ObjectSchema<Record<K, V>>;
|
|
45
|
+
export declare function object<const K extends PropertyKey>(properties: Record<never>, options: ObjectSchemaOptions<Record<K, unknown>> & {
|
|
46
|
+
unknownProperties?: undefined;
|
|
47
|
+
unknownPropertiesKey: SchemaTestable<K>;
|
|
48
|
+
}): ObjectSchema<Record<K, unknown>>;
|
|
49
|
+
export declare function object<const V>(properties: Record<never>, options: ObjectSchemaOptions<Record<PropertyKey, V>> & {
|
|
50
|
+
unknownProperties: SchemaTestable<V>;
|
|
51
|
+
unknownPropertiesKey?: undefined;
|
|
52
|
+
}): ObjectSchema<Record<PropertyKey, V>>;
|
|
53
|
+
export declare function object<const TP extends ObjectSchemaProperties, const K extends PropertyKey, const V>(properties: TP, options: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP> & Record<K, V>> & {
|
|
54
|
+
unknownProperties: SchemaTestable<V>;
|
|
55
|
+
unknownPropertiesKey: SchemaTestable<K>;
|
|
56
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<K, V>>;
|
|
57
|
+
export declare function object<const TP extends ObjectSchemaProperties, const K extends PropertyKey>(properties: TP, options: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP> & Record<K, unknown>> & {
|
|
58
|
+
unknownPropertiesKey: SchemaTestable<K>;
|
|
59
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<K, unknown>>;
|
|
60
|
+
export declare function object<const TP extends ObjectSchemaProperties, const V>(properties: TP, options: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP> & Record<PropertyKey, V>> & {
|
|
61
|
+
unknownProperties: SchemaTestable<V>;
|
|
62
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<PropertyKey, V>>;
|
|
63
|
+
export declare function object<const TP extends ObjectSchemaProperties>(properties: TP, options?: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP>> & {
|
|
64
|
+
unknownProperties?: undefined;
|
|
65
|
+
unknownPropertiesKey?: undefined;
|
|
66
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP>>;
|
|
67
|
+
export declare function object<const TP extends ObjectSchemaProperties, const K extends PropertyKey, const V>(properties: TP, options?: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP>, K, V>): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<K, V>>;
|
|
68
|
+
export declare function explicitObject<const T extends Record>(properties: ObjectSchemaProperties<T>, options?: ObjectSchemaOptions<T>): ObjectSchema<T>;
|
|
69
|
+
export declare function record<const K extends PropertyKey, const V>(key: Schema<K>, value: Schema<V>, options?: TypedOmit<ObjectSchemaOptions<Record<K, V>>, 'unknownProperties' | 'unknownPropertiesKey'>): ObjectSchema<Record<K, V>>;
|
|
70
|
+
export declare function assign<const T1 extends Record, const T2 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>): ObjectSchema<Merge<T1, T2>>;
|
|
71
|
+
export declare function assign<const T1 extends Record, const T2 extends Record, const T3 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>): ObjectSchema<Merge<Merge<T1, T2>, T3>>;
|
|
72
|
+
export declare function assign<const T1 extends Record, const T2 extends Record, const T3 extends Record, const T4 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>, d: ObjectSchemaOrType<T4>): ObjectSchema<Merge<Merge<Merge<T1, T2>, T3>, T4>>;
|
|
73
|
+
export declare function assign<const T1 extends Record, const T2 extends Record, const T3 extends Record, const T4 extends Record, const T5 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>, d: ObjectSchemaOrType<T4>, e: ObjectSchemaOrType<T5>): ObjectSchema<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>>;
|
|
74
|
+
export declare function assign<const T1 extends Record, const T2 extends Record, const T3 extends Record, const T4 extends Record, const T5 extends Record, const T6 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>, d: ObjectSchemaOrType<T4>, e: ObjectSchemaOrType<T5>, f: ObjectSchemaOrType<T6>): ObjectSchema<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>>;
|
|
75
|
+
export declare function partial<const T extends Record>(schema: ObjectSchemaOrType<T>): ObjectSchema<Partial<T>>;
|
|
76
|
+
export declare function partial<const T extends Record, const K extends keyof T>(schema: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<PartialProperty<T, K>>;
|
|
77
|
+
export declare function pick<const T extends Record, const K extends keyof T>(schemaOrType: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Pick<T, K>>>;
|
|
78
|
+
export declare function omit<const T extends Record, const K extends keyof T>(schemaOrType: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Omit<T, K>>>;
|
|
79
|
+
export declare function getSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T>;
|
|
80
|
+
declare function _tryGetSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T> | ObjectSchema<T> | null;
|
|
81
|
+
export declare function getObjectSchema<T extends Record>(schemaOrType: SchemaTestable<T>): ObjectSchema<T>;
|
|
82
|
+
export declare function Record<K extends PropertyKey, V>(key: Schema<K>, value: Schema<V>, options?: TypedOmit<ObjectSchemaOptions<Record<K, V>>, 'unknownProperties' | 'unknownPropertiesKey'> & SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
83
|
+
export declare const emptyObjectSchema: ObjectSchema<EmptyObject>;
|
|
84
|
+
export {};
|
package/schema/schemas/object.js
CHANGED
|
@@ -1,12 +1,186 @@
|
|
|
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 { Property } from '../decorators/index.js';
|
|
10
|
+
import { Schema } from '../schema.js';
|
|
11
|
+
import { schemaTestableToSchema } from '../testable.js';
|
|
12
|
+
import { array } from './array.js';
|
|
13
|
+
import { nullable } from './nullable.js';
|
|
14
|
+
import { optional } from './optional.js';
|
|
15
|
+
export const tryGetSchemaFromReflection = memoizeSingle(_tryGetSchemaFromReflection, { weak: true });
|
|
16
|
+
export class ObjectSchema extends Schema {
|
|
17
|
+
propertyKeys;
|
|
18
|
+
allowUnknownProperties;
|
|
19
|
+
properties;
|
|
20
|
+
mask;
|
|
21
|
+
unknownProperties;
|
|
22
|
+
unknownPropertiesKey;
|
|
23
|
+
factory;
|
|
24
|
+
constructor(properties, options = {}) {
|
|
25
|
+
super();
|
|
26
|
+
this.properties = mapObjectValues(properties, (value) => schemaTestableToSchema(value));
|
|
27
|
+
this.mask = options.mask ?? null;
|
|
28
|
+
this.unknownProperties = isNotNullOrUndefined(options.unknownProperties) ? schemaTestableToSchema(options.unknownProperties) : null;
|
|
29
|
+
this.unknownPropertiesKey = isNotNullOrUndefined(options.unknownPropertiesKey) ? schemaTestableToSchema(options.unknownPropertiesKey) : null;
|
|
30
|
+
this.factory = options.factory ?? null;
|
|
31
|
+
this.allowUnknownProperties = isNotNull(this.unknownProperties) || isNotNull(this.unknownPropertiesKey);
|
|
32
|
+
this.propertyKeys = new Set(objectKeys(properties));
|
|
33
|
+
}
|
|
34
|
+
_test(value, path, options) {
|
|
35
|
+
if (!isObject(value)) {
|
|
36
|
+
return { valid: false, error: SchemaError.expectedButGot('object', typeOf(value), path) };
|
|
37
|
+
}
|
|
38
|
+
const mask = this.mask ?? options.mask ?? false;
|
|
39
|
+
const resultValue = isLiteralObject(this.factory) ? new this.factory.type() : {};
|
|
40
|
+
const valueKeys = new Set(objectKeys(value));
|
|
41
|
+
const unknownValuePropertyKeys = valueKeys.difference(this.propertyKeys);
|
|
42
|
+
if ((unknownValuePropertyKeys.size > 0) && !mask && !this.allowUnknownProperties) {
|
|
43
|
+
return { valid: false, error: new SchemaError('Unexpected property', { path: path.add(unknownValuePropertyKeys.values().next().value), fast: options.fastErrors }) };
|
|
44
|
+
}
|
|
45
|
+
for (const key of this.propertyKeys) {
|
|
46
|
+
const propertyResult = this.properties[key]._test(value[key], path.add(key), options);
|
|
47
|
+
if (!propertyResult.valid) {
|
|
48
|
+
return propertyResult;
|
|
49
|
+
}
|
|
50
|
+
resultValue[key] = propertyResult.value;
|
|
51
|
+
}
|
|
52
|
+
if (this.allowUnknownProperties) {
|
|
53
|
+
for (const key of unknownValuePropertyKeys) {
|
|
54
|
+
const propertyPath = path.add(key);
|
|
55
|
+
const keyResult = this.unknownPropertiesKey?._test(key, propertyPath, options) ?? { valid: true };
|
|
56
|
+
if (!keyResult.valid) {
|
|
57
|
+
if (mask) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
return { valid: false, error: new SchemaError('Invalid property key.', { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
|
|
61
|
+
}
|
|
62
|
+
const innerValueResult = this.unknownProperties?._test(value[key], propertyPath, options) ?? { valid: true, value: value[key] };
|
|
63
|
+
if (!innerValueResult.valid) {
|
|
64
|
+
return innerValueResult;
|
|
65
|
+
}
|
|
66
|
+
resultValue[key] = innerValueResult.value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const testResultValue = isFunction(this.factory) ? this.factory(resultValue) : resultValue;
|
|
70
|
+
return { valid: true, value: testResultValue };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export function object(properties, options) {
|
|
74
|
+
return new ObjectSchema(properties, options);
|
|
75
|
+
}
|
|
3
76
|
export function explicitObject(properties, options) {
|
|
4
77
|
return object(properties, options);
|
|
5
78
|
}
|
|
6
|
-
export function
|
|
7
|
-
return
|
|
8
|
-
|
|
9
|
-
|
|
79
|
+
export function record(key, value, options) {
|
|
80
|
+
return object({}, { ...options, unknownPropertiesKey: key, unknownProperties: value });
|
|
81
|
+
}
|
|
82
|
+
export function assign(...schemasOrTypes) {
|
|
83
|
+
const schemas = schemasOrTypes.map(getObjectSchema);
|
|
84
|
+
return object(schemas.reduce((result, schema) => ({ ...result, ...schema.properties }), {}), {
|
|
85
|
+
mask: schemas.findLast((schema) => isNotNull(schema.mask))?.mask,
|
|
86
|
+
unknownProperties: schemas.findLast((schema) => isNotNull(schema.unknownProperties))?.unknownProperties,
|
|
87
|
+
unknownPropertiesKey: schemas.findLast((schema) => isNotNull(schema.unknownPropertiesKey))?.unknownPropertiesKey
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
export function partial(schemaOrType, keyOrKeys) {
|
|
91
|
+
const schema = getObjectSchema(schemaOrType);
|
|
92
|
+
const keys = isUndefined(keyOrKeys) ? undefined : toArray(keyOrKeys);
|
|
93
|
+
const mapper = isUndefined(keys)
|
|
94
|
+
? (propertySchema) => optional(propertySchema)
|
|
95
|
+
: (propertySchema, key) => keys.includes(key) ? optional(propertySchema) : propertySchema;
|
|
96
|
+
return object(mapObjectValues(schema.properties, mapper), {
|
|
97
|
+
mask: schema.mask,
|
|
98
|
+
unknownProperties: schema.unknownProperties,
|
|
99
|
+
unknownPropertiesKey: schema.unknownPropertiesKey
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
export function pick(schemaOrType, keys) {
|
|
103
|
+
const schema = getObjectSchema(schemaOrType);
|
|
104
|
+
const keyArray = toArray(keys);
|
|
105
|
+
return object(filterObject(schema.properties, (_, key) => keyArray.includes(key)), {
|
|
106
|
+
mask: schema.mask,
|
|
107
|
+
unknownProperties: schema.unknownProperties,
|
|
108
|
+
unknownPropertiesKey: schema.unknownPropertiesKey
|
|
10
109
|
});
|
|
11
110
|
}
|
|
111
|
+
export function omit(schemaOrType, keys) {
|
|
112
|
+
const schema = getObjectSchema(schemaOrType);
|
|
113
|
+
const keysArray = toArray(keys);
|
|
114
|
+
return object(filterObject(schema.properties, (_, key) => !keysArray.includes(key)), {
|
|
115
|
+
mask: schema.mask,
|
|
116
|
+
unknownProperties: schema.unknownProperties,
|
|
117
|
+
unknownPropertiesKey: schema.unknownPropertiesKey
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
export function getSchemaFromReflection(type) {
|
|
121
|
+
const schema = tryGetSchemaFromReflection(type);
|
|
122
|
+
if (isNull(schema)) {
|
|
123
|
+
throw new Error(`Could not get schema for ${type.name} from reflection data.`);
|
|
124
|
+
}
|
|
125
|
+
return schema;
|
|
126
|
+
}
|
|
127
|
+
function _tryGetSchemaFromReflection(type) {
|
|
128
|
+
const metadata = reflectionRegistry.getMetadata(type);
|
|
129
|
+
if (isUndefined(metadata)) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const typeData = metadata.data.tryGet('schema') ?? {};
|
|
133
|
+
if (typeData.schema instanceof Schema) {
|
|
134
|
+
return typeData.schema;
|
|
135
|
+
}
|
|
136
|
+
if (isFunction(typeData.schema)) {
|
|
137
|
+
return schemaTestableToSchema(typeData.schema);
|
|
138
|
+
}
|
|
139
|
+
const schema = object(getObjectSchemaPropertiesFromReflection(metadata, type), {
|
|
140
|
+
factory: isDefined(typeData.factory) ? typeData.factory : { type: type },
|
|
141
|
+
mask: typeData.mask,
|
|
142
|
+
unknownProperties: typeData.unknownProperties,
|
|
143
|
+
unknownPropertiesKey: typeData.unknownPropertiesKey
|
|
144
|
+
});
|
|
145
|
+
const prototype = Reflect.getPrototypeOf(type);
|
|
146
|
+
if (isNotNull(prototype) && reflectionRegistry.hasType(prototype)) {
|
|
147
|
+
const parentSchema = getSchemaFromReflection(prototype);
|
|
148
|
+
assert(parentSchema instanceof ObjectSchema, 'Can not infer an ObjectSchema from reflection when parent class has an explicit non-object schema defined.');
|
|
149
|
+
return assign(parentSchema, schema);
|
|
150
|
+
}
|
|
151
|
+
return schema;
|
|
152
|
+
}
|
|
153
|
+
function getObjectSchemaPropertiesFromReflection(metadata, type) {
|
|
154
|
+
const properties = {};
|
|
155
|
+
for (const [key, propertyMetadata] of metadata.properties) {
|
|
156
|
+
const reflectionData = propertyMetadata.data.tryGet('schema');
|
|
157
|
+
if (isUndefined(reflectionData?.schema) && (propertyMetadata.type == Object)) {
|
|
158
|
+
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 this is wanted.`);
|
|
159
|
+
}
|
|
160
|
+
let propertySchema = reflectionData?.schema ?? propertyMetadata.type;
|
|
161
|
+
if (reflectionData?.array == true) {
|
|
162
|
+
propertySchema = array(propertySchema);
|
|
163
|
+
}
|
|
164
|
+
if (reflectionData?.nullable == true) {
|
|
165
|
+
propertySchema = nullable(propertySchema);
|
|
166
|
+
}
|
|
167
|
+
if (reflectionData?.optional == true) {
|
|
168
|
+
propertySchema = optional(propertySchema);
|
|
169
|
+
}
|
|
170
|
+
properties[key] = propertySchema;
|
|
171
|
+
}
|
|
172
|
+
return properties;
|
|
173
|
+
}
|
|
174
|
+
export function getObjectSchema(schemaOrType) {
|
|
175
|
+
if (schemaOrType instanceof ObjectSchema) {
|
|
176
|
+
return schemaOrType;
|
|
177
|
+
}
|
|
178
|
+
if (isFunction(schemaOrType)) {
|
|
179
|
+
return getObjectSchema(getSchemaFromReflection(schemaOrType));
|
|
180
|
+
}
|
|
181
|
+
throw new Error('Could not infer ObjectSchema.');
|
|
182
|
+
}
|
|
183
|
+
export function Record(key, value, options) {
|
|
184
|
+
return Property(record(key, value, options), options);
|
|
185
|
+
}
|
|
12
186
|
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,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type
|
|
4
|
-
import type
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
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 OptionalSchema<T> extends Schema<T | undefined> {
|
|
6
|
+
readonly schema: Schema<T>;
|
|
7
|
+
constructor(schema: SchemaTestable<T>);
|
|
8
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T | undefined>;
|
|
9
|
+
}
|
|
10
|
+
export declare function optional<T>(schema: SchemaTestable<T>): OptionalSchema<T>;
|
|
11
|
+
export declare function Optional(schema?: SchemaTestable, options?: TypedOmit<SchemaPropertyDecoratorOptions, 'optional'>): SchemaPropertyDecorator;
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { isUndefined } from '../../utils/type-guards.js';
|
|
2
|
+
import { createSchemaPropertyDecorator } from '../decorators/index.js';
|
|
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
|
+
}
|
|
5
20
|
}
|
|
6
|
-
export function
|
|
7
|
-
return
|
|
21
|
+
export function optional(schema) {
|
|
22
|
+
return new OptionalSchema(schema);
|
|
23
|
+
}
|
|
24
|
+
export function Optional(schema, options) {
|
|
25
|
+
return createSchemaPropertyDecorator({ schema, ...options, optional: true });
|
|
8
26
|
}
|
|
@@ -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 ReadableStreamProperty(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
12
|
export function ReadableStreamProperty(options) {
|
|
8
|
-
return
|
|
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 RegExpProperty(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
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function regExp(options) {
|
|
23
|
+
return new RegExpSchema(options);
|
|
6
24
|
}
|
|
7
25
|
export function RegExpProperty(options) {
|
|
8
|
-
return
|
|
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,10 @@
|
|
|
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 StringProperty(options?: SchemaPropertyDecoratorOptions & StringSchemaOptions): SchemaPropertyDecorator;
|