@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
|
@@ -1,7 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Schema } from '../schema.js';
|
|
2
|
+
export class TransformSchema extends Schema {
|
|
3
|
+
schema;
|
|
4
|
+
transformFn;
|
|
5
|
+
constructor(schema, transformFn) {
|
|
6
|
+
super();
|
|
7
|
+
this.schema = schema;
|
|
8
|
+
this.transformFn = transformFn;
|
|
9
|
+
}
|
|
10
|
+
_test(value, path, options) {
|
|
11
|
+
const result = this.schema._test(value, path, options);
|
|
12
|
+
if (!result.valid) {
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
return { valid: true, value: this.transformFn(result.value) };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function transform(schema, transformFn) {
|
|
19
|
+
return new TransformSchema(schema, transformFn);
|
|
7
20
|
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import
|
|
3
|
-
export type Uint8ArraySchemaOptions =
|
|
1
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
2
|
+
import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
|
|
3
|
+
export type Uint8ArraySchemaOptions = SimpleSchemaOptions & {
|
|
4
4
|
/** Minimum byte length */
|
|
5
5
|
minimumLength?: number;
|
|
6
6
|
/** Maximum byte length */
|
|
7
7
|
maximumLength?: number;
|
|
8
8
|
};
|
|
9
|
-
export declare
|
|
10
|
-
|
|
9
|
+
export declare class Uint8ArraySchema extends SimpleSchema<Uint8Array> {
|
|
10
|
+
readonly options: Uint8ArraySchemaOptions;
|
|
11
|
+
constructor(options?: Uint8ArraySchemaOptions);
|
|
12
|
+
}
|
|
13
|
+
export declare function uint8Array(options?: Uint8ArraySchemaOptions): Uint8ArraySchema;
|
|
14
|
+
export declare function Uint8Array(schemaOptions?: Uint8ArraySchemaOptions, options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -1,29 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
coercers.push(uint8ArrayCoercer);
|
|
1
|
+
import { isDefined, isUint8Array } from '../../utils/type-guards.js';
|
|
2
|
+
import { Property } from '../decorators/index.js';
|
|
3
|
+
import { SimpleSchema } from './simple.js';
|
|
4
|
+
export class Uint8ArraySchema extends SimpleSchema {
|
|
5
|
+
options;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super('Uint8Array', isUint8Array, options, {
|
|
8
|
+
constraints: [
|
|
9
|
+
isDefined(options?.minimumLength) ? (value) => (value.byteLength >= options.minimumLength) ? ({ success: true }) : ({ success: false, error: `size must be at least ${options.minimumLength} bytes.` }) : null,
|
|
10
|
+
isDefined(options?.maximumLength) ? (value) => (value.byteLength <= options.maximumLength) ? ({ success: true }) : ({ success: false, error: `size must be at most ${options.maximumLength} bytes.` }) : null
|
|
11
|
+
]
|
|
12
|
+
});
|
|
14
13
|
}
|
|
15
|
-
if (isDefined(options.minimumLength)) {
|
|
16
|
-
valueConstraints.push(new MinimumLengthConstraint(options.minimumLength));
|
|
17
|
-
}
|
|
18
|
-
if (isDefined(options.maximumLength)) {
|
|
19
|
-
valueConstraints.push(new MaximumLengthConstraint(options.maximumLength));
|
|
20
|
-
}
|
|
21
|
-
return valueSchema(Uint8Array, {
|
|
22
|
-
...options,
|
|
23
|
-
coercers,
|
|
24
|
-
valueConstraints
|
|
25
|
-
});
|
|
26
14
|
}
|
|
27
|
-
export function
|
|
28
|
-
return
|
|
15
|
+
export function uint8Array(options) {
|
|
16
|
+
return new Uint8ArraySchema(options);
|
|
17
|
+
}
|
|
18
|
+
export function Uint8Array(schemaOptions, options) {
|
|
19
|
+
return Property(uint8Array(schemaOptions), options);
|
|
29
20
|
}
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
4
|
-
|
|
5
|
-
export declare
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
+
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
3
|
+
import { Schema, type SchemaOutput, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
4
|
+
type UnionSchemaType<T extends [SchemaTestable, ...SchemaTestable[]]> = T[number] extends SchemaTestable<infer V> ? V : never;
|
|
5
|
+
export declare class UnionSchema<T extends [SchemaTestable, ...SchemaTestable[]]> extends Schema<UnionSchemaType<T>> {
|
|
6
|
+
readonly schemas: {
|
|
7
|
+
[P in keyof T]: T[P] extends Schema ? T[P] : Schema<SchemaOutput<T[P]>>;
|
|
8
|
+
};
|
|
9
|
+
constructor(schemas: T);
|
|
10
|
+
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<UnionSchemaType<T>>;
|
|
11
|
+
}
|
|
12
|
+
export declare function union<T extends [SchemaTestable, ...SchemaTestable[]]>(...schemas: T): UnionSchema<T>;
|
|
13
|
+
export declare function Union(...schemasAndOptions: [SchemaTestable, ...SchemaTestable[]] | [SchemaTestable, ...SchemaTestable[], options: SchemaPropertyDecoratorOptions]): SchemaPropertyDecorator;
|
|
14
|
+
export {};
|
package/schema/schemas/union.js
CHANGED
|
@@ -1,13 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Property } from '../decorators/index.js';
|
|
2
|
+
import { Schema } from '../schema.js';
|
|
3
|
+
import { isSchemaTestable, schemaTestableToSchema } from '../testable.js';
|
|
4
|
+
export class UnionSchema extends Schema {
|
|
5
|
+
schemas;
|
|
6
|
+
constructor(schemas) {
|
|
7
|
+
super();
|
|
8
|
+
this.schemas = schemas.map((schema) => schemaTestableToSchema(schema));
|
|
9
|
+
}
|
|
10
|
+
_test(value, path, options) {
|
|
11
|
+
let firstInvalidResult;
|
|
12
|
+
for (const schema of this.schemas) {
|
|
13
|
+
const result = schema._test(value, path, options);
|
|
14
|
+
if (result.valid) {
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
firstInvalidResult ??= result;
|
|
18
|
+
}
|
|
19
|
+
return firstInvalidResult;
|
|
20
|
+
}
|
|
10
21
|
}
|
|
11
|
-
export function
|
|
12
|
-
return
|
|
22
|
+
export function union(...schemas) {
|
|
23
|
+
return new UnionSchema(schemas);
|
|
24
|
+
}
|
|
25
|
+
export function Union(...schemasAndOptions) {
|
|
26
|
+
const schemaOrOptions = schemasAndOptions.at(-1);
|
|
27
|
+
if (isSchemaTestable(schemaOrOptions)) {
|
|
28
|
+
return Property(union(...schemasAndOptions));
|
|
29
|
+
}
|
|
30
|
+
const schemas = schemasAndOptions.slice(0, -1);
|
|
31
|
+
return Property(union(...schemas), schemaOrOptions);
|
|
13
32
|
}
|
|
@@ -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 UnknownSchema extends Schema<unknown> {
|
|
4
|
+
_test(value: unknown): SchemaTestResult<unknown>;
|
|
5
|
+
}
|
|
6
|
+
export declare function unknown(): UnknownSchema;
|
|
7
|
+
export declare function Unknown(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -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 UnknownSchema extends Schema {
|
|
4
|
+
_test(value) {
|
|
5
|
+
return { valid: true, value };
|
|
6
|
+
}
|
|
6
7
|
}
|
|
7
|
-
export function
|
|
8
|
-
return
|
|
8
|
+
export function unknown() {
|
|
9
|
+
return new UnknownSchema();
|
|
10
|
+
}
|
|
11
|
+
export function Unknown(options) {
|
|
12
|
+
return Property(unknown(), options);
|
|
9
13
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isFunction } from '../utils/type-guards.js';
|
|
2
|
+
import { Schema } from './schema.js';
|
|
3
|
+
import { bigint } from './schemas/bigint.js';
|
|
4
|
+
import { boolean } from './schemas/boolean.js';
|
|
5
|
+
import { number } from './schemas/number.js';
|
|
6
|
+
import { getSchemaFromReflection } from './schemas/object.js';
|
|
7
|
+
import { string } from './schemas/string.js';
|
|
8
|
+
import { symbol } from './schemas/symbol.js';
|
|
9
|
+
export function schemaTestableToSchema(testable) {
|
|
10
|
+
if (testable instanceof Schema) {
|
|
11
|
+
return testable;
|
|
12
|
+
}
|
|
13
|
+
switch (testable) {
|
|
14
|
+
case String:
|
|
15
|
+
return string();
|
|
16
|
+
case Number:
|
|
17
|
+
return number();
|
|
18
|
+
case Boolean:
|
|
19
|
+
return boolean();
|
|
20
|
+
case BigInt:
|
|
21
|
+
return bigint();
|
|
22
|
+
case Symbol:
|
|
23
|
+
return symbol();
|
|
24
|
+
default:
|
|
25
|
+
return getSchemaFromReflection(testable);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function isSchemaTestable(value) {
|
|
29
|
+
return (value instanceof Schema) || isFunction(value);
|
|
30
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SchemaError } from './schema.error.js';
|
|
2
|
+
export type Coercible = {
|
|
3
|
+
/**
|
|
4
|
+
* Try to convert wrong input into desired output.
|
|
5
|
+
* Can be specified on definition and validation. If specified on both, definition has higher priority
|
|
6
|
+
*/
|
|
7
|
+
coerce?: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type Maskable = {
|
|
10
|
+
/**
|
|
11
|
+
* Remove unspecified fields on input data instead of raising an error.
|
|
12
|
+
* Can be specified on definition and validation. If specified on both, definition has higher priority
|
|
13
|
+
*/
|
|
14
|
+
mask?: boolean;
|
|
15
|
+
};
|
|
16
|
+
export type CoerceResult<T> = {
|
|
17
|
+
success: true;
|
|
18
|
+
value: T;
|
|
19
|
+
valid: boolean;
|
|
20
|
+
} | {
|
|
21
|
+
success: false;
|
|
22
|
+
value?: undefined;
|
|
23
|
+
error?: SchemaError;
|
|
24
|
+
};
|
|
25
|
+
export type ConstraintResult = {
|
|
26
|
+
success: true;
|
|
27
|
+
error?: undefined;
|
|
28
|
+
} | {
|
|
29
|
+
success: false;
|
|
30
|
+
error: string | SchemaError;
|
|
31
|
+
};
|
package/schema/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Decorator } from '../../reflection/index.js';
|
|
2
|
+
import type { OneOrMany, TypedOmit } from '../../types.js';
|
|
3
|
+
import type { ValueType } from '../types/index.js';
|
|
4
|
+
import type { PropertyOptions } from './types.js';
|
|
5
|
+
export declare function Property(options?: PropertyOptions): Decorator<'property' | 'accessor'>;
|
|
6
|
+
export declare function Property(schema?: OneOrMany<ValueType>, options?: TypedOmit<PropertyOptions, 'schema'>): Decorator<'property' | 'accessor'>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
+
import { toArray } from '../../utils/array/array.js';
|
|
3
|
+
import { isDefined, isFunction, isString, isUndefined } from '../../utils/type-guards.js';
|
|
4
|
+
import { typeSchema } from '../types/index.js';
|
|
5
|
+
import { createSchemaPropertyDecorator } from './utils.js';
|
|
6
|
+
export function Property(optionsOrTypes = {}, optionsOrNothing) {
|
|
7
|
+
const schema = (isFunction(optionsOrTypes) || isString(optionsOrTypes)) ? typeSchema(optionsOrTypes) : undefined;
|
|
8
|
+
const options = isUndefined(schema) ? optionsOrTypes : optionsOrNothing ?? {};
|
|
9
|
+
return createSchemaPropertyDecorator({
|
|
10
|
+
schema,
|
|
11
|
+
...options,
|
|
12
|
+
coercers: isDefined(options.coercers) ? toArray(options.coercers) : undefined,
|
|
13
|
+
transformers: isDefined(options.transformers) ? toArray(options.transformers) : undefined,
|
|
14
|
+
arrayConstraints: isDefined(options.arrayConstraints) ? toArray(options.arrayConstraints) : undefined,
|
|
15
|
+
valueConstraints: isDefined(options.valueConstraints) ? toArray(options.valueConstraints) : undefined
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { OneOrMany } from '../../types.js';
|
|
2
|
+
import type { SchemaTestable } from '../schema.js';
|
|
3
|
+
import type { ObjectSchema, SchemaArrayConstraint, SchemaFactoryFunction, SchemaValueCoercer, SchemaValueConstraint, SchemaValueTransformer, TypeSchema, ValueSchema, ValueType } from '../types/index.js';
|
|
4
|
+
export type SchemaTypeReflectionData = Partial<Pick<ObjectSchema, 'mask' | 'unknownProperties' | 'unknownPropertiesKey'>> & {
|
|
5
|
+
schema?: ObjectSchema | TypeSchema | ValueType;
|
|
6
|
+
factory?: SchemaFactoryFunction<any>;
|
|
7
|
+
};
|
|
8
|
+
export type SchemaPropertyReflectionData = {
|
|
9
|
+
schema?: OneOrMany<SchemaTestable>;
|
|
10
|
+
array?: boolean;
|
|
11
|
+
optional?: boolean;
|
|
12
|
+
nullable?: boolean;
|
|
13
|
+
coerce?: boolean;
|
|
14
|
+
coercers?: readonly SchemaValueCoercer[];
|
|
15
|
+
transformers?: readonly SchemaValueTransformer[];
|
|
16
|
+
arrayConstraints?: readonly SchemaArrayConstraint[];
|
|
17
|
+
valueConstraints?: readonly SchemaValueConstraint[];
|
|
18
|
+
};
|
|
19
|
+
export type PropertyOptions = Partial<ValueSchema<any>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Decorator, PropertyMetadata } from '../../reflection/index.js';
|
|
2
|
+
import type { OneOrMany } from '../../types.js';
|
|
3
|
+
import type { Schema } from '../schema.js';
|
|
4
|
+
import type { SchemaArrayConstraint } from '../types/schema-array-constraint.js';
|
|
5
|
+
import type { SchemaValueCoercer } from '../types/schema-value-coercer.js';
|
|
6
|
+
import type { SchemaValueConstraint } from '../types/schema-value-constraint.js';
|
|
7
|
+
import type { SchemaValueTransformer } from '../types/schema-value-transformer.js';
|
|
8
|
+
import type { PropertyOptions, SchemaPropertyReflectionData } from './types.js';
|
|
9
|
+
export declare function createSchemaPropertyDecorator(options: PropertyOptions): Decorator<'property' | 'accessor'>;
|
|
10
|
+
export declare function createSchemaPropertyDecoratorFromSchema(schema: Schema): Decorator<'property' | 'accessor'>;
|
|
11
|
+
export declare function createSchemaValueCoercerDecorator(coercer: SchemaValueCoercer, options?: PropertyOptions): Decorator<'property' | 'accessor'>;
|
|
12
|
+
export declare function createSchemaValueTransformerDecorator(transformer: SchemaValueTransformer, options?: PropertyOptions): Decorator<'property' | 'accessor'>;
|
|
13
|
+
export declare function createSchemaArrayConstraintDecorator(constraint: SchemaArrayConstraint, options?: PropertyOptions): Decorator<'property' | 'accessor'>;
|
|
14
|
+
export declare function createSchemaValueConstraintDecorator(constraints: OneOrMany<SchemaValueConstraint>, options?: PropertyOptions): Decorator<'property' | 'accessor'>;
|
|
15
|
+
export declare function tryGetSchemaPropertyReflectionData(metadata: PropertyMetadata): SchemaPropertyReflectionData | undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
+
import { createPropertyOrAccessorDecorator } from '../../reflection/index.js';
|
|
3
|
+
import { toArray } from '../../utils/array/array.js';
|
|
4
|
+
import { merge } from '../../utils/merge.js';
|
|
5
|
+
import { filterObject } from '../../utils/object/object.js';
|
|
6
|
+
import { isArray, isDefined, isUndefined } from '../../utils/type-guards.js';
|
|
7
|
+
import { isValueSchema, valueSchema } from '../types/types.js';
|
|
8
|
+
export function createSchemaPropertyDecorator(options) {
|
|
9
|
+
return createPropertyOrAccessorDecorator({
|
|
10
|
+
handler(_, metadata) {
|
|
11
|
+
const schemaData = getOrCreateSchemaPropertyReflectionData(metadata);
|
|
12
|
+
const newSchemaData = {
|
|
13
|
+
...options,
|
|
14
|
+
coercers: (isDefined(options.coercers) && (!isArray(options.coercers) || options.coercers.length > 0)) ? merge(toArray(options.coercers), schemaData.coercers) : undefined,
|
|
15
|
+
transformers: (isDefined(options.transformers) && (!isArray(options.transformers) || options.transformers.length > 0)) ? merge(toArray(options.transformers), schemaData.transformers) : undefined,
|
|
16
|
+
arrayConstraints: (isDefined(options.arrayConstraints) && (!isArray(options.arrayConstraints) || options.arrayConstraints.length > 0)) ? merge(toArray(options.arrayConstraints), schemaData.arrayConstraints) : undefined,
|
|
17
|
+
valueConstraints: (isDefined(options.valueConstraints) && (!isArray(options.valueConstraints) || options.valueConstraints.length > 0)) ? merge(toArray(options.valueConstraints), schemaData.valueConstraints) : undefined
|
|
18
|
+
};
|
|
19
|
+
metadata.data.set('schema', filterObject(newSchemaData, isDefined), true);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export function createSchemaPropertyDecoratorFromSchema(schema) {
|
|
24
|
+
return createSchemaPropertyDecorator(isValueSchema(schema) ? schema : valueSchema(schema));
|
|
25
|
+
}
|
|
26
|
+
export function createSchemaValueCoercerDecorator(coercer, options) {
|
|
27
|
+
return createSchemaPropertyDecorator({ ...options, coercers: coercer });
|
|
28
|
+
}
|
|
29
|
+
export function createSchemaValueTransformerDecorator(transformer, options) {
|
|
30
|
+
return createSchemaPropertyDecorator({ ...options, transformers: transformer });
|
|
31
|
+
}
|
|
32
|
+
export function createSchemaArrayConstraintDecorator(constraint, options) {
|
|
33
|
+
return createSchemaPropertyDecorator({ ...options, arrayConstraints: constraint });
|
|
34
|
+
}
|
|
35
|
+
export function createSchemaValueConstraintDecorator(constraints, options) {
|
|
36
|
+
return createSchemaPropertyDecorator({ ...options, valueConstraints: constraints });
|
|
37
|
+
}
|
|
38
|
+
export function tryGetSchemaPropertyReflectionData(metadata) {
|
|
39
|
+
return metadata.data.tryGet('schema');
|
|
40
|
+
}
|
|
41
|
+
function getOrCreateSchemaPropertyReflectionData(metadata) {
|
|
42
|
+
let schemaData = tryGetSchemaPropertyReflectionData(metadata);
|
|
43
|
+
if (isUndefined(schemaData)) {
|
|
44
|
+
schemaData = {};
|
|
45
|
+
metadata.data.set('schema', schemaData);
|
|
46
|
+
}
|
|
47
|
+
return schemaData;
|
|
48
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './array-constraints/index.js';
|
|
2
|
+
export * from './coercers/index.js';
|
|
3
|
+
export * from './constraints/index.js';
|
|
4
|
+
export * from './decorators/index.js';
|
|
5
|
+
export * from './schema.error.js';
|
|
6
|
+
export * from './schema.js';
|
|
7
|
+
export * from './schemas/index.js';
|
|
8
|
+
export * from './transformers/index.js';
|
|
9
|
+
export * from './types/index.js';
|
|
10
|
+
export * from './utils/index.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './array-constraints/index.js';
|
|
2
|
+
export * from './coercers/index.js';
|
|
3
|
+
export * from './constraints/index.js';
|
|
4
|
+
export * from './decorators/index.js';
|
|
5
|
+
export * from './schema.error.js';
|
|
6
|
+
export * from './schema.js';
|
|
7
|
+
export * from './schemas/index.js';
|
|
8
|
+
export * from './transformers/index.js';
|
|
9
|
+
export * from './types/index.js';
|
|
10
|
+
export * from './utils/index.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { JsonPath } from '../json-path/index.js';
|
|
2
|
+
import type { NormalizedObjectSchema, NormalizedTypeSchema, NormalizedValueSchema, ObjectSchema, SchemaOutput, SchemaTestOptions, SchemaTestResult, SchemaValueCoercer, TupleSchemaOutput, TypeSchema, ValueSchema, ValueType } from './types/index.js';
|
|
3
|
+
export type Schema<T = any> = ObjectSchema<T> | ValueSchema<T> | TypeSchema<T>;
|
|
4
|
+
export type SchemaTestable<T = any> = Schema<T> | ValueType<T>;
|
|
5
|
+
export type NormalizedSchema<T = any> = NormalizedObjectSchema<T> | NormalizedValueSchema<T> | NormalizedTypeSchema<T>;
|
|
6
|
+
export declare const Schema: {
|
|
7
|
+
registerDefaultCoercer(coercer: SchemaValueCoercer): void;
|
|
8
|
+
test<T>(schemaOrValueType: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions, path?: JsonPath): SchemaTestResult<T>;
|
|
9
|
+
validate<T>(schemaOrValueType: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions): boolean;
|
|
10
|
+
parse<T>(schemaOrValueType: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions): T;
|
|
11
|
+
function<T extends readonly SchemaTestable[], R extends SchemaTestable, F extends (...args: TupleSchemaOutput<T>) => SchemaOutput<R>>(argumentSchemas: T, returnSchema: R, handler: F): F;
|
|
12
|
+
asyncFunction<T extends readonly SchemaTestable[], R extends SchemaTestable, F extends (...args: TupleSchemaOutput<T>) => Promise<SchemaOutput<R>>>(argumentSchemas: T, returnSchema: R, handler: F): F;
|
|
13
|
+
};
|
|
14
|
+
export declare function testSchema<T>(schema: Schema<T>, value: unknown, options?: SchemaTestOptions, path?: JsonPath): SchemaTestResult<T>;
|
|
15
|
+
export declare function getExpectString(schema: SchemaTestable): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CustomErrorOptions } from '../errors/custom.error.js';
|
|
2
|
+
import { CustomError } from '../errors/custom.error.js';
|
|
3
|
+
import type { JsonPath } from '../json-path/index.js';
|
|
4
|
+
import type { OneOrMany, TypedOmit, UndefinableJson } from '../types.js';
|
|
5
|
+
import type { ErrorExtraInfo } from '../utils/format-error.js';
|
|
6
|
+
import type { ValueType } from './types/index.js';
|
|
7
|
+
export type SchemaErrorOptions = Pick<CustomErrorOptions, 'fast'> & {
|
|
8
|
+
path: string | JsonPath;
|
|
9
|
+
details?: UndefinableJson;
|
|
10
|
+
inner?: OneOrMany<SchemaError>;
|
|
11
|
+
cause?: any;
|
|
12
|
+
};
|
|
13
|
+
export declare class SchemaError extends CustomError implements ErrorExtraInfo {
|
|
14
|
+
static readonly errorName = "SchemaError";
|
|
15
|
+
readonly path: string;
|
|
16
|
+
readonly details?: UndefinableJson;
|
|
17
|
+
readonly inner?: OneOrMany<SchemaError>;
|
|
18
|
+
constructor(message: string, options: SchemaErrorOptions, cause?: any);
|
|
19
|
+
static expectedButGot(expected: OneOrMany<string | ValueType>, got: string | ValueType, path: string | JsonPath, options?: TypedOmit<SchemaErrorOptions, 'path'> & {
|
|
20
|
+
customMessage?: string;
|
|
21
|
+
}): SchemaError;
|
|
22
|
+
static couldNotCoerce(expected: OneOrMany<string | ValueType>, got: string | ValueType, path: string | JsonPath, options: TypedOmit<SchemaErrorOptions, 'path'> & {
|
|
23
|
+
customMessage?: string;
|
|
24
|
+
}): SchemaError;
|
|
25
|
+
getExtraInfo(includeMessage?: boolean): UndefinableJson | undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { CustomError } from '../errors/custom.error.js';
|
|
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';
|
|
6
|
+
export class SchemaError extends CustomError {
|
|
7
|
+
static errorName = 'SchemaError';
|
|
8
|
+
path;
|
|
9
|
+
details;
|
|
10
|
+
inner;
|
|
11
|
+
constructor(message, options, cause) {
|
|
12
|
+
super({ message, cause: cause ?? options.cause, fast: options.fast });
|
|
13
|
+
this.path = isString(options.path) ? options.path : options.path.path;
|
|
14
|
+
if (isDefined(options.inner) && (!isArray(options.inner) || (options.inner.length > 0))) {
|
|
15
|
+
this.inner = isArray(options.inner)
|
|
16
|
+
? (options.inner.length == 1)
|
|
17
|
+
? options.inner[0]
|
|
18
|
+
: options.inner
|
|
19
|
+
: options.inner;
|
|
20
|
+
}
|
|
21
|
+
if (isNotNullOrUndefined(options.details)) {
|
|
22
|
+
this.details = options.details;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
26
|
+
static expectedButGot(expected, got, path, options) {
|
|
27
|
+
const expectedNames = toArray(expected).map((exp) => (isString(exp) ? exp : getExpectString(exp)));
|
|
28
|
+
const gotName = isString(got) ? got : getValueTypeName(got);
|
|
29
|
+
const expectedString = expectedNames.length == 1
|
|
30
|
+
? expectedNames[0]
|
|
31
|
+
: `(${expectedNames.join(' | ')})`;
|
|
32
|
+
const customMessage = isDefined(options?.customMessage) ? `: ${options.customMessage}` : '.';
|
|
33
|
+
const message = `Expected ${expectedString} but got ${gotName}${customMessage}`;
|
|
34
|
+
return new SchemaError(message, { path, ...options });
|
|
35
|
+
}
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
37
|
+
static couldNotCoerce(expected, got, path, options) {
|
|
38
|
+
const expectedNames = toArray(expected).map((exp) => (isString(exp) ? exp : getValueTypeName(exp)));
|
|
39
|
+
const gotText = isString(got) ? got : getValueTypeName(got);
|
|
40
|
+
const expectedString = expectedNames.length == 1
|
|
41
|
+
? expectedNames[0]
|
|
42
|
+
: `[${expectedNames.join(', ')}]`;
|
|
43
|
+
const customMessageString = isDefined(options.customMessage) ? `: ${options.customMessage}` : '.';
|
|
44
|
+
const errorMessage = `Could not coerce ${gotText} to ${expectedString}${customMessageString}`;
|
|
45
|
+
return new SchemaError(errorMessage, { path, ...options });
|
|
46
|
+
}
|
|
47
|
+
getExtraInfo(includeMessage = false) {
|
|
48
|
+
const obj = {
|
|
49
|
+
path: this.path
|
|
50
|
+
};
|
|
51
|
+
if (includeMessage) {
|
|
52
|
+
obj['message'] = this.message;
|
|
53
|
+
}
|
|
54
|
+
if (isDefined(this.inner)) {
|
|
55
|
+
obj['inner'] = isArray(this.inner)
|
|
56
|
+
? this.inner.map((error) => error.getExtraInfo(true))
|
|
57
|
+
: this.inner.getExtraInfo(true);
|
|
58
|
+
}
|
|
59
|
+
if (isNotNullOrUndefined(this.details)) {
|
|
60
|
+
obj['details'] = this.details;
|
|
61
|
+
}
|
|
62
|
+
return obj;
|
|
63
|
+
}
|
|
64
|
+
}
|