@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
package/schema/schema.js CHANGED
@@ -1,283 +1,81 @@
1
- import { JsonPath } from '../json-path/index.js';
2
- import { toArray } from '../utils/array/array.js';
3
- import { noop } from '../utils/noop.js';
4
- import { objectKeys } from '../utils/object/object.js';
5
- import { differenceSets } from '../utils/set.js';
6
- import { isArray, isDefined, isFunction, isNotNull, isNull, isUndefined } from '../utils/type-guards.js';
7
- import { booleanCoercer, dateCoercer, numberCoercer, regExpCoercer, stringCoercer, uint8ArrayCoercer } from './coercers/index.js';
8
- import { SchemaError } from './schema.error.js';
9
- import { isObjectSchema, isTransformErrorResult, isTypeSchema, isValueSchema, resolveValueType, resolveValueTypes, schemaTestableToSchema, transformErrorResultSymbol } from './types/index.js';
10
- import { getArrayItemSchema, getSchemaTypeNames, getSchemaValueTypes, getValueType, includesValueType, normalizeObjectSchema, normalizeValueSchema, tryGetObjectSchemaFromReflection } from './utils/index.js';
11
- const defaultCoercers = new Map();
12
- let initialize = () => {
13
- Schema.registerDefaultCoercer(numberCoercer);
14
- Schema.registerDefaultCoercer(booleanCoercer);
15
- Schema.registerDefaultCoercer(stringCoercer);
16
- Schema.registerDefaultCoercer(dateCoercer);
17
- Schema.registerDefaultCoercer(regExpCoercer);
18
- Schema.registerDefaultCoercer(uint8ArrayCoercer);
19
- initialize = noop;
20
- };
21
- // eslint-disable-next-line @typescript-eslint/no-redeclare, @typescript-eslint/naming-convention
22
- export const Schema = {
23
- registerDefaultCoercer(coercer) {
24
- for (const sourceType of toArray(coercer.sourceType)) {
25
- if (!defaultCoercers.has(sourceType)) {
26
- defaultCoercers.set(sourceType, []);
27
- }
28
- defaultCoercers.get(sourceType).push(coercer);
29
- }
30
- },
31
- test(schemaOrValueType, value, options, path = JsonPath.ROOT) {
32
- const normalizedOptions = { fastErrors: true, ...options };
33
- const schema = schemaTestableToSchema(schemaOrValueType);
34
- const result = testSchema(schema, value, normalizedOptions, path);
35
- if (result.valid) {
36
- return result;
37
- }
38
- if (isUndefined(result.error.stack)) {
39
- result.error.stack = new Error().stack;
40
- }
41
- return result;
42
- },
43
- validate(schemaOrValueType, value, options) {
44
- const schema = schemaTestableToSchema(schemaOrValueType);
45
- const result = this.test(schema, value, options);
1
+ import { JsonPath } from '../json-path/json-path.js';
2
+ export class Schema {
3
+ /**
4
+ * Test an unknown value to see whether it corresponds to the schema.
5
+ * @param schema schema to test against
6
+ * @param value value to test
7
+ * @param options validation options
8
+ * @returns test result
9
+ */
10
+ static test;
11
+ /**
12
+ * Validate an unknown value to see whether it corresponds to the schema.
13
+ * @param schema schema to validate against
14
+ * @param value value to validate
15
+ * @param options validation options
16
+ * @returns validation result
17
+ */
18
+ static validate;
19
+ /**
20
+ * Asserts an unknown value to be valid according to the schema.
21
+ * @param schema schema to validate against
22
+ * @param value value to validate
23
+ * @param options validation options
24
+ */
25
+ static assert;
26
+ /**
27
+ * Parse an unknown value to comply with the scheme.
28
+ * @param schema schema to validate against
29
+ * @param value value to validate
30
+ * @param options validation options
31
+ * @returns validation result
32
+ */
33
+ static parse;
34
+ /**
35
+ * Test an unknown value to see whether it corresponds to the schema.
36
+ * @param schema schema to test against
37
+ * @param value value to test
38
+ * @param options validation options
39
+ * @returns test result with either the value or validation error
40
+ */
41
+ test(value, options = {}) {
42
+ return this._test(value, JsonPath.ROOT, options);
43
+ }
44
+ /**
45
+ * Validate an unknown value to see whether it corresponds to the schema.
46
+ * @param schema schema to validate against
47
+ * @param value value to validate
48
+ * @param options validation options
49
+ * @returns validation result. Throws if validation fails
50
+ */
51
+ validate(value, options = {}) {
52
+ const result = this._test(value, JsonPath.ROOT, options);
46
53
  return result.valid;
47
- },
48
- parse(schemaOrValueType, value, options) {
49
- const schema = schemaTestableToSchema(schemaOrValueType);
50
- const result = this.test(schema, value, options);
54
+ }
55
+ /**
56
+ * Asserts an unknown value to be valid according to the schema.
57
+ * @param schema schema to validate against
58
+ * @param value value to validate
59
+ * @param options validation options
60
+ */
61
+ assert(value, options = {}) {
62
+ const result = this._test(value, JsonPath.ROOT, options);
63
+ if (!result.valid) {
64
+ throw result.error;
65
+ }
66
+ }
67
+ /**
68
+ * Parse an unknown value to comply with the scheme.
69
+ * @param schema schema to parse against
70
+ * @param value value to parse
71
+ * @param options validation options
72
+ * @returns parsed value
73
+ */
74
+ parse(value, options = {}) {
75
+ const result = this._test(value, JsonPath.ROOT, options);
51
76
  if (result.valid) {
52
77
  return result.value;
53
78
  }
54
79
  throw result.error;
55
- },
56
- function(argumentSchemas, returnSchema, handler) {
57
- const name = `validated${handler.name.slice(0, 1).toUpperCase()}${handler.name.slice(1)}`;
58
- const schema = getFunctionParametersSchema(argumentSchemas);
59
- return {
60
- [name](...unsafeArgs) {
61
- const safeArgs = Schema.parse(schema, unsafeArgs, { mask: true }); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
62
- const unsafeResult = handler(...safeArgs);
63
- return Schema.parse(returnSchema, unsafeResult); // eslint-disable-line @typescript-eslint/no-unsafe-return
64
- }
65
- }[name];
66
- },
67
- asyncFunction(argumentSchemas, returnSchema, handler) {
68
- const name = `validated${handler.name.slice(0, 1).toUpperCase()}${handler.name.slice(1)}`;
69
- const schema = getFunctionParametersSchema(argumentSchemas);
70
- return {
71
- async [name](...unsafeArgs) {
72
- const safeArgs = Schema.parse(schema, unsafeArgs, { mask: true }); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
73
- const unsafeResult = await handler(...safeArgs);
74
- return Schema.parse(returnSchema, unsafeResult); // eslint-disable-line @typescript-eslint/no-unsafe-return
75
- }
76
- }[name];
77
- }
78
- };
79
- function getFunctionParametersSchema(argumentSchemas) {
80
- const schema = {
81
- factory: { type: Array },
82
- properties: {}
83
- };
84
- argumentSchemas.forEach((arg, index) => (schema.properties[index] = arg));
85
- return schema;
86
- }
87
- export function testSchema(schema, value, options, path = JsonPath.ROOT) {
88
- initialize();
89
- if (isValueSchema(schema)) {
90
- return testValue(schema, value, options, path);
91
- }
92
- if (isTypeSchema(schema)) {
93
- return testType(schema, value, options, path);
94
- }
95
- if (isObjectSchema(schema)) {
96
- return testObject(schema, value, options, path);
97
- }
98
- throw new Error('Unsupported schema');
99
- }
100
- function testType(schema, value, options = {}, path = JsonPath.ROOT) {
101
- const resolvedValueType = resolveValueType(schema.type);
102
- if (isFunction(resolvedValueType)) {
103
- if ((value instanceof resolvedValueType) || (getValueType(value) == resolvedValueType)) {
104
- return { valid: true, value: value };
105
- }
106
- const objectSchema = tryGetObjectSchemaFromReflection(resolvedValueType);
107
- if (isNotNull(objectSchema)) {
108
- return testObject(objectSchema, value, options, path);
109
- }
110
- }
111
- else if ((resolvedValueType == 'any') || ((resolvedValueType == 'null') && isNull(value)) || ((resolvedValueType == 'undefined') && isUndefined(value))) {
112
- return { valid: true, value: value };
113
- }
114
- return { valid: false, error: SchemaError.expectedButGot(resolvedValueType, getValueType(value), path, { fast: options.fastErrors }) };
115
- }
116
- // eslint-disable-next-line complexity
117
- function testObject(objectSchema, value, options = {}, path = JsonPath.ROOT) {
118
- if (!(value instanceof Object)) {
119
- return { valid: false, error: SchemaError.expectedButGot(objectSchema.sourceType ?? 'object', getValueType(value), path, { fast: options.fastErrors }) };
120
- }
121
- const schema = normalizeObjectSchema(objectSchema);
122
- const mask = schema.mask ?? options.mask ?? false;
123
- const resultValue = isDefined(schema.factory?.type) ? new schema.factory.type() : {};
124
- const schemaPropertyKeys = objectKeys(schema.properties);
125
- const valuePropertyKeys = objectKeys(value);
126
- const unknownValuePropertyKeys = differenceSets(new Set(valuePropertyKeys), new Set(schemaPropertyKeys));
127
- if ((unknownValuePropertyKeys.length > 0) && !mask && !schema.allowUnknownProperties) {
128
- return { valid: false, error: new SchemaError('Unknown property', { path: path.add(unknownValuePropertyKeys[0]), fast: options.fastErrors }) };
129
- }
130
- for (const key of schemaPropertyKeys) {
131
- const propertyResult = testSchema(schema.properties[key], value[key], options, path.add(key));
132
- if (!propertyResult.valid) {
133
- return propertyResult;
134
- }
135
- resultValue[key] = propertyResult.value;
136
- }
137
- if (schema.allowUnknownProperties) {
138
- for (const key of unknownValuePropertyKeys) {
139
- const propertyPath = path.add(key);
140
- if (isDefined(schema.unknownPropertiesKey)) {
141
- const keyResult = testSchema(schema.unknownPropertiesKey, key, options);
142
- if (!keyResult.valid && !mask) {
143
- return { valid: false, error: new SchemaError('Invalid property key.', { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
144
- }
145
- }
146
- const propertyResult = testSchema(schema.unknownProperties, value[key], options, path.add(key));
147
- if (!propertyResult.valid && !mask) {
148
- return propertyResult;
149
- }
150
- resultValue[key] = propertyResult.value;
151
- }
152
- }
153
- const testResultValue = isUndefined(schema.factory) ? resultValue : isDefined(schema.factory.type) ? resultValue : schema.factory.builder(resultValue);
154
- return { valid: true, value: testResultValue };
155
- }
156
- // eslint-disable-next-line max-lines-per-function, max-statements, complexity
157
- function testValue(schema, value, options = {}, path = JsonPath.ROOT) {
158
- const normalizedValueSchema = normalizeValueSchema(schema);
159
- if (normalizedValueSchema.optional && isUndefined(value)) {
160
- return { valid: true, value: value };
161
80
  }
162
- if (normalizedValueSchema.nullable && isNull(value)) {
163
- return { valid: true, value: value };
164
- }
165
- const context = {
166
- schema: normalizedValueSchema,
167
- options
168
- };
169
- /** handle arrays */
170
- if (normalizedValueSchema.array) {
171
- if (!isArray(value)) {
172
- if (normalizedValueSchema.coerce) {
173
- return testValue(schema, [value], options, path);
174
- }
175
- return { valid: false, error: SchemaError.expectedButGot(Array, getValueType(value), path, { fast: options.fastErrors }) };
176
- }
177
- for (const arrayConstraint of normalizedValueSchema.arrayConstraints) {
178
- const result = arrayConstraint.validate(value, path, context);
179
- if (!result.valid) {
180
- return { valid: false, error: result.error };
181
- }
182
- }
183
- const itemSchema = getArrayItemSchema(schema);
184
- const validatedItems = [];
185
- for (let i = 0; i < value.length; i++) {
186
- const result = testValue(itemSchema, value[i], options, path.add(i));
187
- if (!result.valid) {
188
- return result;
189
- }
190
- validatedItems.push(result.value);
191
- }
192
- return { valid: true, value: validatedItems };
193
- }
194
- let valueTestResult;
195
- let resultValue;
196
- let valueType;
197
- function updateCurrentState(newValue) {
198
- valueTestResult = isValidValue(normalizedValueSchema.schema, newValue, options, path);
199
- resultValue = valueTestResult.valid ? valueTestResult.value : newValue;
200
- valueType = getValueType(resultValue);
201
- }
202
- updateCurrentState(value);
203
- /** try to coerce */
204
- if (!valueTestResult.valid) {
205
- const targetTypes = getSchemaValueTypes(schema);
206
- const coercers = [
207
- ...(normalizedValueSchema.coercers.get(valueType) ?? []),
208
- ...((normalizedValueSchema.coerce || options.coerce == true) ? (defaultCoercers.get(valueType) ?? []) : [])
209
- ]
210
- .filter((coercer) => includesValueType(coercer.targetType, targetTypes));
211
- const errors = [];
212
- let success = false;
213
- for (const coercer of coercers) {
214
- const coerceResult = coercer.coerce(resultValue, path, context);
215
- if (!coerceResult.success) {
216
- errors.push(coerceResult.error);
217
- continue;
218
- }
219
- success = true;
220
- updateCurrentState(coerceResult.value);
221
- break;
222
- }
223
- if (!success && (errors.length > 0)) {
224
- return { valid: false, error: (errors.length == 1) ? errors[0] : new SchemaError('Value could not be coerced.', { inner: errors, path, fast: options.fastErrors }) };
225
- }
226
- }
227
- if (!valueTestResult.valid) {
228
- return valueTestResult;
229
- }
230
- if (normalizedValueSchema.valueConstraints.length > 0) {
231
- const errors = [];
232
- for (const constraint of normalizedValueSchema.valueConstraints) {
233
- const result = constraint.validate(resultValue, path, context);
234
- if (!result.valid) {
235
- errors.push(result.error);
236
- }
237
- }
238
- if (errors.length > 0) {
239
- return { valid: false, error: (errors.length == 1) ? errors[0] : new SchemaError('Value did not match schema.', { path, inner: errors, fast: options.fastErrors }) };
240
- }
241
- }
242
- if (normalizedValueSchema.transformers.length > 0) {
243
- for (const transformer of normalizedValueSchema.transformers) {
244
- if (isDefined(transformer.sourceType) && !includesValueType(valueType, resolveValueTypes(transformer.sourceType))) {
245
- continue;
246
- }
247
- const transformResult = transformer.transform(resultValue, path, context);
248
- if (isTransformErrorResult(transformResult)) {
249
- return { valid: false, error: transformResult[transformErrorResultSymbol] };
250
- }
251
- return { valid: true, value: transformResult };
252
- }
253
- }
254
- return { valid: true, value: resultValue };
255
- }
256
- function isValidValue(validSchemas, value, options, path) {
257
- const errorResults = [];
258
- for (const schema of validSchemas) {
259
- const result = testSchema(schema, value, options, path);
260
- if (result.valid) {
261
- return result;
262
- }
263
- errorResults.push(result);
264
- }
265
- if (errorResults.length == 1) {
266
- return errorResults[0];
267
- }
268
- const errors = errorResults.map((result) => result.error);
269
- const expectStrings = [];
270
- for (const schema of validSchemas) {
271
- expectStrings.push(getExpectString(schema));
272
- }
273
- const expectString = expectStrings.length > 1 ? `(${expectStrings.join(' | ')})` : expectStrings[0];
274
- return { valid: false, error: SchemaError.expectedButGot(expectString, getValueType(value), path, { inner: errors, fast: options.fastErrors }) };
275
- }
276
- export function getExpectString(schema) {
277
- const expectedNames = getSchemaTypeNames(schema);
278
- const arraySuffix = (isValueSchema(schema) && (schema.array == true)) ? '[]' : '';
279
- const expectedTypeString = (expectedNames.length == 1) ? `${expectedNames[0]}${arraySuffix}` : `(${expectedNames.join(' | ')})${arraySuffix}`;
280
- const expects = isValueSchema(schema) ? toArray(schema.valueConstraints ?? []).map((constraint) => constraint.expects) : [];
281
- const expectsString = (expects.length > 0) ? `[${expects.join(', ')}]` : '';
282
- return `${expectedTypeString}${expectsString}`;
283
81
  }
@@ -1,5 +1,7 @@
1
- import type { Decorator } from '../../reflection/index.js';
2
- import type { ValueSchema, ValueSchemaOptions } from '../types/index.js';
3
- export type AnyOptions = ValueSchemaOptions;
4
- export declare function any(options?: AnyOptions): ValueSchema<any>;
5
- export declare function Any(options?: AnyOptions): 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 AnySchema extends Schema<any> {
4
+ _test(value: any): SchemaTestResult<any>;
5
+ }
6
+ export declare function any(): AnySchema;
7
+ export declare function Any(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 any(options) {
5
- return valueSchema('any', options);
1
+ import { Property } from '../decorators/index.js';
2
+ import { Schema } from '../schema.js';
3
+ export class AnySchema extends Schema {
4
+ _test(value) {
5
+ return { valid: true, value };
6
+ }
7
+ }
8
+ export function any() {
9
+ return new AnySchema();
6
10
  }
7
11
  export function Any(options) {
8
- return createSchemaPropertyDecoratorFromSchema(any(options));
12
+ return Property(any(), options);
9
13
  }
@@ -1,12 +1,10 @@
1
- import type { Decorator } from '../../reflection/index.js';
2
- import type { OneOrMany } from '../../types.js';
3
- import type { SchemaTestable } from '../schema.js';
4
- import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
5
- export type ArrayOptions = ValueSchemaOptions & {
6
- /** minimum array length */
7
- minimumLength?: number;
8
- /** maximum array length */
9
- maximumLength?: number;
10
- };
11
- export declare function array<T>(innerValues: OneOrMany<SchemaTestable<T>>, options?: ArrayOptions): ValueSchema<T[]>;
12
- export declare function Array(innerValues: OneOrMany<SchemaTestable>, options?: ArrayOptions): Decorator<'property' | 'accessor'>;
1
+ import type { JsonPath } from '../../json-path/json-path.js';
2
+ import { type SchemaPropertyDecorator } from '../decorators/index.js';
3
+ import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
+ export declare class ArraySchema<T> extends Schema<T[]> {
5
+ readonly itemSchema: Schema<T>;
6
+ constructor(itemSchema: SchemaTestable<T>);
7
+ _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T[]>;
8
+ }
9
+ export declare function array<T>(schema: SchemaTestable<T>): ArraySchema<T>;
10
+ export declare function Array(schema: SchemaTestable): SchemaPropertyDecorator;
@@ -1,23 +1,31 @@
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 { ArrayMaximumLengthConstraint } from '../array-constraints/maximum-length.js';
5
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/utils.js';
6
- import { valueSchema } from '../types/types.js';
7
- export function array(innerValues, options = {}) {
8
- const arrayConstraints = toArrayCopy(options.arrayConstraints ?? []);
9
- if (isDefined(options.minimumLength)) {
10
- arrayConstraints.push(new ArrayMaximumLengthConstraint(options.minimumLength));
1
+ import { SchemaError } from '../../schema/schema.error.js';
2
+ import { isArray } 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
+ import { schemaTestableToSchema } from '../testable.js';
7
+ export class ArraySchema extends Schema {
8
+ itemSchema;
9
+ constructor(itemSchema) {
10
+ super();
11
+ this.itemSchema = schemaTestableToSchema(itemSchema);
11
12
  }
12
- if (isDefined(options.maximumLength)) {
13
- arrayConstraints.push(new ArrayMaximumLengthConstraint(options.maximumLength));
13
+ _test(value, path, options) {
14
+ if (isArray(value)) {
15
+ for (let i = 0; i < value.length; i++) {
16
+ const result = this.itemSchema._test(value[i], path.add(i), options);
17
+ if (!result.valid) {
18
+ return result;
19
+ }
20
+ }
21
+ return { valid: true, value: value };
22
+ }
23
+ return { valid: false, error: SchemaError.expectedButGot('array', typeOf(value), path) };
14
24
  }
15
- return valueSchema(innerValues, {
16
- ...options,
17
- array: true,
18
- arrayConstraints
19
- });
20
25
  }
21
- export function Array(innerValues, options) {
22
- return createSchemaPropertyDecoratorFromSchema(array(innerValues, options));
26
+ export function array(schema) {
27
+ return new ArraySchema(schema);
28
+ }
29
+ export function Array(schema) {
30
+ return Property(schema, { array: true });
23
31
  }
@@ -0,0 +1,8 @@
1
+ import type { JsonPath } from '../../json-path/json-path.js';
2
+ import type { SchemaPropertyDecorator } from '../decorators/types.js';
3
+ import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
+ export declare class BigIntSchema extends Schema<bigint> {
5
+ _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<bigint>;
6
+ }
7
+ export declare function bigint(): BigIntSchema;
8
+ export declare function BigInt(): SchemaPropertyDecorator;
@@ -0,0 +1,19 @@
1
+ import { SchemaError } from '../../schema/schema.error.js';
2
+ import { isBigInt } 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 BigIntSchema extends Schema {
7
+ _test(value, path, options) {
8
+ if (isBigInt(value)) {
9
+ return { valid: true, value };
10
+ }
11
+ return { valid: false, error: SchemaError.expectedButGot('bigint', typeOf(value), path, { fast: options.fastErrors }) };
12
+ }
13
+ }
14
+ export function bigint() {
15
+ return new BigIntSchema();
16
+ }
17
+ export function BigInt() {
18
+ return Property(bigint());
19
+ }
@@ -1,5 +1,15 @@
1
- import type { Decorator } from '../../reflection/index.js';
2
- import type { ValueSchema, ValueSchemaOptions } from '../types/index.js';
3
- export type BooleanOptions = ValueSchemaOptions;
4
- export declare function boolean(options?: BooleanOptions): ValueSchema<boolean>;
5
- export declare function BooleanProperty(options?: BooleanOptions): 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 type { SchemaTestOptions, SchemaTestResult } from '../schema.js';
4
+ import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
5
+ export type BooleanSchemaOptions = SimpleSchemaOptions;
6
+ export declare class BooleanSchema extends SimpleSchema<boolean> {
7
+ constructor(options?: BooleanSchemaOptions);
8
+ _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<boolean>;
9
+ }
10
+ export declare function boolean(): BooleanSchema;
11
+ export declare function Boolean(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
12
+ /**
13
+ * @deprecated use {@link Boolean()} instead
14
+ */
15
+ export declare function BooleanProperty(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
@@ -1,9 +1,50 @@
1
- /* eslint-disable @typescript-eslint/naming-convention */
2
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
3
- import { valueSchema } from '../types/index.js';
4
- export function boolean(options = {}) {
5
- return valueSchema(Boolean, options);
1
+ import { SchemaError } from '../../schema/schema.error.js';
2
+ import { isBoolean, isString } from '../../utils/type-guards.js';
3
+ import { typeOf } from '../../utils/type-of.js';
4
+ import { Property } from '../decorators/index.js';
5
+ import { SimpleSchema } from './simple.js';
6
+ export class BooleanSchema extends SimpleSchema {
7
+ constructor(options) {
8
+ super('boolean', isBoolean, options, {
9
+ coercers: {
10
+ all: (value) => {
11
+ const normalizedValue = isString(value) ? value.toLowerCase().trim() : value;
12
+ switch (normalizedValue) {
13
+ case 1:
14
+ case 1n:
15
+ case 'true':
16
+ case '1':
17
+ case 'yes':
18
+ return { success: true, value: true, valid: true };
19
+ case 0:
20
+ case 0n:
21
+ case 'false':
22
+ case '0':
23
+ case 'no':
24
+ return { success: true, value: false, valid: true };
25
+ default:
26
+ return { success: false };
27
+ }
28
+ }
29
+ }
30
+ });
31
+ }
32
+ _test(value, path, options) {
33
+ if (isBoolean(value)) {
34
+ return { valid: true, value };
35
+ }
36
+ return { valid: false, error: SchemaError.expectedButGot('boolean', typeOf(value), path, { fast: options.fastErrors }) };
37
+ }
6
38
  }
39
+ export function boolean() {
40
+ return new BooleanSchema();
41
+ }
42
+ export function Boolean(options) {
43
+ return Property(boolean(), options);
44
+ }
45
+ /**
46
+ * @deprecated use {@link Boolean()} instead
47
+ */
7
48
  export function BooleanProperty(options) {
8
- return createSchemaPropertyDecoratorFromSchema(boolean(options));
49
+ return Boolean(options);
9
50
  }
@@ -1,8 +1,10 @@
1
- import type { Decorator } from '../../reflection/index.js';
2
- import type { ValueSchema, ValueSchemaOptions } from '../types/types.js';
3
- export type DateOptions = ValueSchemaOptions & {
4
- minimum?: Date | number;
5
- maximum?: Date | number;
1
+ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
2
+ import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
3
+ export type DateSchemaOptions = SimpleSchemaOptions & {
4
+ integer?: boolean;
6
5
  };
7
- export declare function date(options?: DateOptions): ValueSchema<Date>;
8
- export declare function DateProperty(options?: DateOptions): Decorator<'property' | 'accessor'>;
6
+ export declare class DateSchema extends SimpleSchema<globalThis.Date> {
7
+ constructor(options?: DateSchemaOptions);
8
+ }
9
+ export declare function date(options?: DateSchemaOptions): DateSchema;
10
+ export declare function Date(options?: SchemaPropertyDecoratorOptions & DateSchemaOptions): SchemaPropertyDecorator;
@@ -1,22 +1,28 @@
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 { MaximumDateConstraint, MinimumDateConstraint } from '../constraints/index.js';
5
- import { createSchemaPropertyDecoratorFromSchema } from '../decorators/index.js';
6
- import { valueSchema } from '../types/types.js';
7
- export function date(options = {}) {
8
- const constraints = toArrayCopy(options.valueConstraints ?? []);
9
- if (isDefined(options.minimum)) {
10
- constraints.push(new MinimumDateConstraint(options.minimum));
1
+ import { isValidDate } from '../../utils/type-guards.js';
2
+ import { Property } from '../decorators/index.js';
3
+ import { SchemaError } from '../schema.error.js';
4
+ import { SimpleSchema } from './simple.js';
5
+ export class DateSchema extends SimpleSchema {
6
+ constructor(options) {
7
+ super('date', isValidDate, options, {
8
+ coercers: {
9
+ string: (value, path, options) => {
10
+ const result = new globalThis.Date(value);
11
+ return globalThis.Number.isNaN(result.getTime())
12
+ ? { success: false, error: SchemaError.couldNotCoerce('date', 'string', path, { fast: options.fastErrors }) }
13
+ : { success: true, value: result, valid: true };
14
+ },
15
+ number: (value) => ({ success: true, value: new globalThis.Date(value), valid: false })
16
+ },
17
+ constraints: [
18
+ (options?.integer == true) ? (value) => globalThis.Number.isInteger(value) ? ({ success: true }) : ({ success: false, error: 'value is not an integer.' }) : null
19
+ ]
20
+ });
11
21
  }
12
- if (isDefined(options.maximum)) {
13
- constraints.push(new MaximumDateConstraint(options.maximum));
14
- }
15
- return valueSchema(Date, {
16
- ...options,
17
- valueConstraints: constraints
18
- });
19
22
  }
20
- export function DateProperty(options) {
21
- return createSchemaPropertyDecoratorFromSchema(date(options));
23
+ export function date(options) {
24
+ return new DateSchema(options);
25
+ }
26
+ export function Date(options) {
27
+ return Property(date(options), options);
22
28
  }