@tstdl/base 0.90.91 → 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.
Files changed (252) hide show
  1. package/api/types.d.ts +1 -1
  2. package/authentication/authentication.api.d.ts +21 -22
  3. package/authentication/authentication.api.js +1 -6
  4. package/authentication/client/api.client.d.ts +4 -5
  5. package/authentication/client/api.client.js +1 -2
  6. package/authentication/client/http-client.middleware.d.ts +1 -1
  7. package/browser/pdf-options.js +8 -9
  8. package/dom/observation/intersection-observer.js +2 -2
  9. package/errors/custom.error.d.ts +5 -5
  10. package/examples/api/basic-overview.js +4 -4
  11. package/http/client/http-client-request.d.ts +1 -2
  12. package/http/client/module.d.ts +1 -1
  13. package/image-service/image-service.js +4 -7
  14. package/mail/mail.client.d.ts +2 -2
  15. package/mail/mail.client.js +3 -5
  16. package/orm/decorators.d.ts +1 -0
  17. package/orm/decorators.js +4 -0
  18. package/orm/entity.js +3 -3
  19. package/orm/index.d.ts +4 -0
  20. package/orm/index.js +4 -0
  21. package/orm/repository.d.ts +6 -8
  22. package/orm/repository.js +7 -0
  23. package/orm/schema-converter.d.ts +83 -0
  24. package/orm/schema-converter.js +74 -0
  25. package/orm/schema.d.ts +3 -0
  26. package/orm/schema.js +1 -0
  27. package/orm/types.d.ts +8 -0
  28. package/orm/types.js +6 -0
  29. package/package.json +6 -4
  30. package/reflection/registry.d.ts +1 -1
  31. package/reflection/utils.d.ts +2 -2
  32. package/schema/decorators/class.d.ts +1 -1
  33. package/schema/decorators/index.d.ts +0 -2
  34. package/schema/decorators/index.js +0 -2
  35. package/schema/decorators/property.d.ts +6 -5
  36. package/schema/decorators/property.js +10 -14
  37. package/schema/decorators/types.d.ts +7 -12
  38. package/schema/decorators/utils.d.ts +2 -15
  39. package/schema/decorators/utils.js +3 -43
  40. package/schema/index.d.ts +1 -6
  41. package/schema/index.js +15 -6
  42. package/schema/schema.d.ts +89 -14
  43. package/schema/schema.error.d.ts +4 -6
  44. package/schema/schema.error.js +6 -10
  45. package/schema/schema.js +74 -276
  46. package/schema/schemas/any.d.ts +7 -5
  47. package/schema/schemas/any.js +10 -6
  48. package/schema/schemas/array.d.ts +10 -12
  49. package/schema/schemas/array.js +27 -19
  50. package/schema/schemas/bigint.d.ts +8 -0
  51. package/schema/schemas/bigint.js +19 -0
  52. package/schema/schemas/boolean.d.ts +15 -5
  53. package/schema/schemas/boolean.js +47 -6
  54. package/schema/schemas/date.d.ts +9 -7
  55. package/schema/schemas/date.js +25 -19
  56. package/schema/schemas/defaulted.d.ts +11 -6
  57. package/schema/schemas/defaulted.js +23 -8
  58. package/schema/schemas/enumeration.d.ts +10 -5
  59. package/schema/schemas/enumeration.js +25 -15
  60. package/schema/schemas/index.d.ts +4 -6
  61. package/schema/schemas/index.js +4 -6
  62. package/schema/schemas/instance.d.ts +11 -3
  63. package/schema/schemas/instance.js +25 -4
  64. package/schema/schemas/literal.d.ts +10 -5
  65. package/schema/schemas/literal.js +25 -15
  66. package/schema/schemas/never.d.ts +6 -0
  67. package/schema/schemas/never.js +11 -0
  68. package/schema/schemas/nullable.d.ts +10 -6
  69. package/schema/schemas/nullable.js +24 -7
  70. package/schema/schemas/number.d.ts +13 -7
  71. package/schema/schemas/number.js +34 -23
  72. package/schema/schemas/object.d.ts +75 -7
  73. package/schema/schemas/object.js +171 -6
  74. package/schema/schemas/one-or-many.d.ts +10 -7
  75. package/schema/schemas/one-or-many.js +18 -11
  76. package/schema/schemas/optional.d.ts +10 -7
  77. package/schema/schemas/optional.js +21 -3
  78. package/schema/schemas/readable-stream.d.ts +8 -6
  79. package/schema/schemas/readable-stream.js +11 -6
  80. package/schema/schemas/regexp.d.ts +8 -6
  81. package/schema/schemas/regexp.js +25 -7
  82. package/schema/schemas/simple.d.ts +27 -0
  83. package/schema/schemas/simple.js +57 -0
  84. package/schema/schemas/string.d.ts +13 -21
  85. package/schema/schemas/string.js +26 -34
  86. package/schema/schemas/symbol.d.ts +12 -0
  87. package/schema/schemas/symbol.js +25 -0
  88. package/schema/schemas/transform.d.ts +9 -5
  89. package/schema/schemas/transform.js +19 -6
  90. package/schema/schemas/uint8-array.d.ts +9 -5
  91. package/schema/schemas/uint8-array.js +17 -26
  92. package/schema/schemas/union.d.ts +14 -7
  93. package/schema/schemas/union.js +30 -11
  94. package/schema/schemas/unknown.d.ts +7 -5
  95. package/schema/schemas/unknown.js +11 -7
  96. package/schema/testable.d.ts +3 -0
  97. package/schema/testable.js +30 -0
  98. package/schema/types.d.ts +31 -0
  99. package/schema/types.js +1 -0
  100. package/schema-old/decorators/class.d.ts +3 -0
  101. package/schema-old/decorators/class.js +5 -0
  102. package/schema-old/decorators/index.d.ts +6 -0
  103. package/schema-old/decorators/index.js +6 -0
  104. package/schema-old/decorators/property.d.ts +6 -0
  105. package/schema-old/decorators/property.js +17 -0
  106. package/schema-old/decorators/types.d.ts +19 -0
  107. package/schema-old/decorators/types.js +1 -0
  108. package/schema-old/decorators/utils.d.ts +15 -0
  109. package/schema-old/decorators/utils.js +48 -0
  110. package/schema-old/index.d.ts +10 -0
  111. package/schema-old/index.js +10 -0
  112. package/schema-old/schema.d.ts +15 -0
  113. package/schema-old/schema.error.d.ts +26 -0
  114. package/schema-old/schema.error.js +64 -0
  115. package/schema-old/schema.js +283 -0
  116. package/schema-old/schemas/any.d.ts +5 -0
  117. package/schema-old/schemas/any.js +9 -0
  118. package/schema-old/schemas/array.d.ts +12 -0
  119. package/schema-old/schemas/array.js +23 -0
  120. package/schema-old/schemas/boolean.d.ts +5 -0
  121. package/schema-old/schemas/boolean.js +9 -0
  122. package/schema-old/schemas/date.d.ts +8 -0
  123. package/schema-old/schemas/date.js +22 -0
  124. package/schema-old/schemas/defaulted.d.ts +6 -0
  125. package/schema-old/schemas/defaulted.js +10 -0
  126. package/schema-old/schemas/enumeration.d.ts +6 -0
  127. package/schema-old/schemas/enumeration.js +17 -0
  128. package/schema-old/schemas/index.d.ts +26 -0
  129. package/schema-old/schemas/index.js +26 -0
  130. package/schema-old/schemas/instance.d.ts +3 -0
  131. package/schema-old/schemas/instance.js +5 -0
  132. package/schema-old/schemas/literal.d.ts +5 -0
  133. package/schema-old/schemas/literal.js +17 -0
  134. package/schema-old/schemas/nullable.d.ts +6 -0
  135. package/schema-old/schemas/nullable.js +9 -0
  136. package/schema-old/schemas/number.d.ts +9 -0
  137. package/schema-old/schemas/number.js +27 -0
  138. package/schema-old/schemas/object.d.ts +7 -0
  139. package/schema-old/schemas/object.js +12 -0
  140. package/schema-old/schemas/one-or-many.d.ts +9 -0
  141. package/schema-old/schemas/one-or-many.js +15 -0
  142. package/schema-old/schemas/optional.d.ts +7 -0
  143. package/schema-old/schemas/optional.js +8 -0
  144. package/schema-old/schemas/readable-stream.d.ts +6 -0
  145. package/schema-old/schemas/readable-stream.js +9 -0
  146. package/schema-old/schemas/regexp.d.ts +6 -0
  147. package/schema-old/schemas/regexp.js +9 -0
  148. package/schema-old/schemas/string.d.ts +22 -0
  149. package/schema-old/schemas/string.js +38 -0
  150. package/schema-old/schemas/transform.d.ts +5 -0
  151. package/schema-old/schemas/transform.js +7 -0
  152. package/schema-old/schemas/uint8-array.d.ts +10 -0
  153. package/schema-old/schemas/uint8-array.js +29 -0
  154. package/schema-old/schemas/union.d.ts +7 -0
  155. package/schema-old/schemas/union.js +13 -0
  156. package/schema-old/schemas/unknown.d.ts +5 -0
  157. package/schema-old/schemas/unknown.js +9 -0
  158. package/templates/resolvers/string.template-resolver.d.ts +0 -1
  159. package/templates/resolvers/string.template-resolver.js +1 -5
  160. package/templates/template.model.js +1 -1
  161. package/types.d.ts +2 -0
  162. package/utils/type-guards.js +1 -1
  163. /package/{schema → schema-old}/array-constraints/index.d.ts +0 -0
  164. /package/{schema → schema-old}/array-constraints/index.js +0 -0
  165. /package/{schema → schema-old}/array-constraints/maximum-length.d.ts +0 -0
  166. /package/{schema → schema-old}/array-constraints/maximum-length.js +0 -0
  167. /package/{schema → schema-old}/array-constraints/minimum-length.d.ts +0 -0
  168. /package/{schema → schema-old}/array-constraints/minimum-length.js +0 -0
  169. /package/{schema → schema-old}/coercers/boolean.coercer.d.ts +0 -0
  170. /package/{schema → schema-old}/coercers/boolean.coercer.js +0 -0
  171. /package/{schema → schema-old}/coercers/date.coercer.d.ts +0 -0
  172. /package/{schema → schema-old}/coercers/date.coercer.js +0 -0
  173. /package/{schema → schema-old}/coercers/index.d.ts +0 -0
  174. /package/{schema → schema-old}/coercers/index.js +0 -0
  175. /package/{schema → schema-old}/coercers/number.coercer.d.ts +0 -0
  176. /package/{schema → schema-old}/coercers/number.coercer.js +0 -0
  177. /package/{schema → schema-old}/coercers/regexp.coercer.d.ts +0 -0
  178. /package/{schema → schema-old}/coercers/regexp.coercer.js +0 -0
  179. /package/{schema → schema-old}/coercers/string.coercer.d.ts +0 -0
  180. /package/{schema → schema-old}/coercers/string.coercer.js +0 -0
  181. /package/{schema → schema-old}/coercers/uint8-array.coercer.d.ts +0 -0
  182. /package/{schema → schema-old}/coercers/uint8-array.coercer.js +0 -0
  183. /package/{schema → schema-old}/constraints/enumeration.d.ts +0 -0
  184. /package/{schema → schema-old}/constraints/enumeration.js +0 -0
  185. /package/{schema → schema-old}/constraints/generic.d.ts +0 -0
  186. /package/{schema → schema-old}/constraints/generic.js +0 -0
  187. /package/{schema → schema-old}/constraints/index.d.ts +0 -0
  188. /package/{schema → schema-old}/constraints/index.js +0 -0
  189. /package/{schema → schema-old}/constraints/integer.d.ts +0 -0
  190. /package/{schema → schema-old}/constraints/integer.js +0 -0
  191. /package/{schema → schema-old}/constraints/length.d.ts +0 -0
  192. /package/{schema → schema-old}/constraints/length.js +0 -0
  193. /package/{schema → schema-old}/constraints/literal.d.ts +0 -0
  194. /package/{schema → schema-old}/constraints/literal.js +0 -0
  195. /package/{schema → schema-old}/constraints/maximum-date.d.ts +0 -0
  196. /package/{schema → schema-old}/constraints/maximum-date.js +0 -0
  197. /package/{schema → schema-old}/constraints/maximum-length.d.ts +0 -0
  198. /package/{schema → schema-old}/constraints/maximum-length.js +0 -0
  199. /package/{schema → schema-old}/constraints/maximum.d.ts +0 -0
  200. /package/{schema → schema-old}/constraints/maximum.js +0 -0
  201. /package/{schema → schema-old}/constraints/minimum-date.d.ts +0 -0
  202. /package/{schema → schema-old}/constraints/minimum-date.js +0 -0
  203. /package/{schema → schema-old}/constraints/minimum-length.d.ts +0 -0
  204. /package/{schema → schema-old}/constraints/minimum-length.js +0 -0
  205. /package/{schema → schema-old}/constraints/minimum.d.ts +0 -0
  206. /package/{schema → schema-old}/constraints/minimum.js +0 -0
  207. /package/{schema → schema-old}/constraints/pattern.d.ts +0 -0
  208. /package/{schema → schema-old}/constraints/pattern.js +0 -0
  209. /package/{schema → schema-old}/decorators/coerce.d.ts +0 -0
  210. /package/{schema → schema-old}/decorators/coerce.js +0 -0
  211. /package/{schema → schema-old}/decorators/constraint.d.ts +0 -0
  212. /package/{schema → schema-old}/decorators/constraint.js +0 -0
  213. /package/{schema → schema-old}/schemas/assign.d.ts +0 -0
  214. /package/{schema → schema-old}/schemas/assign.js +0 -0
  215. /package/{schema → schema-old}/schemas/constraint.d.ts +0 -0
  216. /package/{schema → schema-old}/schemas/constraint.js +0 -0
  217. /package/{schema → schema-old}/schemas/omit.d.ts +0 -0
  218. /package/{schema → schema-old}/schemas/omit.js +0 -0
  219. /package/{schema → schema-old}/schemas/partial.d.ts +0 -0
  220. /package/{schema → schema-old}/schemas/partial.js +0 -0
  221. /package/{schema → schema-old}/schemas/pick.d.ts +0 -0
  222. /package/{schema → schema-old}/schemas/pick.js +0 -0
  223. /package/{schema → schema-old}/schemas/record.d.ts +0 -0
  224. /package/{schema → schema-old}/schemas/record.js +0 -0
  225. /package/{schema → schema-old}/transformers/generic.d.ts +0 -0
  226. /package/{schema → schema-old}/transformers/generic.js +0 -0
  227. /package/{schema → schema-old}/transformers/index.d.ts +0 -0
  228. /package/{schema → schema-old}/transformers/index.js +0 -0
  229. /package/{schema → schema-old}/transformers/lowercase.d.ts +0 -0
  230. /package/{schema → schema-old}/transformers/lowercase.js +0 -0
  231. /package/{schema → schema-old}/transformers/trim.d.ts +0 -0
  232. /package/{schema → schema-old}/transformers/trim.js +0 -0
  233. /package/{schema → schema-old}/transformers/uppercase.d.ts +0 -0
  234. /package/{schema → schema-old}/transformers/uppercase.js +0 -0
  235. /package/{schema → schema-old}/types/index.d.ts +0 -0
  236. /package/{schema → schema-old}/types/index.js +0 -0
  237. /package/{schema → schema-old}/types/schema-array-constraint.d.ts +0 -0
  238. /package/{schema → schema-old}/types/schema-array-constraint.js +0 -0
  239. /package/{schema → schema-old}/types/schema-value-coercer.d.ts +0 -0
  240. /package/{schema → schema-old}/types/schema-value-coercer.js +0 -0
  241. /package/{schema → schema-old}/types/schema-value-constraint.d.ts +0 -0
  242. /package/{schema → schema-old}/types/schema-value-constraint.js +0 -0
  243. /package/{schema → schema-old}/types/schema-value-transformer.d.ts +0 -0
  244. /package/{schema → schema-old}/types/schema-value-transformer.js +0 -0
  245. /package/{schema → schema-old}/types/types.d.ts +0 -0
  246. /package/{schema → schema-old}/types/types.js +0 -0
  247. /package/{schema → schema-old}/utils/index.d.ts +0 -0
  248. /package/{schema → schema-old}/utils/index.js +0 -0
  249. /package/{schema → schema-old}/utils/schema.d.ts +0 -0
  250. /package/{schema → schema-old}/utils/schema.js +0 -0
  251. /package/{schema → schema-old}/utils/value-type.d.ts +0 -0
  252. /package/{schema → schema-old}/utils/value-type.js +0 -0
@@ -1,7 +1,20 @@
1
- import { GenericTransformer } from '../transformers/generic.js';
2
- import { valueSchema } from '../types/index.js';
3
- export function transform(schema, transformFunction, sourceType) {
4
- return valueSchema(schema, {
5
- transformers: new GenericTransformer(transformFunction, sourceType)
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 { Decorator } from '../../reflection/index.js';
2
- import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
3
- export type Uint8ArraySchemaOptions = ValueSchemaOptions & {
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 function uint8Array(options?: Uint8ArraySchemaOptions): ValueSchema<Uint8Array>;
10
- export declare function Uint8ArrayProperty(options?: Uint8ArraySchemaOptions): Decorator<'property' | 'accessor'>;
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
- /* eslint-disable @typescript-eslint/naming-convention */
2
- import { toArrayCopy } from '../../utils/array/array.js';
3
- import { isDefined } from '../../utils/type-guards.js';
4
- import { uint8ArrayCoercer } from '../coercers/uint8-array.coercer.js';
5
- import { MaximumLengthConstraint } from '../constraints/maximum-length.js';
6
- import { MinimumLengthConstraint } from '../constraints/minimum-length.js';
7
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/utils.js';
8
- import { valueSchema } from '../types/types.js';
9
- export function uint8Array(options = {}) {
10
- const coercers = toArrayCopy(options.coercers ?? []);
11
- const valueConstraints = toArrayCopy(options.valueConstraints ?? []);
12
- if (options.coerce == true) {
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 Uint8ArrayProperty(options) {
28
- return createSchemaPropertyDecoratorFromSchema(uint8Array(options));
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 { Decorator } from '../../reflection/index.js';
2
- import type { SchemaTestable } from '../schema.js';
3
- import type { SchemaOutput, ValueSchema, ValueSchemaOptions } from '../types/index.js';
4
- export type UnionOptions = ValueSchemaOptions;
5
- export declare function union<T extends SchemaTestable[]>(...schemas: [...T]): ValueSchema<SchemaOutput<T[number]>>;
6
- export declare function union<T extends SchemaTestable[]>(schemas: [...T], options?: UnionOptions): ValueSchema<SchemaOutput<T[number]>>;
7
- export declare function Union(...schemas: SchemaTestable[]): Decorator<'property' | 'accessor'>;
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 {};
@@ -1,13 +1,32 @@
1
- /* eslint-disable @typescript-eslint/naming-convention */
2
- import { assert, isArray } from '../../utils/type-guards.js';
3
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
4
- import { valueSchema } from '../types/index.js';
5
- export function union(...args) {
6
- const schemas = isArray(args[0]) ? args[0] : args;
7
- const options = isArray(args[0]) ? args[1] : undefined;
8
- assert(schemas.length >= 2, 'Union requires at least 2 schemas.');
9
- return valueSchema(schemas, options);
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 Union(...schemas) {
12
- return createSchemaPropertyDecoratorFromSchema(union(...schemas));
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 { Decorator } from '../../reflection/index.js';
2
- import type { ValueSchema, ValueSchemaOptions } from '../types/index.js';
3
- export type UnknownOptions = ValueSchemaOptions;
4
- export declare function unknown(options?: UnknownOptions): ValueSchema<unknown>;
5
- export declare function UnknownProperty(options?: UnknownOptions): Decorator<'property' | 'accessor'>;
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
- /* eslint-disable @typescript-eslint/naming-convention */
2
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
3
- import { valueSchema } from '../types/index.js';
4
- export function unknown(options) {
5
- return valueSchema('any', options);
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 UnknownProperty(options) {
8
- return createSchemaPropertyDecoratorFromSchema(unknown(options));
8
+ export function unknown() {
9
+ return new UnknownSchema();
10
+ }
11
+ export function Unknown(options) {
12
+ return Property(unknown(), options);
9
13
  }
@@ -0,0 +1,3 @@
1
+ import { Schema, type SchemaTestable } from './schema.js';
2
+ export declare function schemaTestableToSchema<T>(testable: SchemaTestable<T>): Schema<T>;
3
+ export declare function isSchemaTestable(value: any): value is SchemaTestable;
@@ -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
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { Decorator } from '../../reflection/index.js';
2
+ import type { SchemaTypeReflectionData } from './types.js';
3
+ export declare function Class(options?: SchemaTypeReflectionData): Decorator<'class'>;
@@ -0,0 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+ import { createClassDecorator } from '../../reflection/index.js';
3
+ export function Class(options = {}) {
4
+ return createClassDecorator({ data: { schema: options }, mergeData: true });
5
+ }
@@ -0,0 +1,6 @@
1
+ export * from './class.js';
2
+ export * from './coerce.js';
3
+ export * from './constraint.js';
4
+ export * from './property.js';
5
+ export * from './types.js';
6
+ export * from './utils.js';
@@ -0,0 +1,6 @@
1
+ export * from './class.js';
2
+ export * from './coerce.js';
3
+ export * from './constraint.js';
4
+ export * from './property.js';
5
+ export * from './types.js';
6
+ export * from './utils.js';
@@ -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
+ }