@nmtjs/type 0.6.4 → 0.7.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 (76) hide show
  1. package/dist/index.js +31 -31
  2. package/dist/index.js.map +1 -1
  3. package/dist/temporal.js +7 -8
  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 +23 -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 +10 -9
  14. package/dist/types/custom.js.map +1 -1
  15. package/dist/types/date.js +6 -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 +26 -93
  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 +31 -47
  28. package/dist/types/string.js.map +1 -1
  29. package/dist/types/temporal.js +40 -41
  30. package/dist/types/temporal.js.map +1 -1
  31. package/dist/types/union.js +25 -18
  32. package/dist/types/union.js.map +1 -1
  33. package/dist/utils.js +0 -1
  34. package/dist/utils.js.map +1 -1
  35. package/package.json +7 -19
  36. package/src/index.ts +24 -25
  37. package/src/temporal.ts +8 -8
  38. package/src/types/any.ts +5 -3
  39. package/src/types/array.ts +24 -22
  40. package/src/types/base.ts +148 -81
  41. package/src/types/boolean.ts +5 -3
  42. package/src/types/custom.ts +43 -24
  43. package/src/types/date.ts +17 -16
  44. package/src/types/enum.ts +12 -22
  45. package/src/types/literal.ts +9 -6
  46. package/src/types/never.ts +5 -3
  47. package/src/types/number.ts +31 -93
  48. package/src/types/object.ts +44 -37
  49. package/src/types/string.ts +41 -39
  50. package/src/types/temporal.ts +72 -32
  51. package/src/types/union.ts +59 -50
  52. package/src/utils.ts +22 -22
  53. package/dist/compiler.js +0 -55
  54. package/dist/compiler.js.map +0 -1
  55. package/dist/formats.js +0 -127
  56. package/dist/formats.js.map +0 -1
  57. package/dist/inference.js +0 -1
  58. package/dist/inference.js.map +0 -1
  59. package/dist/parse.js +0 -145
  60. package/dist/parse.js.map +0 -1
  61. package/dist/runtime.js +0 -73
  62. package/dist/runtime.js.map +0 -1
  63. package/dist/schemas/default.js +0 -6
  64. package/dist/schemas/default.js.map +0 -1
  65. package/dist/schemas/discriminated-union.js +0 -9
  66. package/dist/schemas/discriminated-union.js.map +0 -1
  67. package/dist/schemas/nullable.js +0 -11
  68. package/dist/schemas/nullable.js.map +0 -1
  69. package/src/compiler.ts +0 -100
  70. package/src/formats.ts +0 -182
  71. package/src/inference.ts +0 -128
  72. package/src/parse.ts +0 -217
  73. package/src/runtime.ts +0 -137
  74. package/src/schemas/default.ts +0 -12
  75. package/src/schemas/discriminated-union.ts +0 -49
  76. package/src/schemas/nullable.ts +0 -20
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.4"
15
+ "@zod/mini": "4.0.0-beta.0",
16
+ "temporal-polyfill": "^0.3.0",
17
+ "@nmtjs/common": "0.7.0"
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.4",
25
+ "version": "0.7.0",
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,19 +1,17 @@
1
- import type {
2
- StaticInputDecode,
3
- StaticInputEncode,
4
- StaticOutputDecode,
5
- StaticOutputEncode,
6
- } from './inference.ts'
7
1
  import { AnyType } from './types/any.ts'
8
2
  import { ArrayType } from './types/array.ts'
9
3
  import type { BaseTypeAny } from './types/base.ts'
10
4
  import { BooleanType } from './types/boolean.ts'
11
5
  import { CustomType } from './types/custom.ts'
12
6
  import { DateType } from './types/date.ts'
13
- import { EnumType, ObjectEnumType } from './types/enum.ts'
7
+ import { EnumType } from './types/enum.ts'
14
8
  import { LiteralType } from './types/literal.ts'
15
9
  import { NeverType } from './types/never.ts'
16
- import { BigIntType, IntegerType, NumberType } from './types/number.ts'
10
+ import {
11
+ // BigIntType,
12
+ IntegerType,
13
+ NumberType,
14
+ } from './types/number.ts'
17
15
  import {
18
16
  extend,
19
17
  keyof,
@@ -31,8 +29,7 @@ import {
31
29
  UnionType,
32
30
  } from './types/union.ts'
33
31
 
34
- export type { TSchema } from '@sinclair/typebox'
35
- export * from './schemas/nullable.ts'
32
+ export { NeemataTypeError } from './types/base.ts'
36
33
  export { BaseType, type BaseTypeAny } from './types/base.ts'
37
34
  export {
38
35
  ArrayType,
@@ -49,23 +46,24 @@ export {
49
46
  UnionType,
50
47
  AnyType,
51
48
  NeverType,
49
+ DiscriminatedUnionType,
50
+ RecordType,
52
51
  }
53
52
 
54
53
  export namespace type {
55
54
  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
- >
55
+ export type decoded<T extends BaseTypeAny<any>> =
56
+ T['decodedZodType']['_zod']['output']
57
+
58
+ export type encoded<T extends BaseTypeAny<any>> =
59
+ T['encodedZodType']['_zod']['output']
60
+
62
61
  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
- >
62
+ export type decoded<T extends BaseTypeAny<any>> =
63
+ T['decodedZodType']['_zod']['input']
64
+
65
+ export type encoded<T extends BaseTypeAny<any>> =
66
+ T['encodedZodType']['_zod']['input']
69
67
  }
70
68
  }
71
69
 
@@ -74,16 +72,17 @@ export namespace type {
74
72
  export const string = StringType.factory
75
73
  export const number = NumberType.factory
76
74
  export const integer = IntegerType.factory
77
- export const bitint = BigIntType.factory
75
+ // export const bitint = BigIntType.factory
78
76
  export const literal = LiteralType.factory
79
- export const objectEnum = ObjectEnumType.factory
80
- export const arrayEnum = EnumType.factory
77
+ export const enumeration = EnumType.factory
81
78
  export const date = DateType.factory
82
79
  export const array = ArrayType.factory
83
80
  export const record = RecordType.factory
84
81
  export const any = AnyType.factory
85
82
  export const or = UnionType.factory
86
83
  export const and = IntersactionType.factory
84
+ export const union = UnionType.factory
85
+ export const intersaction = IntersactionType.factory
87
86
  export const discriminatedUnion = DiscriminatedUnionType.factory
88
87
  export const custom = CustomType.factory
89
88
  export const object = Object.assign(ObjectType.factory.bind(ObjectType), {
package/src/temporal.ts CHANGED
@@ -8,15 +8,15 @@ import {
8
8
  ZonedDateTimeType,
9
9
  } from './types/temporal.ts'
10
10
 
11
- export const plainDate = PlainDateType.factory
12
- export const plainDatetime = PlainDateTimeType.factory
13
- export const plainTime = PlainTimeType.factory
14
- export const zonedDatetime = ZonedDateTimeType.factory
15
- export const duration = DurationType.factory
16
- export const plainYearMonth = PlainYearMonthType.factory
17
- export const plainMonthDay = PlainMonthDayType.factory
11
+ export const plainDate = PlainDateType
12
+ export const plainDatetime = PlainDateTimeType
13
+ export const plainTime = PlainTimeType
14
+ export const zonedDatetime = ZonedDateTimeType
15
+ export const duration = DurationType
16
+ export const plainYearMonth = PlainYearMonthType
17
+ export const plainMonthDay = PlainMonthDayType
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,40 @@
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; checks: Check[] }
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
+ props: { element, checks },
23
+ })
15
24
  }
16
25
 
17
26
  min(value: number) {
18
- return ArrayType.factory(this.props.element, {
19
- ...this.props.options,
20
- minItems: value,
21
- })
27
+ const check = minLength(value)
28
+ return ArrayType.factory<T>(this.props.element, ...this.props.checks, check)
22
29
  }
23
30
 
24
31
  max(value: number) {
25
- return ArrayType.factory(this.props.element, {
26
- ...this.props.options,
27
- maxItems: value,
28
- })
32
+ const check = maxLength(value)
33
+ return ArrayType.factory<T>(this.props.element, ...this.props.checks, check)
29
34
  }
30
35
 
31
36
  length(value: number) {
32
- return ArrayType.factory(this.props.element, {
33
- ...this.props.options,
34
- minItems: value,
35
- maxItems: value,
36
- })
37
+ const check = length(value)
38
+ return ArrayType.factory<T>(this.props.element, ...this.props.checks, check)
37
39
  }
38
40
  }
package/src/types/base.ts CHANGED
@@ -1,139 +1,206 @@
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
12
62
  }
13
63
 
14
64
  export type DefaultTypeParams = {
15
- optional: false
16
- nullable: false
17
- hasDefault: false
18
65
  encode?: TypeParams['encode']
66
+ metadata?: TypeMetadata
19
67
  }
20
68
 
21
- export type BaseTypeAny<T extends TSchema = TSchema> = BaseType<
22
- T,
23
- TypeProps,
24
- any
25
- >
69
+ export type BaseTypeAny<
70
+ EncodedZodType extends SimpleZodType = SimpleZodType,
71
+ DecodedZodType extends ZodType = ZodMiniType,
72
+ > = BaseType<EncodedZodType, DecodedZodType, TypeProps>
73
+
74
+ export const typesRegistry = registry<TypeMetadata>()
75
+
76
+ export const NeemataTypeError = core.$ZodError
77
+ export type NeemataTypeError = core.$ZodError
26
78
 
27
79
  export abstract class BaseType<
28
- Schema extends TSchema = TSchema,
80
+ EncodedZodType extends SimpleZodType = SimpleZodType,
81
+ DecodedZodType extends ZodType = EncodedZodType,
29
82
  Props extends TypeProps = TypeProps,
30
- ValueType = unknown,
31
83
  > {
32
- readonly schema: Schema
84
+ readonly encodedZodType: EncodedZodType
85
+ readonly decodedZodType: DecodedZodType
33
86
  readonly props: Props
34
87
  readonly params: TypeParams
35
88
 
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
89
+ constructor({
90
+ encodedZodType,
91
+ decodedZodType = encodedZodType as unknown as DecodedZodType,
92
+ props = {} as Props,
93
+ params = {} as TypeParams,
94
+ }: {
95
+ encodedZodType: EncodedZodType
96
+ decodedZodType?: DecodedZodType
97
+ props?: Props
98
+ params?: TypeParams
99
+ }) {
100
+ this.encodedZodType = encodedZodType
101
+ this.decodedZodType = decodedZodType
43
102
 
44
103
  this.props = props
45
- this.params = {
46
- hasDefault,
47
- nullable,
48
- optional,
49
- } as TypeParams
104
+ this.params = params
50
105
  }
51
106
 
52
- optional(): OptionalType<BaseType<Schema, Props>, ValueType> {
53
- return OptionalType.factory(this) as any
107
+ optional(): OptionalType<this> {
108
+ return OptionalType.factory(this)
54
109
  }
55
110
 
56
- nullable(): NullableType<BaseType<Schema, Props>, ValueType> {
57
- return NullableType.factory(this) as any
111
+ nullable(): NullableType<this> {
112
+ return NullableType.factory(this)
58
113
  }
59
114
 
60
115
  nullish() {
61
116
  return this.nullable().optional()
62
117
  }
63
118
 
64
- default(value: ValueType): DefaultType<BaseType<Schema, Props>, ValueType> {
65
- return DefaultType.factory(
66
- this,
67
- this.params.encode?.(value) ?? value,
68
- ) as any
119
+ default(value: this['encodedZodType']['_zod']['input']): DefaultType<this> {
120
+ return DefaultType.factory(this, value)
121
+ }
122
+
123
+ title(title: string): this {
124
+ return this.meta({ title })
125
+ }
126
+
127
+ description(description: string): this {
128
+ return this.meta({ description })
129
+ }
130
+
131
+ examples(...examples: this['decodedZodType']['_zod']['input'][]): this {
132
+ return this.meta({
133
+ examples: this.params.encode
134
+ ? examples.map(this.params.encode)
135
+ : examples,
136
+ })
69
137
  }
70
138
 
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
139
+ meta(newMetadata: TypeMetadata): this {
140
+ const metadata = typesRegistry.get(this.encodedZodType) ?? {}
141
+ Object.assign(metadata, newMetadata)
142
+ typesRegistry.add(this.encodedZodType, metadata)
143
+ return this
81
144
  }
82
145
 
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
146
+ encode(
147
+ data: this['encodedZodType']['_zod']['input'],
148
+ ): this['encodedZodType']['_zod']['output'] {
149
+ return this.encodedZodType.parse(data, { reportInput: true })
150
+ }
151
+
152
+ decode(
153
+ data: this['decodedZodType']['_zod']['input'],
154
+ ): this['decodedZodType']['_zod']['output'] {
155
+ return this.decodedZodType.parse(data, { reportInput: true })
93
156
  }
94
157
  }
95
158
 
96
159
  export class OptionalType<
97
160
  Type extends BaseTypeAny = BaseTypeAny,
98
- ValueType = unknown,
99
- > extends BaseType<TOptional<Type['schema']>, { inner: Type }, ValueType> {
161
+ > extends BaseType<
162
+ ZodMiniOptional<Type['encodedZodType']>,
163
+ ZodMiniOptional<Type['decodedZodType']>,
164
+ { inner: Type }
165
+ > {
100
166
  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)
167
+ return new OptionalType<T>({
168
+ encodedZodType: optional(type.encodedZodType) as any,
169
+ props: { inner: type },
170
+ })
105
171
  }
106
172
  }
107
173
 
108
174
  export class NullableType<
109
175
  Type extends BaseTypeAny<any> = BaseTypeAny<any>,
110
- ValueType = unknown,
111
176
  > extends BaseType<
112
- TNullable<Type['schema']>,
113
- { inner: Type },
114
- ValueType | null
177
+ ZodMiniNullable<Type['encodedZodType']>,
178
+ ZodMiniNullable<Type['decodedZodType']>,
179
+ { inner: Type }
115
180
  > {
116
181
  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)
182
+ return new NullableType<T>({
183
+ encodedZodType: nullable(type.encodedZodType),
184
+ props: { inner: type },
185
+ })
121
186
  }
122
187
  }
123
188
 
124
189
  export class DefaultType<
125
190
  Type extends BaseTypeAny = BaseTypeAny,
126
- ValueType = unknown,
127
191
  > extends BaseType<
128
- TDefault<TOptional<Type['schema']>>,
129
- { inner: Type },
130
- ValueType
192
+ ZodMiniDefault<Type['encodedZodType']>,
193
+ ZodMiniDefault<Type['decodedZodType']>,
194
+ { inner: Type }
131
195
  > {
132
196
  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
- )
197
+ return new DefaultType<T>({
198
+ encodedZodType: _default(
199
+ type.encodedZodType,
200
+ type.params.encode?.(defaultValue) ?? defaultValue,
201
+ ) as any,
202
+ decodedZodType: _default(type.decodedZodType, defaultValue) as any,
203
+ props: { inner: type },
204
+ })
138
205
  }
139
206
  }
@@ -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,52 @@
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
+ custom,
4
+ overwrite,
5
+ pipe,
6
+ type ZodMiniAny,
7
+ type ZodMiniCustom,
8
+ type ZodMiniPipe,
9
+ } from '@zod/mini'
10
+ import { BaseType, type SimpleZodType, type ZodType } from './base.ts'
9
11
 
10
- export type CustomTypeDecode<T> = (value: any) => T
11
- export type CustomTypeEncode<T> = (value: T) => any
12
+ export type CustomTypeDecode<I, O> = (value: I) => O
13
+ export type CustomTypeEncode<I, O> = (value: I) => O
12
14
 
13
15
  export abstract class TransformType<
14
- T,
15
- S extends TSchema = TAny,
16
- > extends BaseType<TTransform<S, T>, {}, StaticInputDecode<TTransform<S, T>>> {}
16
+ Type,
17
+ EncodedType extends SimpleZodType = ZodMiniAny,
18
+ DecodedType extends ZodType = ZodMiniCustom<Type>,
19
+ > extends BaseType<EncodedType, DecodedType> {}
17
20
 
18
- export class CustomType<T, S extends TSchema = TAny> extends TransformType<
19
- T,
20
- S
21
+ export class CustomType<
22
+ Type,
23
+ EncodedType extends SimpleZodType = ZodMiniAny,
24
+ DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
25
+ > extends BaseType<
26
+ ZodMiniPipe<DecodedType, EncodedType>,
27
+ ZodMiniPipe<EncodedType, DecodedType>
21
28
  > {
22
- static factory<T, S extends TSchema = TAny>(
23
- decode: CustomTypeDecode<T>,
24
- encode: CustomTypeEncode<T>,
25
- schema: S = Type.Any() as S,
29
+ static factory<
30
+ Type,
31
+ EncodedType extends SimpleZodType = ZodMiniAny,
32
+ DecodedType extends ZodType = ZodMiniCustom<Type>,
33
+ >(
34
+ decode: CustomTypeDecode<
35
+ EncodedType['_zod']['output'],
36
+ DecodedType['_zod']['output']
37
+ >,
38
+ encode: CustomTypeEncode<
39
+ DecodedType['_zod']['output'],
40
+ EncodedType['_zod']['output']
41
+ >,
42
+ type: EncodedType = any() as unknown as EncodedType,
26
43
  ) {
27
- return new CustomType<T, S>(
28
- Type.Transform(schema).Decode(decode).Encode(encode),
29
- {},
30
- { encode } as any,
31
- )
44
+ return new CustomType<Type, EncodedType, DecodedType>({
45
+ //@ts-expect-error
46
+ encodedZodType: pipe(custom().check(overwrite(encode)), type),
47
+ //@ts-expect-error
48
+ decodedZodType: pipe(type, custom().check(overwrite(decode))),
49
+ params: { encode },
50
+ })
32
51
  }
33
52
  }
package/src/types/date.ts CHANGED
@@ -1,22 +1,23 @@
1
- import { type TString, Type } from '@sinclair/typebox'
1
+ import {
2
+ date,
3
+ iso,
4
+ union,
5
+ type ZodMiniDate,
6
+ type ZodMiniUnion,
7
+ } from '@zod/mini'
2
8
  import { CustomType, TransformType } from './custom.ts'
3
9
 
4
- const decode = (value: any): Date => new Date(value)
5
- const encode = (value: Date): any => value.toISOString()
10
+ const decode = (value: string): Date => new Date(value)
11
+ const encode = (value: Date): string => value.toISOString()
6
12
 
7
- export class DateType extends TransformType<Date> {
13
+ export class DateType extends TransformType<
14
+ Date,
15
+ ZodMiniUnion<[iso.ZodMiniISODate, iso.ZodMiniISODateTime]>
16
+ > {
8
17
  static factory() {
9
- return CustomType.factory<Date, TString>(
10
- decode,
11
- encode,
12
- Type.Union([
13
- Type.String({
14
- format: 'date',
15
- }),
16
- Type.String({
17
- format: 'date-time',
18
- }),
19
- ]) as unknown as TString,
20
- )
18
+ return CustomType.factory<
19
+ Date,
20
+ ZodMiniUnion<Array<iso.ZodMiniISODate | iso.ZodMiniISODateTime>>
21
+ >(decode, encode, union([iso.datetime(), iso.date()]))
21
22
  }
22
23
  }