@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.
- package/dist/index.js +31 -31
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +7 -8
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +4 -4
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +23 -23
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +65 -66
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +4 -4
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +10 -9
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +6 -13
- package/dist/types/date.js.map +1 -1
- package/dist/types/enum.js +7 -16
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +7 -6
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +4 -4
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +26 -93
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +42 -31
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +31 -47
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +40 -41
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +25 -18
- package/dist/types/union.js.map +1 -1
- package/dist/utils.js +0 -1
- package/dist/utils.js.map +1 -1
- package/package.json +7 -19
- package/src/index.ts +24 -25
- package/src/temporal.ts +8 -8
- package/src/types/any.ts +5 -3
- package/src/types/array.ts +24 -22
- package/src/types/base.ts +148 -81
- package/src/types/boolean.ts +5 -3
- package/src/types/custom.ts +43 -24
- package/src/types/date.ts +17 -16
- package/src/types/enum.ts +12 -22
- package/src/types/literal.ts +9 -6
- package/src/types/never.ts +5 -3
- package/src/types/number.ts +31 -93
- package/src/types/object.ts +44 -37
- package/src/types/string.ts +41 -39
- package/src/types/temporal.ts +72 -32
- package/src/types/union.ts +59 -50
- package/src/utils.ts +22 -22
- package/dist/compiler.js +0 -55
- package/dist/compiler.js.map +0 -1
- package/dist/formats.js +0 -127
- package/dist/formats.js.map +0 -1
- package/dist/inference.js +0 -1
- package/dist/inference.js.map +0 -1
- package/dist/parse.js +0 -145
- package/dist/parse.js.map +0 -1
- package/dist/runtime.js +0 -73
- package/dist/runtime.js.map +0 -1
- package/dist/schemas/default.js +0 -6
- package/dist/schemas/default.js.map +0 -1
- package/dist/schemas/discriminated-union.js +0 -9
- package/dist/schemas/discriminated-union.js.map +0 -1
- package/dist/schemas/nullable.js +0 -11
- package/dist/schemas/nullable.js.map +0 -1
- package/src/compiler.ts +0 -100
- package/src/formats.ts +0 -182
- package/src/inference.ts +0 -128
- package/src/parse.ts +0 -217
- package/src/runtime.ts +0 -137
- package/src/schemas/default.ts +0 -12
- package/src/schemas/discriminated-union.ts +0 -49
- 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
|
-
"
|
|
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
|
-
"
|
|
22
|
-
"default": "./dist/temporal.js",
|
|
10
|
+
"import": "./dist/temporal.js",
|
|
23
11
|
"types": "./src/temporal.ts"
|
|
24
12
|
}
|
|
25
13
|
},
|
|
26
14
|
"dependencies": {
|
|
27
|
-
"@
|
|
28
|
-
"temporal-polyfill": "^0.
|
|
29
|
-
"@nmtjs/common": "0.
|
|
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.
|
|
25
|
+
"version": "0.7.0",
|
|
38
26
|
"scripts": {
|
|
39
|
-
"build": "neemata-build
|
|
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
|
|
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 {
|
|
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
|
|
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>> =
|
|
57
|
-
T['
|
|
58
|
-
|
|
59
|
-
export type encoded<T extends BaseTypeAny<any>> =
|
|
60
|
-
T['
|
|
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>> =
|
|
64
|
-
T['
|
|
65
|
-
|
|
66
|
-
export type encoded<T extends BaseTypeAny<any>> =
|
|
67
|
-
T['
|
|
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
|
|
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
|
|
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
|
|
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
|
|
1
|
+
import { any, type ZodMiniAny } from '@zod/mini'
|
|
2
2
|
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
|
-
export class AnyType extends BaseType<
|
|
4
|
+
export class AnyType extends BaseType<ZodMiniAny> {
|
|
5
5
|
static factory() {
|
|
6
|
-
return new AnyType(
|
|
6
|
+
return new AnyType({
|
|
7
|
+
encodedZodType: any(),
|
|
8
|
+
})
|
|
7
9
|
}
|
|
8
10
|
}
|
package/src/types/array.ts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
11
|
-
|
|
14
|
+
ZodMiniArray<T['encodedZodType']>,
|
|
15
|
+
ZodMiniArray<T['decodedZodType']>,
|
|
16
|
+
{ element: T; checks: Check[] }
|
|
12
17
|
> {
|
|
13
|
-
static factory<T extends BaseType>(element: T,
|
|
14
|
-
return new ArrayType<T>(
|
|
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
|
-
|
|
19
|
-
|
|
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
|
-
|
|
26
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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 {
|
|
2
|
-
|
|
3
|
-
|
|
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<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
80
|
+
EncodedZodType extends SimpleZodType = SimpleZodType,
|
|
81
|
+
DecodedZodType extends ZodType = EncodedZodType,
|
|
29
82
|
Props extends TypeProps = TypeProps,
|
|
30
|
-
ValueType = unknown,
|
|
31
83
|
> {
|
|
32
|
-
readonly
|
|
84
|
+
readonly encodedZodType: EncodedZodType
|
|
85
|
+
readonly decodedZodType: DecodedZodType
|
|
33
86
|
readonly props: Props
|
|
34
87
|
readonly params: TypeParams
|
|
35
88
|
|
|
36
|
-
constructor(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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<
|
|
53
|
-
return OptionalType.factory(this)
|
|
107
|
+
optional(): OptionalType<this> {
|
|
108
|
+
return OptionalType.factory(this)
|
|
54
109
|
}
|
|
55
110
|
|
|
56
|
-
nullable(): NullableType<
|
|
57
|
-
return NullableType.factory(this)
|
|
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:
|
|
65
|
-
return DefaultType.factory(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
99
|
-
|
|
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>(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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>(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
}
|
package/src/types/boolean.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { boolean, type ZodMiniBoolean } from '@zod/mini'
|
|
2
2
|
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
|
-
export class BooleanType extends BaseType<
|
|
4
|
+
export class BooleanType extends BaseType<ZodMiniBoolean<boolean>> {
|
|
5
5
|
static factory() {
|
|
6
|
-
return new BooleanType(
|
|
6
|
+
return new BooleanType({
|
|
7
|
+
encodedZodType: boolean(),
|
|
8
|
+
})
|
|
7
9
|
}
|
|
8
10
|
}
|
package/src/types/custom.ts
CHANGED
|
@@ -1,33 +1,52 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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<
|
|
11
|
-
export type CustomTypeEncode<
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
Type,
|
|
17
|
+
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
18
|
+
DecodedType extends ZodType = ZodMiniCustom<Type>,
|
|
19
|
+
> extends BaseType<EncodedType, DecodedType> {}
|
|
17
20
|
|
|
18
|
-
export class CustomType<
|
|
19
|
-
|
|
20
|
-
|
|
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<
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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<
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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 {
|
|
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:
|
|
5
|
-
const encode = (value: Date):
|
|
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<
|
|
13
|
+
export class DateType extends TransformType<
|
|
14
|
+
Date,
|
|
15
|
+
ZodMiniUnion<[iso.ZodMiniISODate, iso.ZodMiniISODateTime]>
|
|
16
|
+
> {
|
|
8
17
|
static factory() {
|
|
9
|
-
return CustomType.factory<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
}
|