@nmtjs/type 0.6.5 → 0.7.1

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 (76) hide show
  1. package/dist/index.js +34 -31
  2. package/dist/index.js.map +1 -1
  3. package/dist/temporal.js +0 -1
  4. package/dist/temporal.js.map +1 -1
  5. package/dist/types/any.js +4 -4
  6. package/dist/types/any.js.map +1 -1
  7. package/dist/types/array.js +21 -23
  8. package/dist/types/array.js.map +1 -1
  9. package/dist/types/base.js +65 -66
  10. package/dist/types/base.js.map +1 -1
  11. package/dist/types/boolean.js +4 -4
  12. package/dist/types/boolean.js.map +1 -1
  13. package/dist/types/custom.js +9 -8
  14. package/dist/types/custom.js.map +1 -1
  15. package/dist/types/date.js +8 -13
  16. package/dist/types/date.js.map +1 -1
  17. package/dist/types/enum.js +7 -16
  18. package/dist/types/enum.js.map +1 -1
  19. package/dist/types/literal.js +7 -6
  20. package/dist/types/literal.js.map +1 -1
  21. package/dist/types/never.js +4 -4
  22. package/dist/types/never.js.map +1 -1
  23. package/dist/types/number.js +39 -92
  24. package/dist/types/number.js.map +1 -1
  25. package/dist/types/object.js +42 -31
  26. package/dist/types/object.js.map +1 -1
  27. package/dist/types/string.js +49 -47
  28. package/dist/types/string.js.map +1 -1
  29. package/dist/types/temporal.js +75 -41
  30. package/dist/types/temporal.js.map +1 -1
  31. package/dist/types/union.js +26 -18
  32. package/dist/types/union.js.map +1 -1
  33. package/package.json +7 -19
  34. package/src/index.ts +22 -24
  35. package/src/temporal.ts +1 -1
  36. package/src/types/any.ts +5 -3
  37. package/src/types/array.ts +37 -22
  38. package/src/types/base.ts +149 -81
  39. package/src/types/boolean.ts +5 -3
  40. package/src/types/custom.ts +59 -26
  41. package/src/types/date.ts +13 -17
  42. package/src/types/enum.ts +12 -22
  43. package/src/types/literal.ts +9 -6
  44. package/src/types/never.ts +5 -3
  45. package/src/types/number.ts +43 -90
  46. package/src/types/object.ts +45 -37
  47. package/src/types/string.ts +68 -37
  48. package/src/types/temporal.ts +69 -53
  49. package/src/types/union.ts +54 -50
  50. package/dist/compiler.js +0 -55
  51. package/dist/compiler.js.map +0 -1
  52. package/dist/formats.js +0 -127
  53. package/dist/formats.js.map +0 -1
  54. package/dist/inference.js +0 -1
  55. package/dist/inference.js.map +0 -1
  56. package/dist/parse.js +0 -145
  57. package/dist/parse.js.map +0 -1
  58. package/dist/runtime.js +0 -73
  59. package/dist/runtime.js.map +0 -1
  60. package/dist/schemas/default.js +0 -6
  61. package/dist/schemas/default.js.map +0 -1
  62. package/dist/schemas/discriminated-union.js +0 -9
  63. package/dist/schemas/discriminated-union.js.map +0 -1
  64. package/dist/schemas/nullable.js +0 -11
  65. package/dist/schemas/nullable.js.map +0 -1
  66. package/dist/utils.js +0 -1
  67. package/dist/utils.js.map +0 -1
  68. package/src/compiler.ts +0 -100
  69. package/src/formats.ts +0 -182
  70. package/src/inference.ts +0 -128
  71. package/src/parse.ts +0 -217
  72. package/src/runtime.ts +0 -137
  73. package/src/schemas/default.ts +0 -12
  74. package/src/schemas/discriminated-union.ts +0 -49
  75. package/src/schemas/nullable.ts +0 -20
  76. package/src/utils.ts +0 -24
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/types/union.ts"],"sourcesContent":["import {\n type TIntersect,\n type TObject,\n type TSchema,\n type TUnion,\n Type,\n type UnionToTuple,\n} from '@sinclair/typebox'\nimport type { StaticInputDecode } from '../inference.ts'\nimport {\n DiscriminatedUnion,\n type DiscriminatedUnionProperties,\n type TDiscriminatedUnion,\n} from '../schemas/discriminated-union.ts'\nimport { BaseType, type BaseTypeAny } from './base.ts'\nimport type { ObjectType, ObjectTypeProps } from './object.ts'\n\nexport class UnionType<\n T extends readonly BaseType[] = readonly BaseType[],\n S extends TSchema[] = UnionToTuple<T[number]['schema']>,\n> extends BaseType<TUnion<S>, { options: T }, StaticInputDecode<TUnion<S>>> {\n static factory<\n T extends readonly BaseType[] = readonly BaseType[],\n S extends TSchema[] = UnionToTuple<T[number]['schema']>,\n >(...options: T) {\n return new UnionType<T, S>(\n Type.Union(options.map((t) => t.schema)) as any,\n {\n options,\n },\n )\n }\n}\n\nexport class IntersactionType<\n T extends readonly BaseType[] = readonly BaseType[],\n S extends TSchema[] = UnionToTuple<T[number]['schema']>,\n> extends BaseType<\n TIntersect<S>,\n { options: T },\n StaticInputDecode<TIntersect<S>>\n> {\n static factory<\n T extends readonly BaseType[] = readonly BaseType[],\n S extends TSchema[] = UnionToTuple<T[number]['schema']>,\n >(...options: T) {\n return new IntersactionType<T, S>(\n Type.Intersect(options.map((t) => t.schema)) as any,\n { options },\n )\n }\n}\n\nexport type DiscriminatedUnionOptionType<K extends string> = ObjectType<\n ObjectTypeProps & { [_ in K]: BaseTypeAny }\n>\n\nexport class DiscriminatedUnionType<\n K extends string = string,\n T extends\n readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],\n S extends TObject<DiscriminatedUnionProperties<K>>[] = [],\n> extends BaseType<\n TDiscriminatedUnion<K, S>,\n {\n key: K\n options: T\n },\n StaticInputDecode<TDiscriminatedUnion<K, S>>\n> {\n static factory<\n K extends string,\n T extends readonly DiscriminatedUnionOptionType<K>[],\n //@ts-expect-error\n S extends TObject<DiscriminatedUnionProperties<K>>[] = UnionToTuple<\n T[number]['schema']\n >,\n >(key: K, ...options: T) {\n return new DiscriminatedUnionType<K, T, S>(\n DiscriminatedUnion(key, options.map((t) => t.schema) as any),\n { key, options },\n )\n }\n}\n"],"names":["Type","DiscriminatedUnion","BaseType","UnionType","factory","options","Union","map","t","schema","IntersactionType","Intersect","DiscriminatedUnionType","key"],"mappings":"AAAA,SAKEA,IAAI,QAEC,oBAAmB;AAE1B,SACEC,kBAAkB,QAGb,oCAAmC;AAC1C,SAASC,QAAQ,QAA0B,YAAW;AAGtD,OAAO,MAAMC,kBAGHD;IACR,OAAOE,QAGL,GAAGC,OAAU,EAAE;QACf,OAAO,IAAIF,UACTH,KAAKM,KAAK,CAACD,QAAQE,GAAG,CAAC,CAACC,IAAMA,EAAEC,MAAM,IACtC;YACEJ;QACF;IAEJ;AACF;AAEA,OAAO,MAAMK,yBAGHR;IAKR,OAAOE,QAGL,GAAGC,OAAU,EAAE;QACf,OAAO,IAAIK,iBACTV,KAAKW,SAAS,CAACN,QAAQE,GAAG,CAAC,CAACC,IAAMA,EAAEC,MAAM,IAC1C;YAAEJ;QAAQ;IAEd;AACF;AAMA,OAAO,MAAMO,+BAKHV;IAQR,OAAOE,QAOLS,GAAM,EAAE,GAAGR,OAAU,EAAE;QACvB,OAAO,IAAIO,uBACTX,mBAAmBY,KAAKR,QAAQE,GAAG,CAAC,CAACC,IAAMA,EAAEC,MAAM,IACnD;YAAEI;YAAKR;QAAQ;IAEnB;AACF"}
1
+ {"mappings":"AAAA,SAEE,oBACA,cACA,aAIK,WAAW;AAClB,SAAS,gBAAkC,WAAW;AAItD,OAAO,MAAM,kBAEH,SAIR;CACA,OAAO,QACL,GAAG,SACH;AACA,SAAO,IAAI,UAAa;GACtB,gBAAgB,MAAM,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CAAC;GAC3D,gBAAgB,MAAM,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CAAC;GAC3D,OAAO,EAAE,QAAS;EACnB;CACF;AACF;AAED,OAAO,MAAM,yBAEH,SAIR;CACA,OAAO,QAEL,GAAG,SAAY;EACf,MAAM,CAAC,OAAO,OAAO,GAAG;AACxB,SAAO,IAAI,iBAAoB;GAC7B,gBAAgB,aAAa,MAAM,gBAAgB,OAAO,eAAe;GACzE,gBAAgB,aAAa,MAAM,gBAAgB,OAAO,eAAe;GACzE,OAAO,EAAE,QAAS;EACnB;CACF;AACF;AAYD,OAAO,MAAM,+BAIH,SAOR;CACA,OAAO,QAILA,KAAQ,GAAG,SAAY;AACvB,SAAO,IAAI,uBAA6B;GACtC,gBAAgB,mBACd,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CACrC;GACD,gBAAgB,mBACd,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CACrC;GACD,OAAO;IAAE;IAAK;GAAS;EACxB;CACF;AACF","names":["key: K"],"sources":["src/types/union.ts"],"sourcesContent":["import {\n type core,\n discriminatedUnion,\n intersection,\n union,\n type ZodMiniDiscriminatedUnion,\n type ZodMiniIntersection,\n type ZodMiniUnion,\n} from '@zod/mini'\nimport { BaseType, type BaseTypeAny } from './base.ts'\nimport type { LiteralType } from './literal.ts'\nimport type { ObjectType, ObjectTypeProps } from './object.ts'\n\nexport class UnionType<\n T extends readonly BaseType[] = readonly BaseType[],\n> extends BaseType<\n ZodMiniUnion<core.utils.Flatten<T[number]['encodedZodType'][]>>,\n ZodMiniUnion<core.utils.Flatten<T[number]['decodedZodType'][]>>,\n { options: T }\n> {\n static factory<T extends readonly BaseType[] = readonly BaseType[]>(\n ...options: T\n ) {\n return new UnionType<T>({\n encodedZodType: union(options.map((t) => t.encodedZodType)),\n decodedZodType: union(options.map((t) => t.decodedZodType)),\n props: { options },\n })\n }\n}\n\nexport class IntersactionType<\n T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],\n> extends BaseType<\n ZodMiniIntersection<T[0]['encodedZodType'], T[1]['encodedZodType']>,\n ZodMiniIntersection<T[0]['decodedZodType'], T[1]['decodedZodType']>,\n { options: T }\n> {\n static factory<\n T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],\n >(...options: T) {\n const [first, second] = options\n return new IntersactionType<T>({\n encodedZodType: intersection(first.encodedZodType, second.encodedZodType),\n decodedZodType: intersection(first.decodedZodType, second.decodedZodType),\n props: { options },\n })\n }\n}\n\nexport type DiscriminatedUnionProperties<K extends string = string> = {\n [OK in K]: LiteralType<string>\n} & {\n [OK in string]: any\n}\n\nexport type DiscriminatedUnionOptionType<K extends string> = ObjectType<\n ObjectTypeProps & { [_ in K]: BaseTypeAny }\n>\n\nexport class DiscriminatedUnionType<\n K extends string = string,\n T extends\n readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],\n> extends BaseType<\n ZodMiniDiscriminatedUnion<core.utils.Flatten<T[number]['encodedZodType'][]>>,\n ZodMiniDiscriminatedUnion<core.utils.Flatten<T[number]['decodedZodType'][]>>,\n {\n key: K\n options: T\n }\n> {\n static factory<\n K extends string = string,\n T extends\n readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],\n >(key: K, ...options: T) {\n return new DiscriminatedUnionType<K, T>({\n encodedZodType: discriminatedUnion(\n options.map((t) => t.encodedZodType) as any,\n ),\n decodedZodType: discriminatedUnion(\n options.map((t) => t.decodedZodType) as any,\n ),\n props: { key, options },\n })\n }\n}\n"],"version":3}
package/package.json CHANGED
@@ -3,30 +3,18 @@
3
3
  "type": "module",
4
4
  "exports": {
5
5
  ".": {
6
- "bun": "./src/index.ts",
7
- "default": "./dist/index.js",
6
+ "import": "./dist/index.js",
8
7
  "types": "./src/index.ts"
9
8
  },
10
- "./compiler": {
11
- "bun": "./src/compiler.ts",
12
- "default": "./dist/compiler.js",
13
- "types": "./src/compiler.ts"
14
- },
15
- "./runtime": {
16
- "bun": "./src/runtime.ts",
17
- "default": "./dist/runtime.js",
18
- "types": "./src/runtime.ts"
19
- },
20
9
  "./temporal": {
21
- "bun": "./src/temporal.ts",
22
- "default": "./dist/temporal.js",
10
+ "import": "./dist/temporal.js",
23
11
  "types": "./src/temporal.ts"
24
12
  }
25
13
  },
26
14
  "dependencies": {
27
- "@sinclair/typebox": "^0.34.13",
28
- "temporal-polyfill": "^0.2.5",
29
- "@nmtjs/common": "0.6.5"
15
+ "@zod/mini": "4.0.0-beta.0",
16
+ "temporal-polyfill": "^0.3.0",
17
+ "@nmtjs/common": "0.7.1"
30
18
  },
31
19
  "files": [
32
20
  "src",
@@ -34,9 +22,9 @@
34
22
  "LICENSE.md",
35
23
  "README.md"
36
24
  ],
37
- "version": "0.6.5",
25
+ "version": "0.7.1",
38
26
  "scripts": {
39
- "build": "neemata-build -p neutral --root=./src './**/*.ts'",
27
+ "build": "neemata-build --root=./src './**/*.ts'",
40
28
  "type-check": "tsc --noEmit"
41
29
  }
42
30
  }
package/src/index.ts CHANGED
@@ -1,16 +1,11 @@
1
- import type {
2
- StaticInputDecode,
3
- StaticInputEncode,
4
- StaticOutputDecode,
5
- StaticOutputEncode,
6
- } from './inference.ts'
1
+ import * as zod from '@zod/mini'
7
2
  import { AnyType } from './types/any.ts'
8
3
  import { ArrayType } from './types/array.ts'
9
4
  import type { BaseTypeAny } from './types/base.ts'
10
5
  import { BooleanType } from './types/boolean.ts'
11
6
  import { CustomType } from './types/custom.ts'
12
7
  import { DateType } from './types/date.ts'
13
- import { EnumType, ObjectEnumType } from './types/enum.ts'
8
+ import { EnumType } from './types/enum.ts'
14
9
  import { LiteralType } from './types/literal.ts'
15
10
  import { NeverType } from './types/never.ts'
16
11
  import { BigIntType, IntegerType, NumberType } from './types/number.ts'
@@ -31,8 +26,9 @@ import {
31
26
  UnionType,
32
27
  } from './types/union.ts'
33
28
 
34
- export type { TSchema } from '@sinclair/typebox'
35
- export * from './schemas/nullable.ts'
29
+ zod.config(zod.core.locales.en())
30
+
31
+ export { NeemataTypeError } from './types/base.ts'
36
32
  export { BaseType, type BaseTypeAny } from './types/base.ts'
37
33
  export {
38
34
  ArrayType,
@@ -49,23 +45,24 @@ export {
49
45
  UnionType,
50
46
  AnyType,
51
47
  NeverType,
48
+ DiscriminatedUnionType,
49
+ RecordType,
52
50
  }
53
51
 
54
52
  export namespace type {
55
53
  export namespace infer {
56
- export type decoded<T extends BaseTypeAny<any>> = StaticOutputDecode<
57
- T['schema']
58
- >
59
- export type encoded<T extends BaseTypeAny<any>> = StaticOutputEncode<
60
- T['schema']
61
- >
54
+ export type decoded<T extends BaseTypeAny> =
55
+ T['decodedZodType']['_zod']['output']
56
+
57
+ export type encoded<T extends BaseTypeAny> =
58
+ T['encodedZodType']['_zod']['output']
59
+
62
60
  export namespace input {
63
- export type decoded<T extends BaseTypeAny<any>> = StaticInputDecode<
64
- T['schema']
65
- >
66
- export type encoded<T extends BaseTypeAny<any>> = StaticInputEncode<
67
- T['schema']
68
- >
61
+ export type decoded<T extends BaseTypeAny> =
62
+ T['decodedZodType']['_zod']['input']
63
+
64
+ export type encoded<T extends BaseTypeAny> =
65
+ T['encodedZodType']['_zod']['input']
69
66
  }
70
67
  }
71
68
 
@@ -76,14 +73,15 @@ export namespace type {
76
73
  export const integer = IntegerType.factory
77
74
  export const bitint = BigIntType.factory
78
75
  export const literal = LiteralType.factory
79
- export const objectEnum = ObjectEnumType.factory
80
- export const arrayEnum = EnumType.factory
76
+ export const enumeration = EnumType.factory
81
77
  export const date = DateType.factory
82
78
  export const array = ArrayType.factory
83
79
  export const record = RecordType.factory
84
80
  export const any = AnyType.factory
85
81
  export const or = UnionType.factory
86
82
  export const and = IntersactionType.factory
83
+ export const union = UnionType.factory
84
+ export const intersaction = IntersactionType.factory
87
85
  export const discriminatedUnion = DiscriminatedUnionType.factory
88
86
  export const custom = CustomType.factory
89
87
  export const object = Object.assign(ObjectType.factory.bind(ObjectType), {
@@ -96,4 +94,4 @@ export namespace type {
96
94
  })
97
95
  }
98
96
 
99
- export { type as t }
97
+ export { type as t, zod }
package/src/temporal.ts CHANGED
@@ -16,7 +16,7 @@ export const duration = DurationType.factory
16
16
  export const plainYearMonth = PlainYearMonthType.factory
17
17
  export const plainMonthDay = PlainMonthDayType.factory
18
18
 
19
- export {
19
+ export type {
20
20
  DurationType,
21
21
  PlainDateTimeType,
22
22
  PlainDateType,
package/src/types/any.ts CHANGED
@@ -1,8 +1,10 @@
1
- import { type TAny, Type } from '@sinclair/typebox'
1
+ import { any, type ZodMiniAny } from '@zod/mini'
2
2
  import { BaseType } from './base.ts'
3
3
 
4
- export class AnyType extends BaseType<TAny, {}, any> {
4
+ export class AnyType extends BaseType<ZodMiniAny> {
5
5
  static factory() {
6
- return new AnyType(Type.Any())
6
+ return new AnyType({
7
+ encodedZodType: any(),
8
+ })
7
9
  }
8
10
  }
@@ -1,38 +1,53 @@
1
1
  import {
2
- type ArrayOptions,
3
- type StaticDecode,
4
- type TArray,
5
- Type,
6
- } from '@sinclair/typebox'
2
+ array,
3
+ type core,
4
+ length,
5
+ maxLength,
6
+ minLength,
7
+ type ZodMiniArray,
8
+ } from '@zod/mini'
7
9
  import { BaseType } from './base.ts'
8
10
 
11
+ type Check = core.CheckFn<any[]> | core.$ZodCheck<any[]>
12
+
9
13
  export class ArrayType<T extends BaseType = BaseType> extends BaseType<
10
- TArray<T['schema']>,
11
- { element: T; options: ArrayOptions }
14
+ ZodMiniArray<T['encodedZodType']>,
15
+ ZodMiniArray<T['decodedZodType']>,
16
+ { element: T }
12
17
  > {
13
- static factory<T extends BaseType>(element: T, options: ArrayOptions = {}) {
14
- return new ArrayType<T>(Type.Array(element.schema, options))
18
+ static factory<T extends BaseType>(element: T, ...checks: Check[]) {
19
+ return new ArrayType<T>({
20
+ encodedZodType: array(element.encodedZodType).check(...checks),
21
+ decodedZodType: array(element.decodedZodType).check(...checks),
22
+ params: { checks },
23
+ props: { element },
24
+ })
15
25
  }
16
26
 
17
27
  min(value: number) {
18
- return ArrayType.factory(this.props.element, {
19
- ...this.props.options,
20
- minItems: value,
21
- })
28
+ const check = minLength(value)
29
+ return ArrayType.factory<T>(
30
+ this.props.element,
31
+ ...this.params.checks,
32
+ check,
33
+ )
22
34
  }
23
35
 
24
36
  max(value: number) {
25
- return ArrayType.factory(this.props.element, {
26
- ...this.props.options,
27
- maxItems: value,
28
- })
37
+ const check = maxLength(value)
38
+ return ArrayType.factory<T>(
39
+ this.props.element,
40
+ ...this.params.checks,
41
+ check,
42
+ )
29
43
  }
30
44
 
31
45
  length(value: number) {
32
- return ArrayType.factory(this.props.element, {
33
- ...this.props.options,
34
- minItems: value,
35
- maxItems: value,
36
- })
46
+ const check = length(value)
47
+ return ArrayType.factory<T>(
48
+ this.props.element,
49
+ ...this.params.checks,
50
+ check,
51
+ )
37
52
  }
38
53
  }
package/src/types/base.ts CHANGED
@@ -1,139 +1,207 @@
1
- import { Optional, type TOptional, type TSchema } from '@sinclair/typebox'
2
- import { Default, type TDefault } from '../schemas/default.ts'
3
- import { Nullable, type TNullable } from '../schemas/nullable.ts'
1
+ import {
2
+ _default,
3
+ core,
4
+ nullable,
5
+ optional,
6
+ registry,
7
+ type ZodMiniAny,
8
+ type ZodMiniArray,
9
+ type ZodMiniBoolean,
10
+ type ZodMiniDefault,
11
+ type ZodMiniEnum,
12
+ type ZodMiniInterface,
13
+ type ZodMiniIntersection,
14
+ type ZodMiniLiteral,
15
+ type ZodMiniNever,
16
+ type ZodMiniNullable,
17
+ type ZodMiniNumber,
18
+ type ZodMiniObject,
19
+ type ZodMiniOptional,
20
+ type ZodMiniPipe,
21
+ type ZodMiniRecord,
22
+ type ZodMiniString,
23
+ type ZodMiniType,
24
+ type ZodMiniUnion,
25
+ } from '@zod/mini'
26
+
27
+ export type PrimitiveValueType = string | number | boolean | null
28
+
29
+ export type SimpleZodType =
30
+ | ZodMiniNever
31
+ | ZodMiniDefault
32
+ | ZodMiniNullable
33
+ | ZodMiniOptional
34
+ | ZodMiniString
35
+ | ZodMiniObject
36
+ | ZodMiniAny
37
+ | ZodMiniArray
38
+ | ZodMiniBoolean
39
+ | ZodMiniNumber
40
+ | ZodMiniEnum<any>
41
+ | ZodMiniLiteral<PrimitiveValueType>
42
+ | ZodMiniUnion
43
+ | ZodMiniIntersection
44
+ | ZodMiniInterface
45
+ | ZodMiniRecord
46
+ | ZodMiniPipe
47
+
48
+ export type ZodType = SimpleZodType | ZodMiniType
4
49
 
5
50
  export type TypeProps = Record<string, any>
6
51
 
52
+ export type TypeMetadata<T = any> = {
53
+ id?: string
54
+ description?: string
55
+ examples?: T[]
56
+ title?: string
57
+ }
58
+
7
59
  export type TypeParams = {
8
- optional?: boolean
9
- nullable?: boolean
10
- hasDefault?: boolean
11
60
  encode?: (value: any) => any
61
+ metadata?: TypeMetadata
62
+ checks: Array<core.CheckFn<any> | core.$ZodCheck<any>>
12
63
  }
13
64
 
14
65
  export type DefaultTypeParams = {
15
- optional: false
16
- nullable: false
17
- hasDefault: false
18
66
  encode?: TypeParams['encode']
67
+ metadata?: TypeMetadata
19
68
  }
20
69
 
21
- export type BaseTypeAny<T extends TSchema = TSchema> = BaseType<
22
- T,
23
- TypeProps,
24
- any
25
- >
70
+ export type BaseTypeAny<
71
+ EncodedZodType extends SimpleZodType = SimpleZodType,
72
+ DecodedZodType extends ZodType = ZodMiniType,
73
+ > = BaseType<EncodedZodType, DecodedZodType, TypeProps>
74
+
75
+ export const typesRegistry = registry<TypeMetadata>()
76
+
77
+ export const NeemataTypeError = core.$ZodError
78
+ export type NeemataTypeError = core.$ZodError
26
79
 
27
80
  export abstract class BaseType<
28
- Schema extends TSchema = TSchema,
81
+ EncodedZodType extends SimpleZodType = SimpleZodType,
82
+ DecodedZodType extends ZodType = EncodedZodType,
29
83
  Props extends TypeProps = TypeProps,
30
- ValueType = unknown,
31
84
  > {
32
- readonly schema: Schema
85
+ readonly encodedZodType: EncodedZodType
86
+ readonly decodedZodType: DecodedZodType
33
87
  readonly props: Props
34
88
  readonly params: TypeParams
35
89
 
36
- constructor(
37
- schema: Schema,
38
- props: Props = {} as Props,
39
- params: TypeParams = {} as TypeParams,
40
- ) {
41
- const { hasDefault = false, nullable = false, optional = false } = params
42
- this.schema = schema
90
+ constructor({
91
+ encodedZodType,
92
+ decodedZodType = encodedZodType as unknown as DecodedZodType,
93
+ props = {} as Props,
94
+ params = {} as Partial<TypeParams>,
95
+ }: {
96
+ encodedZodType: EncodedZodType
97
+ decodedZodType?: DecodedZodType
98
+ props?: Props
99
+ params?: Partial<TypeParams>
100
+ }) {
101
+ this.encodedZodType = encodedZodType
102
+ this.decodedZodType = decodedZodType
43
103
 
44
104
  this.props = props
45
- this.params = {
46
- hasDefault,
47
- nullable,
48
- optional,
49
- } as TypeParams
105
+ this.params = Object.assign({ checks: [] }, params)
50
106
  }
51
107
 
52
- optional(): OptionalType<BaseType<Schema, Props>, ValueType> {
53
- return OptionalType.factory(this) as any
108
+ optional(): OptionalType<this> {
109
+ return OptionalType.factory(this)
54
110
  }
55
111
 
56
- nullable(): NullableType<BaseType<Schema, Props>, ValueType> {
57
- return NullableType.factory(this) as any
112
+ nullable(): NullableType<this> {
113
+ return NullableType.factory(this)
58
114
  }
59
115
 
60
116
  nullish() {
61
117
  return this.nullable().optional()
62
118
  }
63
119
 
64
- default(value: ValueType): DefaultType<BaseType<Schema, Props>, ValueType> {
65
- return DefaultType.factory(
66
- this,
67
- this.params.encode?.(value) ?? value,
68
- ) as any
120
+ default(value: this['encodedZodType']['_zod']['input']): DefaultType<this> {
121
+ return DefaultType.factory(this, value)
122
+ }
123
+
124
+ title(title: string): this {
125
+ return this.meta({ title })
126
+ }
127
+
128
+ description(description: string): this {
129
+ return this.meta({ description })
130
+ }
131
+
132
+ examples(...examples: this['decodedZodType']['_zod']['input'][]): this {
133
+ return this.meta({
134
+ examples: this.params.encode
135
+ ? examples.map(this.params.encode)
136
+ : examples,
137
+ })
69
138
  }
70
139
 
71
- description(description: string): BaseType<Schema, Props, ValueType> {
72
- const ThisConstructor = this.constructor as any
73
- return new ThisConstructor(
74
- {
75
- ...this.schema,
76
- description,
77
- },
78
- this.props,
79
- this.params,
80
- ) as any
140
+ meta(newMetadata: TypeMetadata): this {
141
+ const metadata = typesRegistry.get(this.encodedZodType) ?? {}
142
+ Object.assign(metadata, newMetadata)
143
+ typesRegistry.add(this.encodedZodType, metadata)
144
+ return this
81
145
  }
82
146
 
83
- examples(...examples: ValueType[]): BaseType<Schema, Props, ValueType> {
84
- const ThisConstructor = this.constructor as any
85
- return new ThisConstructor(
86
- {
87
- ...this.schema,
88
- examples,
89
- },
90
- this.props,
91
- this.params,
92
- ) as any
147
+ encode(
148
+ data: this['encodedZodType']['_zod']['input'],
149
+ ): this['encodedZodType']['_zod']['output'] {
150
+ return this.encodedZodType.parse(data, { reportInput: true })
151
+ }
152
+
153
+ decode(
154
+ data: this['decodedZodType']['_zod']['input'],
155
+ ): this['decodedZodType']['_zod']['output'] {
156
+ return this.decodedZodType.parse(data, { reportInput: true })
93
157
  }
94
158
  }
95
159
 
96
160
  export class OptionalType<
97
161
  Type extends BaseTypeAny = BaseTypeAny,
98
- ValueType = unknown,
99
- > extends BaseType<TOptional<Type['schema']>, { inner: Type }, ValueType> {
162
+ > extends BaseType<
163
+ ZodMiniOptional<Type['encodedZodType']>,
164
+ ZodMiniOptional<Type['decodedZodType']>,
165
+ { inner: Type }
166
+ > {
100
167
  static factory<T extends BaseTypeAny>(type: T) {
101
- return new OptionalType<T>(Optional(type.schema) as any, { inner: type }, {
102
- ...type.params,
103
- optional: true,
104
- } as any)
168
+ return new OptionalType<T>({
169
+ encodedZodType: optional(type.encodedZodType) as any,
170
+ props: { inner: type },
171
+ })
105
172
  }
106
173
  }
107
174
 
108
175
  export class NullableType<
109
176
  Type extends BaseTypeAny<any> = BaseTypeAny<any>,
110
- ValueType = unknown,
111
177
  > extends BaseType<
112
- TNullable<Type['schema']>,
113
- { inner: Type },
114
- ValueType | null
178
+ ZodMiniNullable<Type['encodedZodType']>,
179
+ ZodMiniNullable<Type['decodedZodType']>,
180
+ { inner: Type }
115
181
  > {
116
182
  static factory<T extends BaseTypeAny<any>>(type: T) {
117
- return new NullableType<T>(Nullable(type.schema), { inner: type }, {
118
- ...type.params,
119
- nullable: true,
120
- } as any)
183
+ return new NullableType<T>({
184
+ encodedZodType: nullable(type.encodedZodType),
185
+ props: { inner: type },
186
+ })
121
187
  }
122
188
  }
123
189
 
124
190
  export class DefaultType<
125
191
  Type extends BaseTypeAny = BaseTypeAny,
126
- ValueType = unknown,
127
192
  > extends BaseType<
128
- TDefault<TOptional<Type['schema']>>,
129
- { inner: Type },
130
- ValueType
193
+ ZodMiniDefault<Type['encodedZodType']>,
194
+ ZodMiniDefault<Type['decodedZodType']>,
195
+ { inner: Type }
131
196
  > {
132
197
  static factory<T extends BaseTypeAny<any>>(type: T, defaultValue: any) {
133
- return new DefaultType<T>(
134
- Default(Optional(type.schema), defaultValue) as any,
135
- { inner: type },
136
- { ...type.params, hasDefault: true } as any,
137
- )
198
+ return new DefaultType<T>({
199
+ encodedZodType: _default(
200
+ type.encodedZodType,
201
+ type.params.encode?.(defaultValue) ?? defaultValue,
202
+ ) as any,
203
+ decodedZodType: _default(type.decodedZodType, defaultValue) as any,
204
+ props: { inner: type },
205
+ })
138
206
  }
139
207
  }
@@ -1,8 +1,10 @@
1
- import { type TBoolean, Type } from '@sinclair/typebox'
1
+ import { boolean, type ZodMiniBoolean } from '@zod/mini'
2
2
  import { BaseType } from './base.ts'
3
3
 
4
- export class BooleanType extends BaseType<TBoolean, {}, boolean> {
4
+ export class BooleanType extends BaseType<ZodMiniBoolean<boolean>> {
5
5
  static factory() {
6
- return new BooleanType(Type.Boolean())
6
+ return new BooleanType({
7
+ encodedZodType: boolean(),
8
+ })
7
9
  }
8
10
  }
@@ -1,33 +1,66 @@
1
1
  import {
2
- type TAny,
3
- type TSchema,
4
- type TTransform,
5
- Type,
6
- } from '@sinclair/typebox'
7
- import type { StaticInputDecode, StaticOutputDecode } from '../inference.ts'
8
- import { BaseType } from './base.ts'
2
+ any,
3
+ type core,
4
+ custom,
5
+ overwrite,
6
+ pipe,
7
+ type ZodMiniAny,
8
+ type ZodMiniCustom,
9
+ type ZodMiniPipe,
10
+ } from '@zod/mini'
11
+ import { BaseType, type SimpleZodType, type ZodType } from './base.ts'
9
12
 
10
- export type CustomTypeDecode<T> = (value: any) => T
11
- export type CustomTypeEncode<T> = (value: T) => any
13
+ export type CustomTypeDecode<I, O> = (value: I) => O
14
+ export type CustomTypeEncode<I, O> = (value: I) => O
12
15
 
13
16
  export abstract class TransformType<
14
- T,
15
- S extends TSchema = TAny,
16
- > extends BaseType<TTransform<S, T>, {}, StaticInputDecode<TTransform<S, T>>> {}
17
+ Type,
18
+ EncodedType extends SimpleZodType = ZodMiniAny,
19
+ DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
20
+ > extends BaseType<
21
+ ZodMiniPipe<
22
+ ZodMiniCustom<DecodedType['_zod']['output'], DecodedType['_zod']['input']>,
23
+ EncodedType
24
+ >,
25
+ ZodMiniPipe<
26
+ EncodedType,
27
+ ZodMiniCustom<EncodedType['_zod']['output'], EncodedType['_zod']['input']>
28
+ >
29
+ > {}
17
30
 
18
- export class CustomType<T, S extends TSchema = TAny> extends TransformType<
19
- T,
20
- S
21
- > {
22
- static factory<T, S extends TSchema = TAny>(
23
- decode: CustomTypeDecode<T>,
24
- encode: CustomTypeEncode<T>,
25
- schema: S = Type.Any() as S,
26
- ) {
27
- return new CustomType<T, S>(
28
- Type.Transform(schema).Decode(decode).Encode(encode),
29
- {},
30
- { encode } as any,
31
- )
31
+ export class CustomType<
32
+ Type,
33
+ EncodedType extends SimpleZodType = ZodMiniAny,
34
+ DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
35
+ > extends TransformType<Type, EncodedType, DecodedType> {
36
+ static factory<
37
+ Type,
38
+ EncodedType extends SimpleZodType = ZodMiniAny,
39
+ DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
40
+ >({
41
+ decode,
42
+ encode,
43
+ error,
44
+ type = any() as unknown as EncodedType,
45
+ }: {
46
+ decode: CustomTypeDecode<
47
+ EncodedType['_zod']['input'],
48
+ DecodedType['_zod']['output']
49
+ >
50
+ encode: CustomTypeEncode<
51
+ DecodedType['_zod']['input'],
52
+ EncodedType['_zod']['output']
53
+ >
54
+ error?: string | core.$ZodErrorMap<core.$ZodIssueBase>
55
+ type?: EncodedType
56
+ }) {
57
+ return new CustomType<Type, EncodedType, DecodedType>({
58
+ encodedZodType: pipe(custom().check(overwrite(encode)), type),
59
+ decodedZodType: pipe(
60
+ type,
61
+ custom(undefined, { error }).check(overwrite(decode)),
62
+ ),
63
+ params: { encode },
64
+ })
32
65
  }
33
66
  }