@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.
Files changed (253) hide show
  1. package/api/types.d.ts +1 -1
  2. package/authentication/authentication.api.d.ts +56 -57
  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/errors/custom.error.d.ts +5 -5
  9. package/examples/api/basic-overview.js +4 -4
  10. package/http/client/http-client-request.d.ts +1 -2
  11. package/http/client/module.d.ts +1 -1
  12. package/image-service/image-service.js +4 -7
  13. package/mail/mail.client.d.ts +2 -2
  14. package/mail/mail.client.js +3 -5
  15. package/orm/decorators.d.ts +1 -0
  16. package/orm/decorators.js +4 -0
  17. package/orm/entity.js +1 -1
  18. package/orm/index.d.ts +4 -0
  19. package/orm/index.js +4 -0
  20. package/orm/repository.d.ts +6 -8
  21. package/orm/repository.js +7 -0
  22. package/orm/schema-converter.d.ts +99 -0
  23. package/orm/schema-converter.js +74 -0
  24. package/orm/schema.d.ts +3 -0
  25. package/orm/schema.js +1 -0
  26. package/orm/types.d.ts +8 -0
  27. package/orm/types.js +6 -0
  28. package/package.json +7 -5
  29. package/reflection/registry.d.ts +1 -1
  30. package/reflection/utils.d.ts +2 -2
  31. package/schema/decorators/class.d.ts +1 -1
  32. package/schema/decorators/index.d.ts +0 -2
  33. package/schema/decorators/index.js +0 -2
  34. package/schema/decorators/property.d.ts +6 -5
  35. package/schema/decorators/property.js +10 -14
  36. package/schema/decorators/types.d.ts +7 -12
  37. package/schema/decorators/utils.d.ts +2 -15
  38. package/schema/decorators/utils.js +4 -43
  39. package/schema/index.d.ts +1 -6
  40. package/schema/index.js +15 -6
  41. package/schema/schema.d.ts +89 -14
  42. package/schema/schema.error.d.ts +4 -6
  43. package/schema/schema.error.js +6 -10
  44. package/schema/schema.js +74 -276
  45. package/schema/schemas/any.d.ts +7 -5
  46. package/schema/schemas/any.js +10 -6
  47. package/schema/schemas/array.d.ts +11 -12
  48. package/schema/schemas/array.js +29 -19
  49. package/schema/schemas/bigint.d.ts +9 -0
  50. package/schema/schemas/bigint.js +19 -0
  51. package/schema/schemas/boolean.d.ts +11 -5
  52. package/schema/schemas/boolean.js +41 -6
  53. package/schema/schemas/date.d.ts +9 -7
  54. package/schema/schemas/date.js +24 -18
  55. package/schema/schemas/defaulted.d.ts +11 -6
  56. package/schema/schemas/defaulted.js +23 -8
  57. package/schema/schemas/enumeration.d.ts +10 -5
  58. package/schema/schemas/enumeration.js +25 -15
  59. package/schema/schemas/function.d.ts +8 -0
  60. package/schema/schemas/function.js +14 -0
  61. package/schema/schemas/index.d.ts +5 -6
  62. package/schema/schemas/index.js +5 -6
  63. package/schema/schemas/instance.d.ts +11 -3
  64. package/schema/schemas/instance.js +25 -4
  65. package/schema/schemas/literal.d.ts +10 -5
  66. package/schema/schemas/literal.js +25 -15
  67. package/schema/schemas/never.d.ts +6 -0
  68. package/schema/schemas/never.js +11 -0
  69. package/schema/schemas/nullable.d.ts +11 -6
  70. package/schema/schemas/nullable.js +24 -7
  71. package/schema/schemas/number.d.ts +9 -7
  72. package/schema/schemas/number.js +28 -23
  73. package/schema/schemas/object.d.ts +84 -7
  74. package/schema/schemas/object.js +180 -6
  75. package/schema/schemas/one-or-many.d.ts +10 -7
  76. package/schema/schemas/one-or-many.js +18 -11
  77. package/schema/schemas/optional.d.ts +11 -7
  78. package/schema/schemas/optional.js +24 -6
  79. package/schema/schemas/readable-stream.d.ts +8 -6
  80. package/schema/schemas/readable-stream.js +10 -5
  81. package/schema/schemas/regexp.d.ts +8 -6
  82. package/schema/schemas/regexp.js +24 -6
  83. package/schema/schemas/simple.d.ts +27 -0
  84. package/schema/schemas/simple.js +57 -0
  85. package/schema/schemas/string.d.ts +9 -21
  86. package/schema/schemas/string.js +20 -34
  87. package/schema/schemas/symbol.d.ts +8 -0
  88. package/schema/schemas/symbol.js +19 -0
  89. package/schema/schemas/transform.d.ts +9 -5
  90. package/schema/schemas/transform.js +19 -6
  91. package/schema/schemas/uint8-array.d.ts +9 -5
  92. package/schema/schemas/uint8-array.js +17 -26
  93. package/schema/schemas/union.d.ts +14 -7
  94. package/schema/schemas/union.js +30 -11
  95. package/schema/schemas/unknown.d.ts +7 -5
  96. package/schema/schemas/unknown.js +11 -7
  97. package/schema/testable.d.ts +3 -0
  98. package/schema/testable.js +33 -0
  99. package/schema/types.d.ts +31 -0
  100. package/schema/types.js +1 -0
  101. package/schema-old/decorators/class.d.ts +3 -0
  102. package/schema-old/decorators/class.js +5 -0
  103. package/schema-old/decorators/index.d.ts +6 -0
  104. package/schema-old/decorators/index.js +6 -0
  105. package/schema-old/decorators/property.d.ts +6 -0
  106. package/schema-old/decorators/property.js +17 -0
  107. package/schema-old/decorators/types.d.ts +19 -0
  108. package/schema-old/decorators/types.js +1 -0
  109. package/schema-old/decorators/utils.d.ts +15 -0
  110. package/schema-old/decorators/utils.js +48 -0
  111. package/schema-old/index.d.ts +10 -0
  112. package/schema-old/index.js +10 -0
  113. package/schema-old/schema.d.ts +15 -0
  114. package/schema-old/schema.error.d.ts +26 -0
  115. package/schema-old/schema.error.js +64 -0
  116. package/schema-old/schema.js +283 -0
  117. package/schema-old/schemas/any.d.ts +5 -0
  118. package/schema-old/schemas/any.js +9 -0
  119. package/schema-old/schemas/array.d.ts +12 -0
  120. package/schema-old/schemas/array.js +23 -0
  121. package/schema-old/schemas/boolean.d.ts +5 -0
  122. package/schema-old/schemas/boolean.js +9 -0
  123. package/schema-old/schemas/date.d.ts +8 -0
  124. package/schema-old/schemas/date.js +22 -0
  125. package/schema-old/schemas/defaulted.d.ts +6 -0
  126. package/schema-old/schemas/defaulted.js +10 -0
  127. package/schema-old/schemas/enumeration.d.ts +6 -0
  128. package/schema-old/schemas/enumeration.js +17 -0
  129. package/schema-old/schemas/index.d.ts +26 -0
  130. package/schema-old/schemas/index.js +26 -0
  131. package/schema-old/schemas/instance.d.ts +3 -0
  132. package/schema-old/schemas/instance.js +5 -0
  133. package/schema-old/schemas/literal.d.ts +5 -0
  134. package/schema-old/schemas/literal.js +17 -0
  135. package/schema-old/schemas/nullable.d.ts +6 -0
  136. package/schema-old/schemas/nullable.js +9 -0
  137. package/schema-old/schemas/number.d.ts +9 -0
  138. package/schema-old/schemas/number.js +27 -0
  139. package/schema-old/schemas/object.d.ts +7 -0
  140. package/schema-old/schemas/object.js +12 -0
  141. package/schema-old/schemas/one-or-many.d.ts +9 -0
  142. package/schema-old/schemas/one-or-many.js +15 -0
  143. package/schema-old/schemas/optional.d.ts +7 -0
  144. package/schema-old/schemas/optional.js +8 -0
  145. package/schema-old/schemas/readable-stream.d.ts +6 -0
  146. package/schema-old/schemas/readable-stream.js +9 -0
  147. package/schema-old/schemas/regexp.d.ts +6 -0
  148. package/schema-old/schemas/regexp.js +9 -0
  149. package/schema-old/schemas/string.d.ts +22 -0
  150. package/schema-old/schemas/string.js +38 -0
  151. package/schema-old/schemas/transform.d.ts +5 -0
  152. package/schema-old/schemas/transform.js +7 -0
  153. package/schema-old/schemas/uint8-array.d.ts +10 -0
  154. package/schema-old/schemas/uint8-array.js +29 -0
  155. package/schema-old/schemas/union.d.ts +7 -0
  156. package/schema-old/schemas/union.js +13 -0
  157. package/schema-old/schemas/unknown.d.ts +5 -0
  158. package/schema-old/schemas/unknown.js +9 -0
  159. package/templates/resolvers/string.template-resolver.d.ts +0 -1
  160. package/templates/resolvers/string.template-resolver.js +1 -5
  161. package/templates/template.model.js +1 -1
  162. package/types.d.ts +2 -0
  163. package/utils/type-guards.js +1 -1
  164. /package/{schema → schema-old}/array-constraints/index.d.ts +0 -0
  165. /package/{schema → schema-old}/array-constraints/index.js +0 -0
  166. /package/{schema → schema-old}/array-constraints/maximum-length.d.ts +0 -0
  167. /package/{schema → schema-old}/array-constraints/maximum-length.js +0 -0
  168. /package/{schema → schema-old}/array-constraints/minimum-length.d.ts +0 -0
  169. /package/{schema → schema-old}/array-constraints/minimum-length.js +0 -0
  170. /package/{schema → schema-old}/coercers/boolean.coercer.d.ts +0 -0
  171. /package/{schema → schema-old}/coercers/boolean.coercer.js +0 -0
  172. /package/{schema → schema-old}/coercers/date.coercer.d.ts +0 -0
  173. /package/{schema → schema-old}/coercers/date.coercer.js +0 -0
  174. /package/{schema → schema-old}/coercers/index.d.ts +0 -0
  175. /package/{schema → schema-old}/coercers/index.js +0 -0
  176. /package/{schema → schema-old}/coercers/number.coercer.d.ts +0 -0
  177. /package/{schema → schema-old}/coercers/number.coercer.js +0 -0
  178. /package/{schema → schema-old}/coercers/regexp.coercer.d.ts +0 -0
  179. /package/{schema → schema-old}/coercers/regexp.coercer.js +0 -0
  180. /package/{schema → schema-old}/coercers/string.coercer.d.ts +0 -0
  181. /package/{schema → schema-old}/coercers/string.coercer.js +0 -0
  182. /package/{schema → schema-old}/coercers/uint8-array.coercer.d.ts +0 -0
  183. /package/{schema → schema-old}/coercers/uint8-array.coercer.js +0 -0
  184. /package/{schema → schema-old}/constraints/enumeration.d.ts +0 -0
  185. /package/{schema → schema-old}/constraints/enumeration.js +0 -0
  186. /package/{schema → schema-old}/constraints/generic.d.ts +0 -0
  187. /package/{schema → schema-old}/constraints/generic.js +0 -0
  188. /package/{schema → schema-old}/constraints/index.d.ts +0 -0
  189. /package/{schema → schema-old}/constraints/index.js +0 -0
  190. /package/{schema → schema-old}/constraints/integer.d.ts +0 -0
  191. /package/{schema → schema-old}/constraints/integer.js +0 -0
  192. /package/{schema → schema-old}/constraints/length.d.ts +0 -0
  193. /package/{schema → schema-old}/constraints/length.js +0 -0
  194. /package/{schema → schema-old}/constraints/literal.d.ts +0 -0
  195. /package/{schema → schema-old}/constraints/literal.js +0 -0
  196. /package/{schema → schema-old}/constraints/maximum-date.d.ts +0 -0
  197. /package/{schema → schema-old}/constraints/maximum-date.js +0 -0
  198. /package/{schema → schema-old}/constraints/maximum-length.d.ts +0 -0
  199. /package/{schema → schema-old}/constraints/maximum-length.js +0 -0
  200. /package/{schema → schema-old}/constraints/maximum.d.ts +0 -0
  201. /package/{schema → schema-old}/constraints/maximum.js +0 -0
  202. /package/{schema → schema-old}/constraints/minimum-date.d.ts +0 -0
  203. /package/{schema → schema-old}/constraints/minimum-date.js +0 -0
  204. /package/{schema → schema-old}/constraints/minimum-length.d.ts +0 -0
  205. /package/{schema → schema-old}/constraints/minimum-length.js +0 -0
  206. /package/{schema → schema-old}/constraints/minimum.d.ts +0 -0
  207. /package/{schema → schema-old}/constraints/minimum.js +0 -0
  208. /package/{schema → schema-old}/constraints/pattern.d.ts +0 -0
  209. /package/{schema → schema-old}/constraints/pattern.js +0 -0
  210. /package/{schema → schema-old}/decorators/coerce.d.ts +0 -0
  211. /package/{schema → schema-old}/decorators/coerce.js +0 -0
  212. /package/{schema → schema-old}/decorators/constraint.d.ts +0 -0
  213. /package/{schema → schema-old}/decorators/constraint.js +0 -0
  214. /package/{schema → schema-old}/schemas/assign.d.ts +0 -0
  215. /package/{schema → schema-old}/schemas/assign.js +0 -0
  216. /package/{schema → schema-old}/schemas/constraint.d.ts +0 -0
  217. /package/{schema → schema-old}/schemas/constraint.js +0 -0
  218. /package/{schema → schema-old}/schemas/omit.d.ts +0 -0
  219. /package/{schema → schema-old}/schemas/omit.js +0 -0
  220. /package/{schema → schema-old}/schemas/partial.d.ts +0 -0
  221. /package/{schema → schema-old}/schemas/partial.js +0 -0
  222. /package/{schema → schema-old}/schemas/pick.d.ts +0 -0
  223. /package/{schema → schema-old}/schemas/pick.js +0 -0
  224. /package/{schema → schema-old}/schemas/record.d.ts +0 -0
  225. /package/{schema → schema-old}/schemas/record.js +0 -0
  226. /package/{schema → schema-old}/transformers/generic.d.ts +0 -0
  227. /package/{schema → schema-old}/transformers/generic.js +0 -0
  228. /package/{schema → schema-old}/transformers/index.d.ts +0 -0
  229. /package/{schema → schema-old}/transformers/index.js +0 -0
  230. /package/{schema → schema-old}/transformers/lowercase.d.ts +0 -0
  231. /package/{schema → schema-old}/transformers/lowercase.js +0 -0
  232. /package/{schema → schema-old}/transformers/trim.d.ts +0 -0
  233. /package/{schema → schema-old}/transformers/trim.js +0 -0
  234. /package/{schema → schema-old}/transformers/uppercase.d.ts +0 -0
  235. /package/{schema → schema-old}/transformers/uppercase.js +0 -0
  236. /package/{schema → schema-old}/types/index.d.ts +0 -0
  237. /package/{schema → schema-old}/types/index.js +0 -0
  238. /package/{schema → schema-old}/types/schema-array-constraint.d.ts +0 -0
  239. /package/{schema → schema-old}/types/schema-array-constraint.js +0 -0
  240. /package/{schema → schema-old}/types/schema-value-coercer.d.ts +0 -0
  241. /package/{schema → schema-old}/types/schema-value-coercer.js +0 -0
  242. /package/{schema → schema-old}/types/schema-value-constraint.d.ts +0 -0
  243. /package/{schema → schema-old}/types/schema-value-constraint.js +0 -0
  244. /package/{schema → schema-old}/types/schema-value-transformer.d.ts +0 -0
  245. /package/{schema → schema-old}/types/schema-value-transformer.js +0 -0
  246. /package/{schema → schema-old}/types/types.d.ts +0 -0
  247. /package/{schema → schema-old}/types/types.js +0 -0
  248. /package/{schema → schema-old}/utils/index.d.ts +0 -0
  249. /package/{schema → schema-old}/utils/index.js +0 -0
  250. /package/{schema → schema-old}/utils/schema.d.ts +0 -0
  251. /package/{schema → schema-old}/utils/schema.js +0 -0
  252. /package/{schema → schema-old}/utils/value-type.d.ts +0 -0
  253. /package/{schema → schema-old}/utils/value-type.js +0 -0
@@ -1,38 +1,24 @@
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 { MaximumLengthConstraint, MinimumLengthConstraint, PatternConstraint } from '../constraints/index.js';
5
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/utils.js';
6
- import { LowercaseTransformer, TrimTransformer, UppercaseTransformer } from '../transformers/index.js';
7
- import { valueSchema } from '../types/types.js';
8
- export function string(options = {}) {
9
- const valueConstraints = toArrayCopy(options.valueConstraints ?? []);
10
- const transformers = toArrayCopy(options.transformers ?? []);
11
- if (isDefined(options.minimumLength)) {
12
- valueConstraints.push(new MinimumLengthConstraint(options.minimumLength));
1
+ import { isDefined, isRegExp, isString } from '../../utils/type-guards.js';
2
+ import { Property } from '../decorators/index.js';
3
+ import { SimpleSchema } from './simple.js';
4
+ export class StringSchema extends SimpleSchema {
5
+ constructor(options) {
6
+ const pattern = isDefined(options?.pattern) ? isString(options.pattern) ? RegExp(options.pattern, 'u') : isRegExp(options.pattern) ? options.pattern : undefined : undefined;
7
+ super('string', isString, options, {
8
+ coercers: {
9
+ number: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
10
+ boolean: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
11
+ bigint: (value) => ({ success: true, value: globalThis.String(value), valid: true })
12
+ },
13
+ constraints: [
14
+ isDefined(pattern) ? ((value) => pattern.test(value) ? ({ success: true }) : ({ success: false, error: 'Value did not match pattern.' })) : null
15
+ ]
16
+ });
13
17
  }
14
- if (isDefined(options.maximumLength)) {
15
- valueConstraints.push(new MaximumLengthConstraint(options.maximumLength));
16
- }
17
- if (isDefined(options.pattern)) {
18
- const pattern = RegExp(options.pattern, options.patternFlags);
19
- valueConstraints.push(new PatternConstraint(pattern, options.patternName));
20
- }
21
- if (isDefined(options.trim)) {
22
- transformers.push(new TrimTransformer());
23
- }
24
- if (isDefined(options.lowercase)) {
25
- transformers.push(new LowercaseTransformer());
26
- }
27
- if (isDefined(options.uppercase)) {
28
- transformers.push(new UppercaseTransformer());
29
- }
30
- return valueSchema(String, {
31
- ...options,
32
- valueConstraints,
33
- transformers
34
- });
18
+ }
19
+ export function string(options) {
20
+ return new StringSchema(options);
35
21
  }
36
22
  export function StringProperty(options) {
37
- return createSchemaPropertyDecoratorFromSchema(string(options));
23
+ return Property(string(options), options);
38
24
  }
@@ -0,0 +1,8 @@
1
+ import type { JsonPath } from '../../json-path/json-path.js';
2
+ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
3
+ import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
+ export declare class SymbolSchema extends Schema<symbol> {
5
+ _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<symbol>;
6
+ }
7
+ export declare function symbol(): SymbolSchema;
8
+ export declare function SymbolProperty(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
@@ -0,0 +1,19 @@
1
+ import { SchemaError } from '../../schema/schema.error.js';
2
+ import { isSymbol } from '../../utils/type-guards.js';
3
+ import { typeOf } from '../../utils/type-of.js';
4
+ import { Property } from '../decorators/index.js';
5
+ import { Schema } from '../schema.js';
6
+ export class SymbolSchema extends Schema {
7
+ _test(value, path, options) {
8
+ if (isSymbol(value)) {
9
+ return { valid: true, value };
10
+ }
11
+ return { valid: false, error: SchemaError.expectedButGot('symbol', typeOf(value), path, { fast: options.fastErrors }) };
12
+ }
13
+ }
14
+ export function symbol() {
15
+ return new SymbolSchema();
16
+ }
17
+ export function SymbolProperty(options) {
18
+ return Property(symbol(), options);
19
+ }
@@ -1,5 +1,9 @@
1
- import type { OneOrMany } from '../../types.js';
2
- import type { SchemaTestable } from '../schema.js';
3
- import type { GenericTransformFunction } from '../transformers/generic.js';
4
- import type { ValueSchema, ValueType } from '../types/index.js';
5
- export declare function transform<T, O>(schema: OneOrMany<SchemaTestable<T>>, transformFunction: GenericTransformFunction<T, O>, sourceType?: OneOrMany<ValueType<T>>): ValueSchema<O>;
1
+ import type { JsonPath } from '../../json-path/json-path.js';
2
+ import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
3
+ export declare class TransformSchema<I, O> extends Schema<O> {
4
+ readonly schema: Schema<I>;
5
+ readonly transformFn: (value: I) => O;
6
+ constructor(schema: Schema<I>, transformFn: (value: I) => O);
7
+ _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<O>;
8
+ }
9
+ export declare function transform<I, O>(schema: Schema<I>, transformFn: (value: I) => O): TransformSchema<I, O>;
@@ -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 Uint8ArrayProperty(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 Uint8ArrayProperty(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,33 @@
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 { func } from './schemas/function.js';
6
+ import { number } from './schemas/number.js';
7
+ import { getSchemaFromReflection } from './schemas/object.js';
8
+ import { string } from './schemas/string.js';
9
+ import { symbol } from './schemas/symbol.js';
10
+ export function schemaTestableToSchema(testable) {
11
+ if (testable instanceof Schema) {
12
+ return testable;
13
+ }
14
+ switch (testable) {
15
+ case String:
16
+ return string();
17
+ case Number:
18
+ return number();
19
+ case Boolean:
20
+ return boolean();
21
+ case BigInt:
22
+ return bigint();
23
+ case Symbol:
24
+ return symbol();
25
+ case Function:
26
+ return func();
27
+ default:
28
+ return getSchemaFromReflection(testable);
29
+ }
30
+ }
31
+ export function isSchemaTestable(value) {
32
+ return (value instanceof Schema) || isFunction(value);
33
+ }
@@ -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
+ }