@ddd-ts-kit/validation 0.12.0 → 0.13.0

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 (91) hide show
  1. package/README.md +89 -7
  2. package/dist/class-validator/class-schema.d.ts +5 -0
  3. package/dist/class-validator/class-schema.d.ts.map +1 -1
  4. package/dist/class-validator/class-schema.js +1 -1
  5. package/dist/class-validator/class-schema.js.map +1 -1
  6. package/dist/class-validator/copy-class-metadata.d.ts +9 -0
  7. package/dist/class-validator/copy-class-metadata.d.ts.map +1 -0
  8. package/dist/class-validator/copy-class-metadata.js +52 -0
  9. package/dist/class-validator/copy-class-metadata.js.map +1 -0
  10. package/dist/class-validator/declared-properties.d.ts +4 -0
  11. package/dist/class-validator/declared-properties.d.ts.map +1 -0
  12. package/dist/class-validator/declared-properties.js +8 -0
  13. package/dist/class-validator/declared-properties.js.map +1 -0
  14. package/dist/class-validator/derived-class.d.ts +8 -0
  15. package/dist/class-validator/derived-class.d.ts.map +1 -0
  16. package/dist/class-validator/derived-class.js +25 -0
  17. package/dist/class-validator/derived-class.js.map +1 -0
  18. package/dist/class-validator/omit-class.d.ts +4 -0
  19. package/dist/class-validator/omit-class.d.ts.map +1 -0
  20. package/dist/class-validator/omit-class.js +6 -0
  21. package/dist/class-validator/omit-class.js.map +1 -0
  22. package/dist/class-validator/partial-class.d.ts +7 -0
  23. package/dist/class-validator/partial-class.d.ts.map +1 -0
  24. package/dist/class-validator/partial-class.js +17 -0
  25. package/dist/class-validator/partial-class.js.map +1 -0
  26. package/dist/class-validator/pick-class.d.ts +7 -0
  27. package/dist/class-validator/pick-class.d.ts.map +1 -0
  28. package/dist/class-validator/pick-class.js +16 -0
  29. package/dist/class-validator/pick-class.js.map +1 -0
  30. package/dist/class-validator/prototype-of.d.ts +4 -0
  31. package/dist/class-validator/prototype-of.d.ts.map +1 -0
  32. package/dist/class-validator/prototype-of.js +6 -0
  33. package/dist/class-validator/prototype-of.js.map +1 -0
  34. package/dist/class-validator/self-contained-schema.d.ts +6 -2
  35. package/dist/class-validator/self-contained-schema.d.ts.map +1 -1
  36. package/dist/class-validator/self-contained-schema.js +24 -9
  37. package/dist/class-validator/self-contained-schema.js.map +1 -1
  38. package/dist/class-validator.d.ts +3 -0
  39. package/dist/class-validator.d.ts.map +1 -1
  40. package/dist/class-validator.js +3 -0
  41. package/dist/class-validator.js.map +1 -1
  42. package/dist/index.d.ts +2 -0
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +2 -0
  45. package/dist/index.js.map +1 -1
  46. package/dist/shared/definitions-pointer.d.ts +3 -0
  47. package/dist/shared/definitions-pointer.d.ts.map +1 -0
  48. package/dist/shared/definitions-pointer.js +3 -0
  49. package/dist/shared/definitions-pointer.js.map +1 -0
  50. package/dist/shared/is-record.d.ts +2 -0
  51. package/dist/shared/is-record.d.ts.map +1 -0
  52. package/dist/shared/is-record.js +4 -0
  53. package/dist/shared/is-record.js.map +1 -0
  54. package/dist/shared/shared-name-of.d.ts +8 -0
  55. package/dist/shared/shared-name-of.d.ts.map +1 -0
  56. package/dist/shared/shared-name-of.js +17 -0
  57. package/dist/shared/shared-name-of.js.map +1 -0
  58. package/dist/shared/shared-schema.d.ts +9 -0
  59. package/dist/shared/shared-schema.d.ts.map +1 -0
  60. package/dist/shared/shared-schema.js +36 -0
  61. package/dist/shared/shared-schema.js.map +1 -0
  62. package/dist/standard-schema/standard-schema-options.d.ts +8 -0
  63. package/dist/standard-schema/standard-schema-options.d.ts.map +1 -0
  64. package/dist/standard-schema/standard-schema-options.js +2 -0
  65. package/dist/standard-schema/standard-schema-options.js.map +1 -0
  66. package/dist/standard-schema/standard-schema-v1.d.ts +14 -1
  67. package/dist/standard-schema/standard-schema-v1.d.ts.map +1 -1
  68. package/dist/standard-schema/standard-schema.d.ts +6 -3
  69. package/dist/standard-schema/standard-schema.d.ts.map +1 -1
  70. package/dist/standard-schema/standard-schema.js +18 -4
  71. package/dist/standard-schema/standard-schema.js.map +1 -1
  72. package/dist/standard-schema.d.ts +2 -1
  73. package/dist/standard-schema.d.ts.map +1 -1
  74. package/dist/standard-schema.js.map +1 -1
  75. package/dist/typebox/definitions-of.d.ts +9 -0
  76. package/dist/typebox/definitions-of.d.ts.map +1 -0
  77. package/dist/typebox/definitions-of.js +56 -0
  78. package/dist/typebox/definitions-of.js.map +1 -0
  79. package/dist/typebox/typebox-schema-options.d.ts +8 -0
  80. package/dist/typebox/typebox-schema-options.d.ts.map +1 -0
  81. package/dist/typebox/typebox-schema-options.js +2 -0
  82. package/dist/typebox/typebox-schema-options.js.map +1 -0
  83. package/dist/typebox/typebox-schema.d.ts +11 -0
  84. package/dist/typebox/typebox-schema.d.ts.map +1 -0
  85. package/dist/typebox/typebox-schema.js +50 -0
  86. package/dist/typebox/typebox-schema.js.map +1 -0
  87. package/dist/typebox.d.ts +3 -0
  88. package/dist/typebox.d.ts.map +1 -0
  89. package/dist/typebox.js +2 -0
  90. package/dist/typebox.js.map +1 -0
  91. package/package.json +18 -9
@@ -0,0 +1,4 @@
1
+ export function isRecord(value) {
2
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
3
+ }
4
+ //# sourceMappingURL=is-record.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-record.js","sourceRoot":"","sources":["../../src/shared/is-record.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { JsonSchema } from '../json-schema.js';
2
+ /**
3
+ * The name a schema is shared under, when its JSON Schema is a reference to one of its own
4
+ * definitions: the form `sharedSchema`, `classSchema(Dto, { shared: true })` and a zod schema
5
+ * registered with `.meta({ id })` all take.
6
+ */
7
+ export declare function sharedNameOf(jsonSchema: JsonSchema): string | undefined;
8
+ //# sourceMappingURL=shared-name-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-name-of.d.ts","sourceRoot":"","sources":["../../src/shared/shared-name-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAWvE"}
@@ -0,0 +1,17 @@
1
+ import { DEFINITIONS_POINTER } from './definitions-pointer.js';
2
+ import { isRecord } from './is-record.js';
3
+ /**
4
+ * The name a schema is shared under, when its JSON Schema is a reference to one of its own
5
+ * definitions: the form `sharedSchema`, `classSchema(Dto, { shared: true })` and a zod schema
6
+ * registered with `.meta({ id })` all take.
7
+ */
8
+ export function sharedNameOf(jsonSchema) {
9
+ const reference = jsonSchema['$ref'];
10
+ const definitions = jsonSchema['$defs'];
11
+ if (typeof reference !== 'string' || !reference.startsWith(DEFINITIONS_POINTER) || !isRecord(definitions)) {
12
+ return undefined;
13
+ }
14
+ const name = reference.slice(DEFINITIONS_POINTER.length);
15
+ return definitions[name] === undefined ? undefined : name;
16
+ }
17
+ //# sourceMappingURL=shared-name-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-name-of.js","sourceRoot":"","sources":["../../src/shared/shared-name-of.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,UAAsB;IACjD,MAAM,SAAS,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IAExC,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QAC1G,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;IAEzD,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { Schema } from '../schema.js';
2
+ /**
3
+ * A schema documented once, under `name`: its JSON Schema becomes a reference to its own
4
+ * definition, so the OpenAPI generator writes it in `components.schemas` and every route that
5
+ * uses it, whole or inside another schema, points at it with `$ref` instead of a copy.
6
+ * Validation is unchanged: `parse` is the one of the schema given.
7
+ */
8
+ export declare function sharedSchema<T>(name: string, schema: Schema<T>): Schema<T>;
9
+ //# sourceMappingURL=shared-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-schema.d.ts","sourceRoot":"","sources":["../../src/shared/shared-schema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAQ3C;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAW1E"}
@@ -0,0 +1,36 @@
1
+ import { DeclarationError } from '@ddd-ts-kit/kernel/declarations';
2
+ import { DEFINITIONS_POINTER } from './definitions-pointer.js';
3
+ import { isRecord } from './is-record.js';
4
+ import { sharedNameOf } from './shared-name-of.js';
5
+ /** What OpenAPI accepts as the key of a component, starting with a letter. */
6
+ const COMPONENT_NAME = /^[A-Za-z][A-Za-z0-9._-]*$/;
7
+ /**
8
+ * A schema documented once, under `name`: its JSON Schema becomes a reference to its own
9
+ * definition, so the OpenAPI generator writes it in `components.schemas` and every route that
10
+ * uses it, whole or inside another schema, points at it with `$ref` instead of a copy.
11
+ * Validation is unchanged: `parse` is the one of the schema given.
12
+ */
13
+ export function sharedSchema(name, schema) {
14
+ if (!COMPONENT_NAME.test(name)) {
15
+ throw new DeclarationError(`"${name}" cannot name a shared schema: a name starts with a letter and holds only letters, digits, ".", "-" and "_", as an OpenAPI component does.`);
16
+ }
17
+ return {
18
+ parse: (input) => schema.parse(input),
19
+ toJsonSchema: () => asShared(name, schema.toJsonSchema()),
20
+ };
21
+ }
22
+ function asShared(name, jsonSchema) {
23
+ if (sharedNameOf(jsonSchema) === name) {
24
+ return jsonSchema;
25
+ }
26
+ const { $schema: dialect, $defs, definitions, ...own } = jsonSchema;
27
+ return {
28
+ ...(dialect === undefined ? {} : { $schema: dialect }),
29
+ $ref: `${DEFINITIONS_POINTER}${name}`,
30
+ $defs: { ...recordOf(definitions), ...recordOf($defs), [name]: own },
31
+ };
32
+ }
33
+ function recordOf(value) {
34
+ return isRecord(value) ? value : {};
35
+ }
36
+ //# sourceMappingURL=shared-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-schema.js","sourceRoot":"","sources":["../../src/shared/shared-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAGnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,8EAA8E;AAC9E,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAI,IAAY,EAAE,MAAiB;IAC7D,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,gBAAgB,CACxB,IAAI,IAAI,4IAA4I,CACrJ,CAAC;IACJ,CAAC;IAED,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;QACrC,YAAY,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,YAAY,EAAE,CAAC;KAC1D,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAC,IAAY,EAAE,UAAsB;IACpD,IAAI,YAAY,CAAC,UAAU,CAAC,KAAK,IAAI,EAAE,CAAC;QACtC,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,GAAG,EAAE,GAAG,UAAU,CAAC;IAEpE,OAAO;QACL,GAAG,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;QACtD,IAAI,EAAE,GAAG,mBAAmB,GAAG,IAAI,EAAE;QACrC,KAAK,EAAE,EAAE,GAAG,QAAQ,CAAC,WAAW,CAAC,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE;KACrE,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AACtC,CAAC"}
@@ -0,0 +1,8 @@
1
+ export interface StandardSchemaOptions {
2
+ /**
3
+ * Which side of the schema the JSON Schema describes, when the library converts it itself
4
+ * (Standard JSON Schema): `input` (default) for what a client sends, `output` for a response body.
5
+ */
6
+ readonly io?: 'input' | 'output';
7
+ }
8
+ //# sourceMappingURL=standard-schema-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standard-schema-options.d.ts","sourceRoot":"","sources":["../../src/standard-schema/standard-schema-options.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;CAClC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=standard-schema-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standard-schema-options.js","sourceRoot":"","sources":["../../src/standard-schema/standard-schema-options.ts"],"names":[],"mappings":""}
@@ -1,4 +1,7 @@
1
- /** The subset of the Standard Schema v1 specification this adapter relies on. */
1
+ /**
2
+ * The subset of the Standard Schema v1 specification this adapter relies on, with the optional
3
+ * converter of its companion specification, Standard JSON Schema v1.
4
+ */
2
5
  export interface StandardSchemaV1<Input = unknown, Output = Input> {
3
6
  readonly '~standard': {
4
7
  readonly version: 1;
@@ -8,6 +11,7 @@ export interface StandardSchemaV1<Input = unknown, Output = Input> {
8
11
  readonly input: Input;
9
12
  readonly output: Output;
10
13
  } | undefined;
14
+ readonly jsonSchema?: StandardJsonSchemaConverter | undefined;
11
15
  };
12
16
  }
13
17
  export type StandardSchemaResult<Output> = {
@@ -22,4 +26,13 @@ export interface StandardSchemaIssue {
22
26
  readonly key: PropertyKey;
23
27
  })[] | undefined;
24
28
  }
29
+ /** What a library that implements Standard JSON Schema v1 converts its schemas with. */
30
+ export interface StandardJsonSchemaConverter {
31
+ readonly input: (options: {
32
+ readonly target: string;
33
+ }) => Record<string, unknown>;
34
+ readonly output: (options: {
35
+ readonly target: string;
36
+ }) => Record<string, unknown>;
37
+ }
25
38
  //# sourceMappingURL=standard-schema-v1.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"standard-schema-v1.d.ts","sourceRoot":"","sources":["../../src/standard-schema/standard-schema-v1.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC/D,QAAQ,CAAC,WAAW,EAAE;QACpB,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QAC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QAC5G,QAAQ,CAAC,KAAK,CAAC,EAAE;YAAE,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;YAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;SAAE,GAAG,SAAS,CAAC;QAChF,QAAQ,CAAC,UAAU,CAAC,EAAE,2BAA2B,GAAG,SAAS,CAAC;KAC/D,CAAC;CACH;AAED,MAAM,MAAM,oBAAoB,CAAC,MAAM,IACnC;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,mBAAmB,EAAE,CAAA;CAAE,CAAC;AAExD,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,WAAW,GAAG;QAAE,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAA;KAAE,CAAC,EAAE,GAAG,SAAS,CAAC;CACtF;AAED,wFAAwF;AACxF,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClF,QAAQ,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpF"}
@@ -1,10 +1,13 @@
1
1
  import type { JsonSchema } from '../json-schema.js';
2
2
  import type { Schema } from '../schema.js';
3
3
  import type { InferStandardOutput } from './infer-standard-output.js';
4
+ import type { StandardSchemaOptions } from './standard-schema-options.js';
4
5
  import type { StandardSchemaV1 } from './standard-schema-v1.js';
5
6
  /**
6
- * Standard Schema has no JSON Schema export, so the contract side is supplied by the caller
7
- * and defaults to `{}` (any value).
7
+ * Validates with any Standard Schema library. The JSON Schema of the contract is the one given;
8
+ * without it, the one the library converts itself when it implements Standard JSON Schema
9
+ * (verified with zod 4), else `{}` (any value). A schema the library cannot write as JSON Schema
10
+ * (a date, a custom check) is documented `{}` as well, never a failure of the contract.
8
11
  */
9
- export declare function standardSchema<S extends StandardSchemaV1>(schema: S, jsonSchema?: JsonSchema): Schema<InferStandardOutput<S>>;
12
+ export declare function standardSchema<S extends StandardSchemaV1>(schema: S, jsonSchema?: JsonSchema, options?: StandardSchemaOptions): Schema<InferStandardOutput<S>>;
10
13
  //# sourceMappingURL=standard-schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../../src/standard-schema/standard-schema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,KAAK,EAA6C,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3G;;;GAGG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,gBAAgB,EACvD,MAAM,EAAE,CAAC,EACT,UAAU,GAAE,UAAe,GAC1B,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAkBhC"}
1
+ {"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../../src/standard-schema/standard-schema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,KAAK,EAA6C,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3G;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,gBAAgB,EACvD,MAAM,EAAE,CAAC,EACT,UAAU,CAAC,EAAE,UAAU,EACvB,OAAO,GAAE,qBAA0B,GAClC,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAkBhC"}
@@ -1,10 +1,12 @@
1
1
  import { err, ok } from '@ddd-ts-kit/kernel';
2
2
  import { formatPath } from '../format-path.js';
3
3
  /**
4
- * Standard Schema has no JSON Schema export, so the contract side is supplied by the caller
5
- * and defaults to `{}` (any value).
4
+ * Validates with any Standard Schema library. The JSON Schema of the contract is the one given;
5
+ * without it, the one the library converts itself when it implements Standard JSON Schema
6
+ * (verified with zod 4), else `{}` (any value). A schema the library cannot write as JSON Schema
7
+ * (a date, a custom check) is documented `{}` as well, never a failure of the contract.
6
8
  */
7
- export function standardSchema(schema, jsonSchema = {}) {
9
+ export function standardSchema(schema, jsonSchema, options = {}) {
8
10
  return {
9
11
  parse(input) {
10
12
  const result = schema['~standard'].validate(input);
@@ -15,9 +17,21 @@ export function standardSchema(schema, jsonSchema = {}) {
15
17
  }
16
18
  return verdictOf(result);
17
19
  },
18
- toJsonSchema: () => jsonSchema,
20
+ toJsonSchema: () => jsonSchema ?? converted(schema, options),
19
21
  };
20
22
  }
23
+ function converted(schema, { io = 'input' }) {
24
+ const converter = schema['~standard'].jsonSchema;
25
+ if (converter === undefined) {
26
+ return {};
27
+ }
28
+ try {
29
+ return converter[io]({ target: 'draft-2020-12' });
30
+ }
31
+ catch {
32
+ return {};
33
+ }
34
+ }
21
35
  function verdictOf(result) {
22
36
  if (result.issues === undefined) {
23
37
  return ok(result.value);
@@ -1 +1 @@
1
- {"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../../src/standard-schema/standard-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE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,CAAC;AACH,CAAC;AAED,SAAS,SAAS,CAAS,MAAoC;IAC7D,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;QAChC,OAAO,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED,OAAO,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,OAAO,CAAC,KAA0B;IACzC,MAAM,QAAQ,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;IAE5G,sDAAsD;IACtD,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;AACjF,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export type { InferStandardOutput } from './standard-schema/infer-standard-output.js';
2
- export type { StandardSchemaIssue, StandardSchemaResult, StandardSchemaV1, } from './standard-schema/standard-schema-v1.js';
2
+ export type { StandardSchemaOptions } from './standard-schema/standard-schema-options.js';
3
+ export type { StandardJsonSchemaConverter, StandardSchemaIssue, StandardSchemaResult, StandardSchemaV1, } from './standard-schema/standard-schema-v1.js';
3
4
  export { standardSchema } from './standard-schema/standard-schema.js';
4
5
  //# sourceMappingURL=standard-schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAC;AACtF,YAAY,EACV,mBAAmB,EACnB,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC"}
1
+ {"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAC;AACtF,YAAY,EAAE,qBAAqB,EAAE,MAAM,8CAA8C,CAAC;AAC1F,YAAY,EACV,2BAA2B,EAC3B,mBAAmB,EACnB,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC"}
1
+ {"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { JsonSchema } from '../json-schema.js';
2
+ /**
3
+ * The JSON Schema of a TypeBox schema, its shared parts moved under `$defs`: a schema built with
4
+ * an `$id` (`Type.Object({...}, { $id: 'Slot' })`) is written once there and referenced wherever
5
+ * it is nested. A schema that has an `$id` itself is a reference to its own definition: shared.
6
+ * Two different schemas under one `$id` are refused: one of them would be documented as the other.
7
+ */
8
+ export declare function definitionsOf(type: unknown): JsonSchema;
9
+ //# sourceMappingURL=definitions-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions-of.d.ts","sourceRoot":"","sources":["../../src/typebox/definitions-of.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAYpD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAWvD"}
@@ -0,0 +1,56 @@
1
+ import { DeclarationError } from '@ddd-ts-kit/kernel/declarations';
2
+ import { DEFINITIONS_POINTER } from '../shared/definitions-pointer.js';
3
+ import { isRecord } from '../shared/is-record.js';
4
+ /** Keywords whose value is data, never a schema: an `$id` written there is a value. */
5
+ const VALUE_KEYWORDS = new Set(['default', 'const', 'enum', 'examples']);
6
+ /** Keywords whose value maps names to schemas: the names are kept, the schemas are read. */
7
+ const SCHEMA_MAPS = new Set(['properties', 'patternProperties', 'dependentSchemas', '$defs']);
8
+ /**
9
+ * The JSON Schema of a TypeBox schema, its shared parts moved under `$defs`: a schema built with
10
+ * an `$id` (`Type.Object({...}, { $id: 'Slot' })`) is written once there and referenced wherever
11
+ * it is nested. A schema that has an `$id` itself is a reference to its own definition: shared.
12
+ * Two different schemas under one `$id` are refused: one of them would be documented as the other.
13
+ */
14
+ export function definitionsOf(type) {
15
+ const definitions = {};
16
+ const described = movedSchema(type, definitions);
17
+ const root = isRecord(described) ? described : {};
18
+ const names = Object.keys(definitions).sort();
19
+ return {
20
+ $schema: 'https://json-schema.org/draft/2020-12/schema',
21
+ ...root,
22
+ ...(names.length > 0 ? { $defs: Object.fromEntries(names.map((name) => [name, definitions[name]])) } : {}),
23
+ };
24
+ }
25
+ function movedSchema(node, definitions) {
26
+ if (!isRecord(node)) {
27
+ return node;
28
+ }
29
+ const { $id: id, ...rest } = node;
30
+ const content = Object.fromEntries(Object.entries(rest).map(([keyword, value]) => [keyword, movedKeyword(keyword, value, definitions)]));
31
+ if (typeof id !== 'string') {
32
+ return content;
33
+ }
34
+ define(id, content, definitions);
35
+ return { $ref: `${DEFINITIONS_POINTER}${id}` };
36
+ }
37
+ function movedKeyword(keyword, value, definitions) {
38
+ if (VALUE_KEYWORDS.has(keyword)) {
39
+ return value;
40
+ }
41
+ if (Array.isArray(value)) {
42
+ return value.map((item) => movedSchema(item, definitions));
43
+ }
44
+ if (SCHEMA_MAPS.has(keyword) && isRecord(value)) {
45
+ return Object.fromEntries(Object.entries(value).map(([name, schema]) => [name, movedSchema(schema, definitions)]));
46
+ }
47
+ return movedSchema(value, definitions);
48
+ }
49
+ function define(id, content, definitions) {
50
+ const known = definitions[id];
51
+ if (known !== undefined && JSON.stringify(known) !== JSON.stringify(content)) {
52
+ throw new DeclarationError(`Two different TypeBox schemas are built with the $id "${id}": the contract would document one as the other. Give each its own $id, or build the shared one once and nest it.`);
53
+ }
54
+ definitions[id] = content;
55
+ }
56
+ //# sourceMappingURL=definitions-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions-of.js","sourceRoot":"","sources":["../../src/typebox/definitions-of.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAEnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAIlD,uFAAuF;AACvF,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;AAEzE,4FAA4F;AAC5F,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,OAAO,CAAC,CAAC,CAAC;AAE9F;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,IAAa;IACzC,MAAM,WAAW,GAAgB,EAAE,CAAC;IACpC,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IACjD,MAAM,IAAI,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAClD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,IAAI,EAAE,CAAC;IAE9C,OAAO;QACL,OAAO,EAAE,8CAA8C;QACvD,GAAG,IAAI;QACP,GAAG,CAAC,KAAK,CAAC,MAAM,G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AAE,WAAW,CAAC,CAAC,CAAC;IAC7D,CAAC;IAED,IAAI,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAChD,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IACrH,CAAC;IAED,OAAO,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,MAAM,CAAC,EAAU,EAAE,OAAmB,EAAE,WAAwB;IACvE,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;IAE9B,IAAI,KAAK,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;QAC7E,MAAM,IAAI,gBAAgB,CACxB,yDAAyD,EAAE,mHAAmH,CAC/K,CAAC;IACJ,CAAC;IAED,WAAW,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,8 @@
1
+ export interface TypeboxSchemaOptions {
2
+ /**
3
+ * Converts text to the declared types before checking (`"3"` to `3`, `"true"` to `true`): for
4
+ * path parameters and query strings, which always arrive as text.
5
+ */
6
+ readonly coerce?: boolean;
7
+ }
8
+ //# sourceMappingURL=typebox-schema-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-schema-options.d.ts","sourceRoot":"","sources":["../../src/typebox/typebox-schema-options.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;CAC3B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=typebox-schema-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-schema-options.js","sourceRoot":"","sources":["../../src/typebox/typebox-schema-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import type { Static, TSchema } from 'typebox';
2
+ import type { Schema } from '../schema.js';
3
+ import type { TypeboxSchemaOptions } from './typebox-schema-options.js';
4
+ /**
5
+ * Validates with a TypeBox schema, compiled once. Like zod, it fills the defaults and drops the
6
+ * properties the schema does not declare before checking; the issue code is the JSON Schema
7
+ * keyword that failed (`required`, `format`, `minLength`...). The contract is the schema itself,
8
+ * TypeBox being JSON Schema.
9
+ */
10
+ export declare function typeboxSchema<T extends TSchema>(type: T, options?: TypeboxSchemaOptions): Schema<Static<T>>;
11
+ //# sourceMappingURL=typebox-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-schema.d.ts","sourceRoot":"","sources":["../../src/typebox/typebox-schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAM/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAG3C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AASxE;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAgB/G"}
@@ -0,0 +1,50 @@
1
+ import { Compile } from 'typebox/compile';
2
+ import { Value } from 'typebox/value';
3
+ import { err, ok } from '@ddd-ts-kit/kernel';
4
+ import { formatPath } from '../format-path.js';
5
+ import { isRecord } from '../shared/is-record.js';
6
+ import { definitionsOf } from './definitions-of.js';
7
+ /**
8
+ * Validates with a TypeBox schema, compiled once. Like zod, it fills the defaults and drops the
9
+ * properties the schema does not declare before checking; the issue code is the JSON Schema
10
+ * keyword that failed (`required`, `format`, `minLength`...). The contract is the schema itself,
11
+ * TypeBox being JSON Schema.
12
+ */
13
+ export function typeboxSchema(type, options = {}) {
14
+ const validator = Compile(type);
15
+ return {
16
+ parse(input) {
17
+ const defaulted = validator.Default(Value.Clone(input));
18
+ const cleaned = validator.Clean(options.coerce === true ? validator.Convert(defaulted) : defaulted);
19
+ if (validator.Check(cleaned)) {
20
+ return ok(cleaned);
21
+ }
22
+ return err(validator.Errors(cleaned).flatMap(issuesOf));
23
+ },
24
+ toJsonSchema: () => definitionsOf(type),
25
+ };
26
+ }
27
+ /**
28
+ * One issue per failed keyword at its path; a missing property is reported by TypeBox on the
29
+ * object that lacks it, so each missing name becomes an issue at its own path, as zod reports it.
30
+ */
31
+ function issuesOf({ keyword, instancePath, params, message }) {
32
+ const segments = instancePath
33
+ .split('/')
34
+ .slice(1)
35
+ .map((segment) => segment.replaceAll('~1', '/').replaceAll('~0', '~'));
36
+ const missing = keyword === 'required' ? requiredPropertiesOf(params) : [];
37
+ if (missing.length === 0) {
38
+ return [{ path: formatPath(segments), code: keyword, message }];
39
+ }
40
+ return missing.map((name) => ({
41
+ path: formatPath([...segments, name]),
42
+ code: keyword,
43
+ message: `must have required property ${name}`,
44
+ }));
45
+ }
46
+ function requiredPropertiesOf(params) {
47
+ const names = isRecord(params) ? params['requiredProperties'] : undefined;
48
+ return Array.isArray(names) ? names.filter((name) => typeof name === 'string') : [];
49
+ }
50
+ //# sourceMappingURL=typebox-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-schema.js","sourceRoot":"","sources":["../../src/typebox/typebox-schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO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zE,MAAM,OAAO,GAAG,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAE3E,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;IAClE,CAAC;IAED,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC5B,IAAI,EAAE,UAAU,CAAC,CAAC,GAAG,QAAQ,EAAE,IAAI,CAAC,CAAC;QACrC,IAAI,EAAE,OAAO;QACb,OAAO,EAAE,+BAA+B,IAAI,EAAE;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,oBAAoB,CAAC,MAAe;IAC3C,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE1E,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAkB,EAAE,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACtG,CAAC"}
@@ -0,0 +1,3 @@
1
+ export type { TypeboxSchemaOptions } from './typebox/typebox-schema-options.js';
2
+ export { typeboxSchema } from './typebox/typebox-schema.js';
3
+ //# sourceMappingURL=typebox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox.d.ts","sourceRoot":"","sources":["../src/typebox.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAChF,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { typeboxSchema } from './typebox/typebox-schema.js';
2
+ //# sourceMappingURL=typebox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox.js","sourceRoot":"","sources":["../src/typebox.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/validation",
3
- "version": "0.12.0",
4
- "description": "One Schema contract for input validation, with adapters for zod, class-validator and Standard Schema.",
3
+ "version": "0.13.0",
4
+ "description": "One Schema contract for input validation, with adapters for zod, class-validator, TypeBox and Standard Schema.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -32,20 +32,25 @@
32
32
  "./standard-schema": {
33
33
  "types": "./dist/standard-schema.d.ts",
34
34
  "default": "./dist/standard-schema.js"
35
+ },
36
+ "./typebox": {
37
+ "types": "./dist/typebox.d.ts",
38
+ "default": "./dist/typebox.js"
35
39
  }
36
40
  },
37
41
  "files": [
38
42
  "dist"
39
43
  ],
40
44
  "dependencies": {
41
- "@ddd-ts-kit/kernel": "0.12.0"
45
+ "@ddd-ts-kit/kernel": "0.13.0"
42
46
  },
43
47
  "peerDependencies": {
44
- "zod": "^4.0.0",
45
- "class-validator": ">=0.14.0",
46
48
  "class-transformer": ">=0.5.0",
49
+ "class-validator": ">=0.14.0",
47
50
  "class-validator-jsonschema": ">=5.0.0",
48
- "reflect-metadata": ">=0.2.0"
51
+ "reflect-metadata": ">=0.2.0",
52
+ "zod": "^4.0.0",
53
+ "typebox": "^1.0.0"
49
54
  },
50
55
  "peerDependenciesMeta": {
51
56
  "zod": {
@@ -62,14 +67,18 @@
62
67
  },
63
68
  "reflect-metadata": {
64
69
  "optional": true
70
+ },
71
+ "typebox": {
72
+ "optional": true
65
73
  }
66
74
  },
67
75
  "devDependencies": {
68
- "zod": "^4.6.5",
69
- "class-validator": "^0.15.1",
70
76
  "class-transformer": "^0.5.1",
77
+ "class-validator": "^0.15.1",
71
78
  "class-validator-jsonschema": "^5.0.0",
72
79
  "reflect-metadata": "^0.2.2",
73
- "valibot": "^1.0.0"
80
+ "typebox": "^1.3.37",
81
+ "valibot": "^1.0.0",
82
+ "zod": "^4.6.5"
74
83
  }
75
84
  }