@tstdl/base 0.90.92 → 0.91.0-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) 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/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 +3 -3
  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 +83 -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 +6 -4
  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 +3 -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 +10 -12
  48. package/schema/schemas/array.js +27 -19
  49. package/schema/schemas/bigint.d.ts +8 -0
  50. package/schema/schemas/bigint.js +19 -0
  51. package/schema/schemas/boolean.d.ts +15 -5
  52. package/schema/schemas/boolean.js +47 -6
  53. package/schema/schemas/date.d.ts +9 -7
  54. package/schema/schemas/date.js +25 -19
  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/index.d.ts +4 -6
  60. package/schema/schemas/index.js +4 -6
  61. package/schema/schemas/instance.d.ts +11 -3
  62. package/schema/schemas/instance.js +25 -4
  63. package/schema/schemas/literal.d.ts +10 -5
  64. package/schema/schemas/literal.js +25 -15
  65. package/schema/schemas/never.d.ts +6 -0
  66. package/schema/schemas/never.js +11 -0
  67. package/schema/schemas/nullable.d.ts +10 -6
  68. package/schema/schemas/nullable.js +24 -7
  69. package/schema/schemas/number.d.ts +13 -7
  70. package/schema/schemas/number.js +34 -23
  71. package/schema/schemas/object.d.ts +75 -7
  72. package/schema/schemas/object.js +171 -6
  73. package/schema/schemas/one-or-many.d.ts +10 -7
  74. package/schema/schemas/one-or-many.js +18 -11
  75. package/schema/schemas/optional.d.ts +10 -7
  76. package/schema/schemas/optional.js +21 -3
  77. package/schema/schemas/readable-stream.d.ts +8 -6
  78. package/schema/schemas/readable-stream.js +11 -6
  79. package/schema/schemas/regexp.d.ts +8 -6
  80. package/schema/schemas/regexp.js +25 -7
  81. package/schema/schemas/simple.d.ts +27 -0
  82. package/schema/schemas/simple.js +57 -0
  83. package/schema/schemas/string.d.ts +13 -21
  84. package/schema/schemas/string.js +26 -34
  85. package/schema/schemas/symbol.d.ts +12 -0
  86. package/schema/schemas/symbol.js +25 -0
  87. package/schema/schemas/transform.d.ts +9 -5
  88. package/schema/schemas/transform.js +19 -6
  89. package/schema/schemas/uint8-array.d.ts +9 -5
  90. package/schema/schemas/uint8-array.js +17 -26
  91. package/schema/schemas/union.d.ts +14 -7
  92. package/schema/schemas/union.js +30 -11
  93. package/schema/schemas/unknown.d.ts +7 -5
  94. package/schema/schemas/unknown.js +11 -7
  95. package/schema/testable.d.ts +3 -0
  96. package/schema/testable.js +30 -0
  97. package/schema/types.d.ts +31 -0
  98. package/schema/types.js +1 -0
  99. package/schema-old/decorators/class.d.ts +3 -0
  100. package/schema-old/decorators/class.js +5 -0
  101. package/schema-old/decorators/index.d.ts +6 -0
  102. package/schema-old/decorators/index.js +6 -0
  103. package/schema-old/decorators/property.d.ts +6 -0
  104. package/schema-old/decorators/property.js +17 -0
  105. package/schema-old/decorators/types.d.ts +19 -0
  106. package/schema-old/decorators/types.js +1 -0
  107. package/schema-old/decorators/utils.d.ts +15 -0
  108. package/schema-old/decorators/utils.js +48 -0
  109. package/schema-old/index.d.ts +10 -0
  110. package/schema-old/index.js +10 -0
  111. package/schema-old/schema.d.ts +15 -0
  112. package/schema-old/schema.error.d.ts +26 -0
  113. package/schema-old/schema.error.js +64 -0
  114. package/schema-old/schema.js +283 -0
  115. package/schema-old/schemas/any.d.ts +5 -0
  116. package/schema-old/schemas/any.js +9 -0
  117. package/schema-old/schemas/array.d.ts +12 -0
  118. package/schema-old/schemas/array.js +23 -0
  119. package/schema-old/schemas/boolean.d.ts +5 -0
  120. package/schema-old/schemas/boolean.js +9 -0
  121. package/schema-old/schemas/date.d.ts +8 -0
  122. package/schema-old/schemas/date.js +22 -0
  123. package/schema-old/schemas/defaulted.d.ts +6 -0
  124. package/schema-old/schemas/defaulted.js +10 -0
  125. package/schema-old/schemas/enumeration.d.ts +6 -0
  126. package/schema-old/schemas/enumeration.js +17 -0
  127. package/schema-old/schemas/index.d.ts +26 -0
  128. package/schema-old/schemas/index.js +26 -0
  129. package/schema-old/schemas/instance.d.ts +3 -0
  130. package/schema-old/schemas/instance.js +5 -0
  131. package/schema-old/schemas/literal.d.ts +5 -0
  132. package/schema-old/schemas/literal.js +17 -0
  133. package/schema-old/schemas/nullable.d.ts +6 -0
  134. package/schema-old/schemas/nullable.js +9 -0
  135. package/schema-old/schemas/number.d.ts +9 -0
  136. package/schema-old/schemas/number.js +27 -0
  137. package/schema-old/schemas/object.d.ts +7 -0
  138. package/schema-old/schemas/object.js +12 -0
  139. package/schema-old/schemas/one-or-many.d.ts +9 -0
  140. package/schema-old/schemas/one-or-many.js +15 -0
  141. package/schema-old/schemas/optional.d.ts +7 -0
  142. package/schema-old/schemas/optional.js +8 -0
  143. package/schema-old/schemas/readable-stream.d.ts +6 -0
  144. package/schema-old/schemas/readable-stream.js +9 -0
  145. package/schema-old/schemas/regexp.d.ts +6 -0
  146. package/schema-old/schemas/regexp.js +9 -0
  147. package/schema-old/schemas/string.d.ts +22 -0
  148. package/schema-old/schemas/string.js +38 -0
  149. package/schema-old/schemas/transform.d.ts +5 -0
  150. package/schema-old/schemas/transform.js +7 -0
  151. package/schema-old/schemas/uint8-array.d.ts +10 -0
  152. package/schema-old/schemas/uint8-array.js +29 -0
  153. package/schema-old/schemas/union.d.ts +7 -0
  154. package/schema-old/schemas/union.js +13 -0
  155. package/schema-old/schemas/unknown.d.ts +5 -0
  156. package/schema-old/schemas/unknown.js +9 -0
  157. package/templates/resolvers/string.template-resolver.d.ts +0 -1
  158. package/templates/resolvers/string.template-resolver.js +1 -5
  159. package/templates/template.model.js +1 -1
  160. package/types.d.ts +2 -0
  161. package/utils/type-guards.js +1 -1
  162. /package/{schema → schema-old}/array-constraints/index.d.ts +0 -0
  163. /package/{schema → schema-old}/array-constraints/index.js +0 -0
  164. /package/{schema → schema-old}/array-constraints/maximum-length.d.ts +0 -0
  165. /package/{schema → schema-old}/array-constraints/maximum-length.js +0 -0
  166. /package/{schema → schema-old}/array-constraints/minimum-length.d.ts +0 -0
  167. /package/{schema → schema-old}/array-constraints/minimum-length.js +0 -0
  168. /package/{schema → schema-old}/coercers/boolean.coercer.d.ts +0 -0
  169. /package/{schema → schema-old}/coercers/boolean.coercer.js +0 -0
  170. /package/{schema → schema-old}/coercers/date.coercer.d.ts +0 -0
  171. /package/{schema → schema-old}/coercers/date.coercer.js +0 -0
  172. /package/{schema → schema-old}/coercers/index.d.ts +0 -0
  173. /package/{schema → schema-old}/coercers/index.js +0 -0
  174. /package/{schema → schema-old}/coercers/number.coercer.d.ts +0 -0
  175. /package/{schema → schema-old}/coercers/number.coercer.js +0 -0
  176. /package/{schema → schema-old}/coercers/regexp.coercer.d.ts +0 -0
  177. /package/{schema → schema-old}/coercers/regexp.coercer.js +0 -0
  178. /package/{schema → schema-old}/coercers/string.coercer.d.ts +0 -0
  179. /package/{schema → schema-old}/coercers/string.coercer.js +0 -0
  180. /package/{schema → schema-old}/coercers/uint8-array.coercer.d.ts +0 -0
  181. /package/{schema → schema-old}/coercers/uint8-array.coercer.js +0 -0
  182. /package/{schema → schema-old}/constraints/enumeration.d.ts +0 -0
  183. /package/{schema → schema-old}/constraints/enumeration.js +0 -0
  184. /package/{schema → schema-old}/constraints/generic.d.ts +0 -0
  185. /package/{schema → schema-old}/constraints/generic.js +0 -0
  186. /package/{schema → schema-old}/constraints/index.d.ts +0 -0
  187. /package/{schema → schema-old}/constraints/index.js +0 -0
  188. /package/{schema → schema-old}/constraints/integer.d.ts +0 -0
  189. /package/{schema → schema-old}/constraints/integer.js +0 -0
  190. /package/{schema → schema-old}/constraints/length.d.ts +0 -0
  191. /package/{schema → schema-old}/constraints/length.js +0 -0
  192. /package/{schema → schema-old}/constraints/literal.d.ts +0 -0
  193. /package/{schema → schema-old}/constraints/literal.js +0 -0
  194. /package/{schema → schema-old}/constraints/maximum-date.d.ts +0 -0
  195. /package/{schema → schema-old}/constraints/maximum-date.js +0 -0
  196. /package/{schema → schema-old}/constraints/maximum-length.d.ts +0 -0
  197. /package/{schema → schema-old}/constraints/maximum-length.js +0 -0
  198. /package/{schema → schema-old}/constraints/maximum.d.ts +0 -0
  199. /package/{schema → schema-old}/constraints/maximum.js +0 -0
  200. /package/{schema → schema-old}/constraints/minimum-date.d.ts +0 -0
  201. /package/{schema → schema-old}/constraints/minimum-date.js +0 -0
  202. /package/{schema → schema-old}/constraints/minimum-length.d.ts +0 -0
  203. /package/{schema → schema-old}/constraints/minimum-length.js +0 -0
  204. /package/{schema → schema-old}/constraints/minimum.d.ts +0 -0
  205. /package/{schema → schema-old}/constraints/minimum.js +0 -0
  206. /package/{schema → schema-old}/constraints/pattern.d.ts +0 -0
  207. /package/{schema → schema-old}/constraints/pattern.js +0 -0
  208. /package/{schema → schema-old}/decorators/coerce.d.ts +0 -0
  209. /package/{schema → schema-old}/decorators/coerce.js +0 -0
  210. /package/{schema → schema-old}/decorators/constraint.d.ts +0 -0
  211. /package/{schema → schema-old}/decorators/constraint.js +0 -0
  212. /package/{schema → schema-old}/schemas/assign.d.ts +0 -0
  213. /package/{schema → schema-old}/schemas/assign.js +0 -0
  214. /package/{schema → schema-old}/schemas/constraint.d.ts +0 -0
  215. /package/{schema → schema-old}/schemas/constraint.js +0 -0
  216. /package/{schema → schema-old}/schemas/omit.d.ts +0 -0
  217. /package/{schema → schema-old}/schemas/omit.js +0 -0
  218. /package/{schema → schema-old}/schemas/partial.d.ts +0 -0
  219. /package/{schema → schema-old}/schemas/partial.js +0 -0
  220. /package/{schema → schema-old}/schemas/pick.d.ts +0 -0
  221. /package/{schema → schema-old}/schemas/pick.js +0 -0
  222. /package/{schema → schema-old}/schemas/record.d.ts +0 -0
  223. /package/{schema → schema-old}/schemas/record.js +0 -0
  224. /package/{schema → schema-old}/transformers/generic.d.ts +0 -0
  225. /package/{schema → schema-old}/transformers/generic.js +0 -0
  226. /package/{schema → schema-old}/transformers/index.d.ts +0 -0
  227. /package/{schema → schema-old}/transformers/index.js +0 -0
  228. /package/{schema → schema-old}/transformers/lowercase.d.ts +0 -0
  229. /package/{schema → schema-old}/transformers/lowercase.js +0 -0
  230. /package/{schema → schema-old}/transformers/trim.d.ts +0 -0
  231. /package/{schema → schema-old}/transformers/trim.js +0 -0
  232. /package/{schema → schema-old}/transformers/uppercase.d.ts +0 -0
  233. /package/{schema → schema-old}/transformers/uppercase.js +0 -0
  234. /package/{schema → schema-old}/types/index.d.ts +0 -0
  235. /package/{schema → schema-old}/types/index.js +0 -0
  236. /package/{schema → schema-old}/types/schema-array-constraint.d.ts +0 -0
  237. /package/{schema → schema-old}/types/schema-array-constraint.js +0 -0
  238. /package/{schema → schema-old}/types/schema-value-coercer.d.ts +0 -0
  239. /package/{schema → schema-old}/types/schema-value-coercer.js +0 -0
  240. /package/{schema → schema-old}/types/schema-value-constraint.d.ts +0 -0
  241. /package/{schema → schema-old}/types/schema-value-constraint.js +0 -0
  242. /package/{schema → schema-old}/types/schema-value-transformer.d.ts +0 -0
  243. /package/{schema → schema-old}/types/schema-value-transformer.js +0 -0
  244. /package/{schema → schema-old}/types/types.d.ts +0 -0
  245. /package/{schema → schema-old}/types/types.js +0 -0
  246. /package/{schema → schema-old}/utils/index.d.ts +0 -0
  247. /package/{schema → schema-old}/utils/index.js +0 -0
  248. /package/{schema → schema-old}/utils/schema.d.ts +0 -0
  249. /package/{schema → schema-old}/utils/schema.js +0 -0
  250. /package/{schema → schema-old}/utils/value-type.d.ts +0 -0
  251. /package/{schema → schema-old}/utils/value-type.js +0 -0
@@ -0,0 +1,74 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { boolean, doublePrecision, integer, pgSchema, text, unique, uuid } from 'drizzle-orm/pg-core';
11
+ import { NotSupportedError } from '../errors/not-supported.error.js';
12
+ import { reflectionRegistry } from '../reflection/registry.js';
13
+ import { Property } from '../schema/index.js';
14
+ import { fromEntries } from '../utils/object/object.js';
15
+ import { assertStringPass, isUndefined } from '../utils/type-guards.js';
16
+ import { Integer } from './types.js';
17
+ export const mySchema = pgSchema('my_schema');
18
+ export const colors = mySchema.enum('colors', ['red', 'green', 'blue']);
19
+ export const mySchemaUsers = mySchema.table('users', {
20
+ id: uuid('id'),
21
+ name: text('name'),
22
+ age: integer(''),
23
+ color: colors('color').default('red'),
24
+ }, (self) => ({
25
+ unq: unique().on(self.age).nullsNotDistinct().nullsNotDistinct()
26
+ }));
27
+ export function getDrizzleSchemaFromType(type) {
28
+ const metadata = reflectionRegistry.getMetadata(type);
29
+ if (isUndefined(metadata)) {
30
+ throw new Error('Type does not have reflection metadata.');
31
+ }
32
+ const entries = [...metadata.properties].map(([property, propertyMetadata]) => [property, getPostgresColumn(assertStringPass(property), propertyMetadata.type)]);
33
+ const schema = mySchema.table(type.entityName, fromEntries(entries));
34
+ return schema;
35
+ }
36
+ function getPostgresColumn(name, type) {
37
+ if (type == String) {
38
+ return text(name);
39
+ }
40
+ if (type == Number) {
41
+ return doublePrecision(name);
42
+ }
43
+ if (type == Boolean) {
44
+ return boolean(name);
45
+ }
46
+ throw new NotSupportedError(`Type ${type.name} not supported`);
47
+ }
48
+ export class Entity2 {
49
+ id;
50
+ }
51
+ __decorate([
52
+ Property(),
53
+ __metadata("design:type", String)
54
+ ], Entity2.prototype, "id", void 0);
55
+ export class User extends Entity2 {
56
+ name;
57
+ age;
58
+ hasAge;
59
+ }
60
+ __decorate([
61
+ Property(),
62
+ __metadata("design:type", String)
63
+ ], User.prototype, "name", void 0);
64
+ __decorate([
65
+ Integer(),
66
+ __metadata("design:type", Object)
67
+ ], User.prototype, "age", void 0);
68
+ __decorate([
69
+ Property(),
70
+ __metadata("design:type", Boolean)
71
+ ], User.prototype, "hasAge", void 0);
72
+ const user = getDrizzleSchemaFromType(User);
73
+ const db = null;
74
+ const results = await db.select().from(user);
@@ -0,0 +1,3 @@
1
+ export interface DbSchemaStatic {
2
+ readonly entityName: string;
3
+ }
package/orm/schema.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/orm/types.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { Integer } from '../schema/index.js';
2
+ import type { doublePrecision, integer, uuid } from 'drizzle-orm/pg-core';
3
+ import type { Tagged } from 'type-fest';
4
+ export declare function Uuid(): PropertyDecorator & MethodDecorator;
5
+ export type Uuid<ColumnName extends string> = Tagged<string, 'column', ReturnType<typeof uuid<ColumnName>>>;
6
+ export type Integer<ColumnName extends string> = Tagged<number, 'column', ReturnType<typeof integer<ColumnName>>>;
7
+ export type DoublePrecision<ColumnName extends string> = Tagged<number, 'column', ReturnType<typeof doublePrecision<ColumnName>>>;
8
+ export { Integer };
package/orm/types.js ADDED
@@ -0,0 +1,6 @@
1
+ /* eslint-disable @typescript-eslint/no-redeclare, @typescript-eslint/naming-convention */
2
+ import { createSchemaPropertyDecorator, Integer, string } from '../schema/index.js';
3
+ export function Uuid() {
4
+ return createSchemaPropertyDecorator({ schema: string() });
5
+ }
6
+ export { Integer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.90.92",
3
+ "version": "0.91.0-beta1",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -111,11 +111,11 @@
111
111
  "luxon": "^3.4",
112
112
  "reflect-metadata": "^0.2",
113
113
  "rxjs": "^7.8",
114
- "type-fest": "4.20"
114
+ "type-fest": "4.21"
115
115
  },
116
116
  "devDependencies": {
117
117
  "@mxssfd/typedoc-theme": "1.1",
118
- "@stylistic/eslint-plugin": "2.2",
118
+ "@stylistic/eslint-plugin": "2.3",
119
119
  "@types/chroma-js": "2.4",
120
120
  "@types/koa__router": "12.0",
121
121
  "@types/luxon": "3.4",
@@ -123,7 +123,7 @@
123
123
  "@types/mjml": "4.7",
124
124
  "@types/node": "20",
125
125
  "@types/nodemailer": "6.4",
126
- "@typescript-eslint/eslint-plugin": "7.14",
126
+ "@typescript-eslint/eslint-plugin": "7.16",
127
127
  "concurrently": "8.2",
128
128
  "eslint": "8.57",
129
129
  "eslint-import-resolver-typescript": "3.6",
@@ -142,12 +142,14 @@
142
142
  "@zxcvbn-ts/language-de": "^3.0",
143
143
  "@zxcvbn-ts/language-en": "^3.0",
144
144
  "chroma-js": "^2.4",
145
+ "drizzle-orm": "^0.31",
145
146
  "handlebars": "^4.7",
146
147
  "koa": "^2.15",
147
148
  "minio": "^8.0",
148
149
  "mjml": "^4.15",
149
150
  "mongodb": "^6.7",
150
151
  "nodemailer": "^6.9",
152
+ "pg": "^8.12",
151
153
  "playwright": "^1.45",
152
154
  "preact": "^10.22",
153
155
  "preact-render-to-string": "^6.5",
@@ -9,7 +9,7 @@ export type MetadataBase<T extends MetadataType> = {
9
9
  export type TypeMetadata = MetadataBase<'type'> & {
10
10
  readonly constructor: AbstractConstructor;
11
11
  readonly parent: AbstractConstructor | null;
12
- /** undefined if class has no constructor */
12
+ /** Undefined if class has no constructor */
13
13
  readonly parameters: ConstructorParameterMetadata[] | undefined;
14
14
  readonly properties: ReadonlyMap<string | symbol, PropertyMetadata>;
15
15
  readonly staticProperties: ReadonlyMap<string | symbol, PropertyMetadata>;
@@ -5,9 +5,9 @@ export type CreateDecoratorTypeOptions = {
5
5
  };
6
6
  export type CreateDecoratorOptions = {
7
7
  data?: Record<string | symbol>;
8
- /** merge data values instead of replacing them (requires them to be objects, arrays, maps or sets) */
8
+ /** Merge data values instead of replacing them (requires them to be objects, arrays, maps or sets) */
9
9
  mergeData?: boolean;
10
- /** return values of these decorators are not used */
10
+ /** Return values of these decorators are not used */
11
11
  include?: OneOrMany<DecoratorUnion>;
12
12
  };
13
13
  export type SpecificCreateDecoratorOptions<T extends DecoratorType> = TypedOmit<CreateDecoratorOptions, 'include'> & {
@@ -1,3 +1,3 @@
1
- import type { Decorator } from '../../reflection/index.js';
1
+ import { type Decorator } from '../../reflection/index.js';
2
2
  import type { SchemaTypeReflectionData } from './types.js';
3
3
  export declare function Class(options?: SchemaTypeReflectionData): Decorator<'class'>;
@@ -1,6 +1,4 @@
1
1
  export * from './class.js';
2
- export * from './coerce.js';
3
- export * from './constraint.js';
4
2
  export * from './property.js';
5
3
  export * from './types.js';
6
4
  export * from './utils.js';
@@ -1,6 +1,4 @@
1
1
  export * from './class.js';
2
- export * from './coerce.js';
3
- export * from './constraint.js';
4
2
  export * from './property.js';
5
3
  export * from './types.js';
6
4
  export * from './utils.js';
@@ -1,6 +1,7 @@
1
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'>;
2
+ import type { TypedOmit } from '../../types.js';
3
+ import type { SchemaTestable } from '../schema.js';
4
+ import type { SchemaPropertyReflectionData } from './types.js';
5
+ export type SchemaPropertyDecoratorOptions = TypedOmit<SchemaPropertyReflectionData, 'schema'>;
6
+ export declare function Property(schema?: SchemaTestable, options?: SchemaPropertyDecoratorOptions): Decorator<'property' | 'accessor'>;
7
+ export declare function Property(options?: SchemaPropertyDecoratorOptions): Decorator<'property' | 'accessor'>;
@@ -1,17 +1,13 @@
1
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';
2
+ import { isDefined } from '../../utils/type-guards.js';
3
+ import { isSchemaTestable } from '../testable.js';
5
4
  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
- });
5
+ export function Property(schemaOrOptions, optionsOrNothing) {
6
+ if (isDefined(optionsOrNothing)) {
7
+ return createSchemaPropertyDecorator({ schema: schemaOrOptions, ...optionsOrNothing });
8
+ }
9
+ if (isSchemaTestable(schemaOrOptions)) {
10
+ return createSchemaPropertyDecorator({ schema: schemaOrOptions });
11
+ }
12
+ return createSchemaPropertyDecorator(schemaOrOptions);
17
13
  }
@@ -1,19 +1,14 @@
1
- import type { OneOrMany } from '../../types.js';
1
+ import type { Decorator } from '../../reflection/types.js';
2
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>;
3
+ import type { ObjectSchemaFactoryFunction, ObjectSchemaOptions } from '../schemas/object.js';
4
+ export type SchemaPropertyDecorator = Decorator<'property' | 'accessor'>;
5
+ export type SchemaTypeReflectionData = Partial<Pick<ObjectSchemaOptions, 'mask' | 'unknownProperties' | 'unknownPropertiesKey'>> & {
6
+ schema?: SchemaTestable;
7
+ factory?: ObjectSchemaFactoryFunction<any>;
7
8
  };
8
9
  export type SchemaPropertyReflectionData = {
9
- schema?: OneOrMany<SchemaTestable>;
10
+ schema?: SchemaTestable;
10
11
  array?: boolean;
11
12
  optional?: boolean;
12
13
  nullable?: boolean;
13
- coerce?: boolean;
14
- coercers?: readonly SchemaValueCoercer[];
15
- transformers?: readonly SchemaValueTransformer[];
16
- arrayConstraints?: readonly SchemaArrayConstraint[];
17
- valueConstraints?: readonly SchemaValueConstraint[];
18
14
  };
19
- export type PropertyOptions = Partial<ValueSchema<any>>;
@@ -1,15 +1,2 @@
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;
1
+ import type { SchemaPropertyDecorator, SchemaPropertyReflectionData } from './types.js';
2
+ export declare function createSchemaPropertyDecorator(data?: SchemaPropertyReflectionData): SchemaPropertyDecorator;
@@ -1,48 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/naming-convention */
2
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) {
3
+ export function createSchemaPropertyDecorator(data) {
9
4
  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
- }
5
+ data: { schema: data },
6
+ mergeData: true
21
7
  });
22
8
  }
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
- }
package/schema/index.d.ts CHANGED
@@ -1,10 +1,5 @@
1
- export * from './array-constraints/index.js';
2
- export * from './coercers/index.js';
3
- export * from './constraints/index.js';
4
1
  export * from './decorators/index.js';
5
2
  export * from './schema.error.js';
6
3
  export * from './schema.js';
7
4
  export * from './schemas/index.js';
8
- export * from './transformers/index.js';
9
- export * from './types/index.js';
10
- export * from './utils/index.js';
5
+ export * from './testable.js';
package/schema/index.js CHANGED
@@ -1,10 +1,19 @@
1
- export * from './array-constraints/index.js';
2
- export * from './coercers/index.js';
3
- export * from './constraints/index.js';
1
+ import { Schema } from './schema.js';
2
+ import { schemaTestableToSchema } from './testable.js';
4
3
  export * from './decorators/index.js';
5
4
  export * from './schema.error.js';
6
5
  export * from './schema.js';
7
6
  export * from './schemas/index.js';
8
- export * from './transformers/index.js';
9
- export * from './types/index.js';
10
- export * from './utils/index.js';
7
+ export * from './testable.js';
8
+ Schema.test = function test(schema, value, options) {
9
+ return schemaTestableToSchema(schema).test(value, options);
10
+ };
11
+ Schema.validate = function validate(schema, value, options) {
12
+ return schemaTestableToSchema(schema).validate(value, options);
13
+ };
14
+ Schema.assert = function assert(schema, value, options) {
15
+ schemaTestableToSchema(schema).assert(value, options);
16
+ };
17
+ Schema.parse = function parse(schema, value, options) {
18
+ return schemaTestableToSchema(schema).parse(value, options);
19
+ };
@@ -1,15 +1,90 @@
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;
1
+ import type { IsEqual, Or } from 'type-fest';
2
+ import { JsonPath } from '../json-path/json-path.js';
3
+ import type { AbstractConstructor } from '../types.js';
4
+ import type { SchemaError } from './schema.error.js';
5
+ import type { Coercible, Maskable } from './types.js';
6
+ export type SchemaTestOptions = Coercible & Maskable & {
7
+ /**
8
+ * Use fast errors which can improve performance a lot but misses detailed stack traces.
9
+ */
10
+ fastErrors?: boolean;
13
11
  };
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;
12
+ export type SchemaTestResult<T> = {
13
+ valid: true;
14
+ value: T;
15
+ error?: undefined;
16
+ } | {
17
+ valid: false;
18
+ value?: undefined;
19
+ error: SchemaError;
20
+ };
21
+ type NormalizePrimitiveToConstructor<T> = Or<IsEqual<T, string>, IsEqual<T, String>> extends true ? typeof String : Or<IsEqual<T, number>, IsEqual<T, Number>> extends true ? typeof Number : Or<IsEqual<T, boolean>, IsEqual<T, Boolean>> extends true ? typeof Boolean : Or<IsEqual<T, bigint>, IsEqual<T, BigInt>> extends true ? typeof BigInt : Or<IsEqual<T, symbol>, IsEqual<T, Symbol>> extends true ? typeof Symbol : never;
22
+ type NormalizeConstructorToPrimitve<T> = T extends String ? string : T extends Number ? number : T extends Boolean ? boolean : T extends BigInt ? bigint : T extends Symbol ? symbol : T;
23
+ export type SchemaTestable<T = unknown> = Schema<T> | AbstractConstructor<T> | NormalizePrimitiveToConstructor<T>;
24
+ export type SchemaOutput<T extends SchemaTestable> = T extends SchemaTestable<infer U> ? NormalizeConstructorToPrimitve<U> : never;
25
+ export declare abstract class Schema<T = unknown> {
26
+ /**
27
+ * Test an unknown value to see whether it corresponds to the schema.
28
+ * @param schema schema to test against
29
+ * @param value value to test
30
+ * @param options validation options
31
+ * @returns test result
32
+ */
33
+ static readonly test: <T>(schema: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions) => SchemaTestResult<T>;
34
+ /**
35
+ * Validate an unknown value to see whether it corresponds to the schema.
36
+ * @param schema schema to validate against
37
+ * @param value value to validate
38
+ * @param options validation options
39
+ * @returns validation result
40
+ */
41
+ static readonly validate: <T>(schema: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions) => boolean;
42
+ /**
43
+ * Asserts an unknown value to be valid according to the schema.
44
+ * @param schema schema to validate against
45
+ * @param value value to validate
46
+ * @param options validation options
47
+ */
48
+ static readonly assert: <T>(schema: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions) => asserts value is T;
49
+ /**
50
+ * Parse an unknown value to comply with the scheme.
51
+ * @param schema schema to validate against
52
+ * @param value value to validate
53
+ * @param options validation options
54
+ * @returns validation result
55
+ */
56
+ static readonly parse: <T>(schema: SchemaTestable<T>, value: unknown, options?: SchemaTestOptions) => T;
57
+ /**
58
+ * Test an unknown value to see whether it corresponds to the schema.
59
+ * @param schema schema to test against
60
+ * @param value value to test
61
+ * @param options validation options
62
+ * @returns test result with either the value or validation error
63
+ */
64
+ test(value: any, options?: SchemaTestOptions): SchemaTestResult<T>;
65
+ /**
66
+ * Validate an unknown value to see whether it corresponds to the schema.
67
+ * @param schema schema to validate against
68
+ * @param value value to validate
69
+ * @param options validation options
70
+ * @returns validation result. Throws if validation fails
71
+ */
72
+ validate(value: any, options?: SchemaTestOptions): boolean;
73
+ /**
74
+ * Asserts an unknown value to be valid according to the schema.
75
+ * @param schema schema to validate against
76
+ * @param value value to validate
77
+ * @param options validation options
78
+ */
79
+ assert(value: any, options?: SchemaTestOptions): asserts value is T;
80
+ /**
81
+ * Parse an unknown value to comply with the scheme.
82
+ * @param schema schema to parse against
83
+ * @param value value to parse
84
+ * @param options validation options
85
+ * @returns parsed value
86
+ */
87
+ parse(value: any, options?: SchemaTestOptions): T;
88
+ abstract _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T>;
89
+ }
90
+ export {};
@@ -1,9 +1,7 @@
1
- import type { CustomErrorOptions } from '../errors/custom.error.js';
2
- import { CustomError } from '../errors/custom.error.js';
1
+ import { CustomError, type CustomErrorOptions } from '../errors/custom.error.js';
3
2
  import type { JsonPath } from '../json-path/index.js';
4
- import type { OneOrMany, TypedOmit, UndefinableJson } from '../types.js';
3
+ import type { AbstractConstructor, OneOrMany, TypedOmit, UndefinableJson } from '../types.js';
5
4
  import type { ErrorExtraInfo } from '../utils/format-error.js';
6
- import type { ValueType } from './types/index.js';
7
5
  export type SchemaErrorOptions = Pick<CustomErrorOptions, 'fast'> & {
8
6
  path: string | JsonPath;
9
7
  details?: UndefinableJson;
@@ -16,10 +14,10 @@ export declare class SchemaError extends CustomError implements ErrorExtraInfo {
16
14
  readonly details?: UndefinableJson;
17
15
  readonly inner?: OneOrMany<SchemaError>;
18
16
  constructor(message: string, options: SchemaErrorOptions, cause?: any);
19
- static expectedButGot(expected: OneOrMany<string | ValueType>, got: string | ValueType, path: string | JsonPath, options: TypedOmit<SchemaErrorOptions, 'path'> & {
17
+ static expectedButGot(expected: OneOrMany<string | AbstractConstructor>, got: string, path: string | JsonPath, options?: TypedOmit<SchemaErrorOptions, 'path'> & {
20
18
  customMessage?: string;
21
19
  }): SchemaError;
22
- static couldNotCoerce(expected: OneOrMany<string | ValueType>, got: string | ValueType, path: string | JsonPath, options: TypedOmit<SchemaErrorOptions, 'path'> & {
20
+ static couldNotCoerce(expected: OneOrMany<string | AbstractConstructor>, got: string, path: string | JsonPath, options: TypedOmit<SchemaErrorOptions, 'path'> & {
23
21
  customMessage?: string;
24
22
  }): SchemaError;
25
23
  getExtraInfo(includeMessage?: boolean): UndefinableJson | undefined;
@@ -1,8 +1,6 @@
1
1
  import { CustomError } from '../errors/custom.error.js';
2
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';
3
+ import { isArray, isDefined, isFunction, isNotNullOrUndefined, isString } from '../utils/type-guards.js';
6
4
  export class SchemaError extends CustomError {
7
5
  static errorName = 'SchemaError';
8
6
  path;
@@ -24,24 +22,22 @@ export class SchemaError extends CustomError {
24
22
  }
25
23
  // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
26
24
  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);
25
+ const expectedNames = toArray(expected).map((e) => isFunction(e) ? e.name : e);
29
26
  const expectedString = expectedNames.length == 1
30
27
  ? expectedNames[0]
31
28
  : `(${expectedNames.join(' | ')})`;
32
- const customMessage = isDefined(options.customMessage) ? `: ${options.customMessage}` : '.';
33
- const message = `Expected ${expectedString} but got ${gotName}${customMessage}`;
29
+ const customMessage = isDefined(options?.customMessage) ? `: ${options.customMessage}` : '.';
30
+ const message = `Expected ${expectedString} but got ${got}${customMessage}`;
34
31
  return new SchemaError(message, { path, ...options });
35
32
  }
36
33
  // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
37
34
  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);
35
+ const expectedNames = toArray(expected).map((e) => isFunction(e) ? e.name : e);
40
36
  const expectedString = expectedNames.length == 1
41
37
  ? expectedNames[0]
42
38
  : `[${expectedNames.join(', ')}]`;
43
39
  const customMessageString = isDefined(options.customMessage) ? `: ${options.customMessage}` : '.';
44
- const errorMessage = `Could not coerce ${gotText} to ${expectedString}${customMessageString}`;
40
+ const errorMessage = `Could not coerce ${got} to ${expectedString}${customMessageString}`;
45
41
  return new SchemaError(errorMessage, { path, ...options });
46
42
  }
47
43
  getExtraInfo(includeMessage = false) {