@nmtjs/type 0.12.6 → 0.12.8
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/package.json +3 -4
- package/src/index.ts +0 -8
- package/src/temporal.ts +0 -1
- package/src/types/any.ts +0 -12
- package/src/types/array.ts +0 -48
- package/src/types/base.ts +0 -183
- package/src/types/boolean.ts +0 -12
- package/src/types/custom.ts +0 -79
- package/src/types/date.ts +0 -20
- package/src/types/enum.ts +0 -21
- package/src/types/literal.ts +0 -15
- package/src/types/never.ts +0 -12
- package/src/types/number.ts +0 -65
- package/src/types/object.ts +0 -163
- package/src/types/string.ts +0 -78
- package/src/types/temporal.ts +0 -154
- package/src/types/tuple.ts +0 -36
- package/src/types/type.ts +0 -31
- package/src/types/union.ts +0 -113
package/package.json
CHANGED
|
@@ -16,20 +16,19 @@
|
|
|
16
16
|
"peerDependencies": {
|
|
17
17
|
"temporal-polyfill": "^0.3.0",
|
|
18
18
|
"zod": "^3.25.0",
|
|
19
|
-
"@nmtjs/common": "0.12.
|
|
19
|
+
"@nmtjs/common": "0.12.8"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"temporal-polyfill": "^0.3.0",
|
|
23
23
|
"zod": "^3.25.0",
|
|
24
|
-
"@nmtjs/common": "0.12.
|
|
24
|
+
"@nmtjs/common": "0.12.8"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"src",
|
|
28
27
|
"dist",
|
|
29
28
|
"LICENSE.md",
|
|
30
29
|
"README.md"
|
|
31
30
|
],
|
|
32
|
-
"version": "0.12.
|
|
31
|
+
"version": "0.12.8",
|
|
33
32
|
"scripts": {
|
|
34
33
|
"build": "tsc",
|
|
35
34
|
"type-check": "tsc --noEmit"
|
package/src/index.ts
DELETED
package/src/temporal.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './types/temporal.ts'
|
package/src/types/any.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
export class AnyType extends BaseType<zod.ZodMiniAny> {
|
|
5
|
-
static factory() {
|
|
6
|
-
return new AnyType({
|
|
7
|
-
encodedZodType: zod.any(),
|
|
8
|
-
})
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const any = AnyType.factory
|
package/src/types/array.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
type Check = zod.core.CheckFn<any[]> | zod.core.$ZodCheck<any[]>
|
|
5
|
-
|
|
6
|
-
export class ArrayType<T extends BaseType = BaseType> extends BaseType<
|
|
7
|
-
zod.ZodMiniArray<T['encodedZodType']>,
|
|
8
|
-
zod.ZodMiniArray<T['decodedZodType']>,
|
|
9
|
-
{ element: T }
|
|
10
|
-
> {
|
|
11
|
-
static factory<T extends BaseType>(element: T, ...checks: Check[]) {
|
|
12
|
-
return new ArrayType<T>({
|
|
13
|
-
encodedZodType: zod.array(element.encodedZodType).check(...checks),
|
|
14
|
-
decodedZodType: zod.array(element.decodedZodType).check(...checks),
|
|
15
|
-
params: { checks },
|
|
16
|
-
props: { element },
|
|
17
|
-
})
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
min(value: number) {
|
|
21
|
-
const check = zod.minLength(value)
|
|
22
|
-
return ArrayType.factory<T>(
|
|
23
|
-
this.props.element,
|
|
24
|
-
...this.params.checks,
|
|
25
|
-
check,
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
max(value: number) {
|
|
30
|
-
const check = zod.maxLength(value)
|
|
31
|
-
return ArrayType.factory<T>(
|
|
32
|
-
this.props.element,
|
|
33
|
-
...this.params.checks,
|
|
34
|
-
check,
|
|
35
|
-
)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
length(value: number) {
|
|
39
|
-
const check = zod.length(value)
|
|
40
|
-
return ArrayType.factory<T>(
|
|
41
|
-
this.props.element,
|
|
42
|
-
...this.params.checks,
|
|
43
|
-
check,
|
|
44
|
-
)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export const array = ArrayType.factory
|
package/src/types/base.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
|
|
3
|
-
export type PrimitiveValueType = string | number | boolean | null
|
|
4
|
-
|
|
5
|
-
export type PrimitiveZodType =
|
|
6
|
-
| zod.ZodMiniNever
|
|
7
|
-
| zod.ZodMiniDefault
|
|
8
|
-
| zod.ZodMiniNullable
|
|
9
|
-
| zod.ZodMiniOptional
|
|
10
|
-
| zod.ZodMiniString
|
|
11
|
-
| zod.ZodMiniObject
|
|
12
|
-
| zod.ZodMiniAny
|
|
13
|
-
| zod.ZodMiniArray
|
|
14
|
-
| zod.ZodMiniBoolean
|
|
15
|
-
| zod.ZodMiniNumber
|
|
16
|
-
| zod.ZodMiniEnum<any>
|
|
17
|
-
| zod.ZodMiniLiteral<PrimitiveValueType>
|
|
18
|
-
| zod.ZodMiniUnion
|
|
19
|
-
| zod.ZodMiniIntersection
|
|
20
|
-
| zod.ZodMiniRecord
|
|
21
|
-
|
|
22
|
-
export type SimpleZodType = zod.ZodMiniType
|
|
23
|
-
|
|
24
|
-
export type ZodType = SimpleZodType | zod.ZodMiniType
|
|
25
|
-
|
|
26
|
-
export type TypeProps = Record<string, any>
|
|
27
|
-
|
|
28
|
-
export type TypeMetadata<T = any> = {
|
|
29
|
-
id?: string
|
|
30
|
-
description?: string
|
|
31
|
-
examples?: T[]
|
|
32
|
-
title?: string
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type TypeParams = {
|
|
36
|
-
encode?: (value: any) => any
|
|
37
|
-
metadata?: TypeMetadata
|
|
38
|
-
checks: Array<zod.core.CheckFn<any> | zod.core.$ZodCheck<any>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type DefaultTypeParams = {
|
|
42
|
-
encode?: TypeParams['encode']
|
|
43
|
-
metadata?: TypeMetadata
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type BaseTypeAny<
|
|
47
|
-
EncodedZodType extends SimpleZodType = SimpleZodType,
|
|
48
|
-
DecodedZodType extends ZodType = zod.ZodMiniType,
|
|
49
|
-
> = BaseType<EncodedZodType, DecodedZodType, TypeProps>
|
|
50
|
-
|
|
51
|
-
export const typesRegistry = zod.registry<TypeMetadata>()
|
|
52
|
-
|
|
53
|
-
export const NeemataTypeError = zod.core.$ZodError
|
|
54
|
-
export type NeemataTypeError = zod.core.$ZodError
|
|
55
|
-
|
|
56
|
-
export abstract class BaseType<
|
|
57
|
-
EncodedZodType extends SimpleZodType = SimpleZodType,
|
|
58
|
-
DecodedZodType extends ZodType = EncodedZodType,
|
|
59
|
-
Props extends TypeProps = TypeProps,
|
|
60
|
-
> {
|
|
61
|
-
readonly encodedZodType: EncodedZodType
|
|
62
|
-
readonly decodedZodType: DecodedZodType
|
|
63
|
-
readonly props: Props
|
|
64
|
-
readonly params: TypeParams
|
|
65
|
-
|
|
66
|
-
constructor({
|
|
67
|
-
encodedZodType,
|
|
68
|
-
decodedZodType = encodedZodType as unknown as DecodedZodType,
|
|
69
|
-
props = {} as Props,
|
|
70
|
-
params = {} as Partial<TypeParams>,
|
|
71
|
-
}: {
|
|
72
|
-
encodedZodType: EncodedZodType
|
|
73
|
-
decodedZodType?: DecodedZodType
|
|
74
|
-
props?: Props
|
|
75
|
-
params?: Partial<TypeParams>
|
|
76
|
-
}) {
|
|
77
|
-
this.encodedZodType = encodedZodType
|
|
78
|
-
this.decodedZodType = decodedZodType
|
|
79
|
-
|
|
80
|
-
this.props = props
|
|
81
|
-
this.params = Object.assign({ checks: [] }, params)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
optional(): OptionalType<this> {
|
|
85
|
-
return OptionalType.factory(this)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
nullable(): NullableType<this> {
|
|
89
|
-
return NullableType.factory(this)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
nullish() {
|
|
93
|
-
return this.nullable().optional()
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
default(value: this['encodedZodType']['_zod']['input']): DefaultType<this> {
|
|
97
|
-
return DefaultType.factory(this, value)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
title(title: string): this {
|
|
101
|
-
return this.meta({ title })
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
description(description: string): this {
|
|
105
|
-
return this.meta({ description })
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
examples(...examples: this['encodedZodType']['_zod']['input'][]): this {
|
|
109
|
-
return this.meta({
|
|
110
|
-
examples: this.params.encode
|
|
111
|
-
? examples.map(this.params.encode)
|
|
112
|
-
: examples,
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
meta(newMetadata: TypeMetadata): this {
|
|
117
|
-
const metadata = typesRegistry.get(this.encodedZodType) ?? {}
|
|
118
|
-
Object.assign(metadata, newMetadata)
|
|
119
|
-
typesRegistry.add(this.encodedZodType, metadata)
|
|
120
|
-
return this
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
encode(
|
|
124
|
-
data: this['encodedZodType']['_zod']['input'],
|
|
125
|
-
): this['encodedZodType']['_zod']['output'] {
|
|
126
|
-
return this.encodedZodType.parse(data, { reportInput: true })
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
decode(
|
|
130
|
-
data: this['decodedZodType']['_zod']['input'],
|
|
131
|
-
): this['decodedZodType']['_zod']['output'] {
|
|
132
|
-
return this.decodedZodType.parse(data, { reportInput: true })
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export class OptionalType<
|
|
137
|
-
Type extends BaseTypeAny = BaseTypeAny,
|
|
138
|
-
> extends BaseType<
|
|
139
|
-
zod.ZodMiniOptional<Type['encodedZodType']>,
|
|
140
|
-
zod.ZodMiniOptional<Type['decodedZodType']>,
|
|
141
|
-
{ inner: Type }
|
|
142
|
-
> {
|
|
143
|
-
static factory<T extends BaseTypeAny>(type: T) {
|
|
144
|
-
return new OptionalType<T>({
|
|
145
|
-
encodedZodType: zod.optional(type.encodedZodType) as any,
|
|
146
|
-
props: { inner: type },
|
|
147
|
-
})
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export class NullableType<
|
|
152
|
-
Type extends BaseTypeAny<any> = BaseTypeAny<any>,
|
|
153
|
-
> extends BaseType<
|
|
154
|
-
zod.ZodMiniNullable<Type['encodedZodType']>,
|
|
155
|
-
zod.ZodMiniNullable<Type['decodedZodType']>,
|
|
156
|
-
{ inner: Type }
|
|
157
|
-
> {
|
|
158
|
-
static factory<T extends BaseTypeAny<any>>(type: T) {
|
|
159
|
-
return new NullableType<T>({
|
|
160
|
-
encodedZodType: zod.nullable(type.encodedZodType),
|
|
161
|
-
props: { inner: type },
|
|
162
|
-
})
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export class DefaultType<
|
|
167
|
-
Type extends BaseTypeAny = BaseTypeAny,
|
|
168
|
-
> extends BaseType<
|
|
169
|
-
zod.ZodMiniDefault<Type['encodedZodType']>,
|
|
170
|
-
zod.ZodMiniDefault<Type['decodedZodType']>,
|
|
171
|
-
{ inner: Type }
|
|
172
|
-
> {
|
|
173
|
-
static factory<T extends BaseTypeAny<any>>(type: T, defaultValue: any) {
|
|
174
|
-
return new DefaultType<T>({
|
|
175
|
-
encodedZodType: zod._default(
|
|
176
|
-
type.encodedZodType,
|
|
177
|
-
type.params.encode?.(defaultValue) ?? defaultValue,
|
|
178
|
-
) as any,
|
|
179
|
-
decodedZodType: zod._default(type.decodedZodType, defaultValue) as any,
|
|
180
|
-
props: { inner: type },
|
|
181
|
-
})
|
|
182
|
-
}
|
|
183
|
-
}
|
package/src/types/boolean.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
export class BooleanType extends BaseType<zod.ZodMiniBoolean<boolean>> {
|
|
5
|
-
static factory() {
|
|
6
|
-
return new BooleanType({
|
|
7
|
-
encodedZodType: zod.boolean(),
|
|
8
|
-
})
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const boolean = BooleanType.factory
|
package/src/types/custom.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType, type SimpleZodType, type ZodType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
export type CustomTransformFn<I, O> = (value: I) => O
|
|
5
|
-
export abstract class TransformType<
|
|
6
|
-
Type,
|
|
7
|
-
EncodedType extends SimpleZodType = zod.ZodMiniType<Type, Type>,
|
|
8
|
-
DecodedType extends ZodType = zod.ZodMiniType<Type, Type>,
|
|
9
|
-
> extends BaseType<
|
|
10
|
-
zod.ZodMiniPipe<
|
|
11
|
-
zod.ZodMiniType<
|
|
12
|
-
DecodedType['_zod']['output'],
|
|
13
|
-
DecodedType['_zod']['input']
|
|
14
|
-
>,
|
|
15
|
-
EncodedType
|
|
16
|
-
>,
|
|
17
|
-
zod.ZodMiniPipe<
|
|
18
|
-
EncodedType,
|
|
19
|
-
zod.ZodMiniType<DecodedType['_zod']['output'], DecodedType['_zod']['input']>
|
|
20
|
-
>
|
|
21
|
-
> {}
|
|
22
|
-
|
|
23
|
-
export class CustomType<
|
|
24
|
-
Type,
|
|
25
|
-
EncodedType extends SimpleZodType = zod.ZodMiniType<Type, Type>,
|
|
26
|
-
DecodedType extends ZodType = zod.ZodMiniType<Type, Type>,
|
|
27
|
-
> extends TransformType<Type, EncodedType, DecodedType> {
|
|
28
|
-
static factory<
|
|
29
|
-
Type,
|
|
30
|
-
EncodedType extends SimpleZodType = zod.ZodMiniType<Type, Type>,
|
|
31
|
-
DecodedType extends ZodType = zod.ZodMiniType<Type, Type>,
|
|
32
|
-
>({
|
|
33
|
-
decode,
|
|
34
|
-
encode,
|
|
35
|
-
error,
|
|
36
|
-
type = zod.any() as unknown as EncodedType,
|
|
37
|
-
}: {
|
|
38
|
-
decode: CustomTransformFn<
|
|
39
|
-
EncodedType['_zod']['input'],
|
|
40
|
-
DecodedType['_zod']['output']
|
|
41
|
-
>
|
|
42
|
-
encode: CustomTransformFn<
|
|
43
|
-
DecodedType['_zod']['input'],
|
|
44
|
-
EncodedType['_zod']['output']
|
|
45
|
-
>
|
|
46
|
-
error?: string | zod.core.$ZodErrorMap<zod.core.$ZodIssueBase>
|
|
47
|
-
type?: EncodedType
|
|
48
|
-
}) {
|
|
49
|
-
return new CustomType<Type, EncodedType, DecodedType>({
|
|
50
|
-
encodedZodType: zod.pipe(
|
|
51
|
-
zod.custom().check(
|
|
52
|
-
zod.refine((val) => typeof val !== 'undefined', {
|
|
53
|
-
error,
|
|
54
|
-
abort: true,
|
|
55
|
-
}),
|
|
56
|
-
zod.overwrite(encode),
|
|
57
|
-
),
|
|
58
|
-
type,
|
|
59
|
-
),
|
|
60
|
-
// @ts-expect-error
|
|
61
|
-
decodedZodType: zod.pipe(
|
|
62
|
-
type,
|
|
63
|
-
// @ts-expect-error
|
|
64
|
-
zod
|
|
65
|
-
.custom()
|
|
66
|
-
.check(
|
|
67
|
-
zod.refine((val) => typeof val !== 'undefined', {
|
|
68
|
-
error,
|
|
69
|
-
abort: true,
|
|
70
|
-
}),
|
|
71
|
-
zod.overwrite(decode),
|
|
72
|
-
),
|
|
73
|
-
),
|
|
74
|
-
params: { encode },
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export const custom = CustomType.factory
|
package/src/types/date.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { CustomType, TransformType } from './custom.ts'
|
|
3
|
-
|
|
4
|
-
export class DateType extends TransformType<
|
|
5
|
-
Date,
|
|
6
|
-
zod.ZodMiniUnion<[zod.iso.ZodMiniISODate, zod.iso.ZodMiniISODateTime]>
|
|
7
|
-
> {
|
|
8
|
-
static factory() {
|
|
9
|
-
return CustomType.factory<
|
|
10
|
-
Date,
|
|
11
|
-
zod.ZodMiniUnion<[zod.iso.ZodMiniISODate, zod.iso.ZodMiniISODateTime]>
|
|
12
|
-
>({
|
|
13
|
-
decode: (value: string): Date => new Date(value),
|
|
14
|
-
encode: (value: Date): string => value.toISOString(),
|
|
15
|
-
type: zod.union([zod.iso.date(), zod.iso.datetime()]),
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const date = DateType.factory
|
package/src/types/enum.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
export class EnumType<
|
|
5
|
-
T extends zod.core.util.EnumLike = zod.core.util.EnumLike,
|
|
6
|
-
> extends BaseType<zod.ZodMiniEnum<T>, zod.ZodMiniEnum<T>, { values: T }> {
|
|
7
|
-
static factory<T extends zod.core.util.EnumLike>(values: T): EnumType<T>
|
|
8
|
-
static factory<T extends string[]>(
|
|
9
|
-
values: T,
|
|
10
|
-
): EnumType<zod.core.util.ToEnum<T[number]>>
|
|
11
|
-
static factory<T extends zod.core.util.EnumLike | string[]>(values: T) {
|
|
12
|
-
return new EnumType({
|
|
13
|
-
encodedZodType: zod.enum(values as any),
|
|
14
|
-
props: { values },
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const _enum = EnumType.factory
|
|
20
|
-
|
|
21
|
-
export { _enum as enum }
|
package/src/types/literal.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType, type PrimitiveValueType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
export class LiteralType<
|
|
5
|
-
T extends PrimitiveValueType = PrimitiveValueType,
|
|
6
|
-
> extends BaseType<zod.ZodMiniLiteral<T>, zod.ZodMiniLiteral<T>, { value: T }> {
|
|
7
|
-
static factory<T extends PrimitiveValueType>(value: T) {
|
|
8
|
-
return new LiteralType<T>({
|
|
9
|
-
encodedZodType: zod.literal(value),
|
|
10
|
-
props: { value },
|
|
11
|
-
})
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const literal = LiteralType.factory
|
package/src/types/never.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType } from './base.ts'
|
|
3
|
-
|
|
4
|
-
export class NeverType extends BaseType<zod.ZodMiniNever> {
|
|
5
|
-
static factory() {
|
|
6
|
-
return new NeverType({
|
|
7
|
-
encodedZodType: zod.never(),
|
|
8
|
-
})
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const never = NeverType.factory
|
package/src/types/number.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
import { BaseType } from './base.ts'
|
|
3
|
-
import { CustomType, TransformType } from './custom.ts'
|
|
4
|
-
|
|
5
|
-
type Check = zod.core.CheckFn<number> | zod.core.$ZodCheck<number>
|
|
6
|
-
|
|
7
|
-
export class NumberType extends BaseType<
|
|
8
|
-
zod.ZodMiniNumber<number>,
|
|
9
|
-
zod.ZodMiniNumber<number>
|
|
10
|
-
> {
|
|
11
|
-
static factory(...checks: Check[]) {
|
|
12
|
-
return new NumberType({
|
|
13
|
-
encodedZodType: zod.number().check(...checks),
|
|
14
|
-
params: { checks },
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
positive() {
|
|
19
|
-
return NumberType.factory(...this.params.checks, zod.gte(0))
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
negative() {
|
|
23
|
-
return NumberType.factory(...this.params.checks, zod.lte(0))
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
lt(value: number) {
|
|
27
|
-
return NumberType.factory(...this.params.checks, zod.lt(value))
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
lte(value: number) {
|
|
31
|
-
return NumberType.factory(...this.params.checks, zod.lte(value))
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
gte(value: number) {
|
|
35
|
-
return NumberType.factory(...this.params.checks, zod.gte(value))
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
gt(value: number) {
|
|
39
|
-
return NumberType.factory(...this.params.checks, zod.gt(value))
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class IntegerType extends NumberType {
|
|
44
|
-
static factory(...checks: Check[]) {
|
|
45
|
-
return NumberType.factory(...checks, zod.int())
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export class BigIntType extends TransformType<
|
|
50
|
-
bigint,
|
|
51
|
-
zod.ZodMiniString<string>
|
|
52
|
-
> {
|
|
53
|
-
static factory() {
|
|
54
|
-
return CustomType.factory<bigint, zod.ZodMiniString<string>>({
|
|
55
|
-
decode: (value) => BigInt(value),
|
|
56
|
-
encode: (value) => value.toString(),
|
|
57
|
-
type: zod.string().check(zod.regex(/^-?\d+$/)),
|
|
58
|
-
error: 'Invalid bigint format',
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export const number = NumberType.factory
|
|
64
|
-
export const integer = IntegerType.factory
|
|
65
|
-
export const bigInt = BigIntType.factory
|
package/src/types/object.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
|
|
3
|
-
import { BaseType, type BaseTypeAny, type OptionalType } from './base.ts'
|
|
4
|
-
import { EnumType } from './enum.ts'
|
|
5
|
-
import type { LiteralType } from './literal.ts'
|
|
6
|
-
import type { StringType } from './string.ts'
|
|
7
|
-
|
|
8
|
-
export type ObjectTypeProps = { [k: string]: BaseTypeAny }
|
|
9
|
-
export type AnyObjectType = ObjectType<ObjectTypeProps>
|
|
10
|
-
export class ObjectType<T extends ObjectTypeProps = {}> extends BaseType<
|
|
11
|
-
zod.ZodMiniObject<
|
|
12
|
-
{ [K in keyof T]: T[K]['encodedZodType'] },
|
|
13
|
-
zod.core.$strict
|
|
14
|
-
>,
|
|
15
|
-
zod.ZodMiniObject<
|
|
16
|
-
{ [K in keyof T]: T[K]['decodedZodType'] },
|
|
17
|
-
zod.core.$strict
|
|
18
|
-
>,
|
|
19
|
-
{ properties: T }
|
|
20
|
-
> {
|
|
21
|
-
static factory<T extends ObjectTypeProps = {}>(properties: T) {
|
|
22
|
-
const encodeProperties = {} as {
|
|
23
|
-
[K in keyof T]: T[K]['encodedZodType']
|
|
24
|
-
}
|
|
25
|
-
const decodeProperties = {} as {
|
|
26
|
-
[K in keyof T]: T[K]['decodedZodType']
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
for (const key in properties) {
|
|
30
|
-
encodeProperties[key] = properties[key].encodedZodType
|
|
31
|
-
decodeProperties[key] = properties[key].decodedZodType
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return new ObjectType<T>({
|
|
35
|
-
encodedZodType: zod.object(encodeProperties),
|
|
36
|
-
decodedZodType: zod.object(decodeProperties),
|
|
37
|
-
props: { properties },
|
|
38
|
-
})
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class RecordType<
|
|
43
|
-
K extends LiteralType<string | number> | EnumType | StringType,
|
|
44
|
-
E extends BaseType,
|
|
45
|
-
> extends BaseType<
|
|
46
|
-
zod.ZodMiniRecord<K['encodedZodType'], E['encodedZodType']>,
|
|
47
|
-
zod.ZodMiniRecord<K['decodedZodType'], E['decodedZodType']>
|
|
48
|
-
> {
|
|
49
|
-
static factory<
|
|
50
|
-
K extends LiteralType<string | number> | EnumType | StringType,
|
|
51
|
-
E extends BaseType,
|
|
52
|
-
>(key: K, element: E) {
|
|
53
|
-
return new RecordType<K, E>({
|
|
54
|
-
encodedZodType: zod.record(
|
|
55
|
-
(key as any).encodedZodType,
|
|
56
|
-
element.encodedZodType,
|
|
57
|
-
),
|
|
58
|
-
decodedZodType: zod.record(
|
|
59
|
-
(key as any).decodedZodType,
|
|
60
|
-
element.decodedZodType,
|
|
61
|
-
),
|
|
62
|
-
props: { key, element },
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function keyof<T extends ObjectType>(
|
|
68
|
-
type: T,
|
|
69
|
-
): EnumType<
|
|
70
|
-
zod.core.util.ToEnum<Extract<keyof T['props']['properties'], string>>
|
|
71
|
-
> {
|
|
72
|
-
return EnumType.factory(Object.keys(type.props.properties) as any)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function pick<
|
|
76
|
-
T extends AnyObjectType,
|
|
77
|
-
P extends { [K in keyof T['props']['properties']]?: true },
|
|
78
|
-
>(
|
|
79
|
-
source: T,
|
|
80
|
-
pick: P,
|
|
81
|
-
): ObjectType<{
|
|
82
|
-
[K in keyof P]: P[K] extends true
|
|
83
|
-
? K extends keyof T['props']['properties']
|
|
84
|
-
? T['props']['properties'][K]
|
|
85
|
-
: never
|
|
86
|
-
: never
|
|
87
|
-
}> {
|
|
88
|
-
const properties = Object.fromEntries(
|
|
89
|
-
Object.entries(source.props.properties).filter(([key]) => pick[key]),
|
|
90
|
-
)
|
|
91
|
-
return ObjectType.factory(properties) as any
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function omit<
|
|
95
|
-
T extends AnyObjectType,
|
|
96
|
-
P extends { [K in keyof T['props']['properties']]?: true },
|
|
97
|
-
>(
|
|
98
|
-
source: T,
|
|
99
|
-
omit: P,
|
|
100
|
-
): ObjectType<{
|
|
101
|
-
[K in keyof T['props']['properties'] as K extends keyof P
|
|
102
|
-
? never
|
|
103
|
-
: K]: T['props']['properties'][K]
|
|
104
|
-
}> {
|
|
105
|
-
const properties = Object.fromEntries(
|
|
106
|
-
Object.entries(source.props.properties).filter(([key]) => !omit[key]),
|
|
107
|
-
)
|
|
108
|
-
return ObjectType.factory(properties) as any
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function extend<T extends AnyObjectType, P extends ObjectTypeProps>(
|
|
112
|
-
object1: T,
|
|
113
|
-
properties: P,
|
|
114
|
-
): ObjectType<{
|
|
115
|
-
[K in keyof T['props']['properties'] | keyof P]: K extends keyof P
|
|
116
|
-
? P[K]
|
|
117
|
-
: K extends keyof T['props']['properties']
|
|
118
|
-
? T['props']['properties'][K]
|
|
119
|
-
: never
|
|
120
|
-
}> {
|
|
121
|
-
return ObjectType.factory({
|
|
122
|
-
...object1.props.properties,
|
|
123
|
-
...properties,
|
|
124
|
-
}) as any
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function merge<T1 extends AnyObjectType, T2 extends AnyObjectType>(
|
|
128
|
-
object1: T1,
|
|
129
|
-
object2: T2,
|
|
130
|
-
): ObjectType<{
|
|
131
|
-
[K in
|
|
132
|
-
| keyof T1['props']['properties']
|
|
133
|
-
| keyof T2['props']['properties']]: K extends keyof T2['props']['properties']
|
|
134
|
-
? T2['props']['properties'][K]
|
|
135
|
-
: K extends keyof T1['props']['properties']
|
|
136
|
-
? T1['props']['properties'][K]
|
|
137
|
-
: never
|
|
138
|
-
}> {
|
|
139
|
-
return ObjectType.factory({
|
|
140
|
-
...object1.props.properties,
|
|
141
|
-
...object2.props.properties,
|
|
142
|
-
}) as any
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function partial<
|
|
146
|
-
T extends AnyObjectType,
|
|
147
|
-
P extends T extends ObjectType<infer Props> ? Props : never,
|
|
148
|
-
>(
|
|
149
|
-
object: T,
|
|
150
|
-
): ObjectType<{
|
|
151
|
-
[K in keyof P]: OptionalType<P[K]>
|
|
152
|
-
}> {
|
|
153
|
-
const properties = {} as any
|
|
154
|
-
|
|
155
|
-
for (const [key, value] of Object.entries(object.props.properties)) {
|
|
156
|
-
properties[key] = value.optional()
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return ObjectType.factory(properties)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export const object = ObjectType.factory
|
|
163
|
-
export const record = RecordType.factory
|
package/src/types/string.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import * as zod from 'zod/v4-mini'
|
|
2
|
-
|
|
3
|
-
import { BaseType } from './base.ts'
|
|
4
|
-
|
|
5
|
-
type Check = zod.core.CheckFn<string> | zod.core.$ZodCheck<string>
|
|
6
|
-
|
|
7
|
-
export class StringType extends BaseType<
|
|
8
|
-
zod.ZodMiniString<string>,
|
|
9
|
-
zod.ZodMiniString<string>
|
|
10
|
-
> {
|
|
11
|
-
static factory(...checks: Check[]) {
|
|
12
|
-
return new StringType({
|
|
13
|
-
encodedZodType: zod.string().check(...checks),
|
|
14
|
-
params: { checks },
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
max(value: number) {
|
|
19
|
-
return StringType.factory(...this.params.checks, zod.maxLength(value))
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
min(value: number) {
|
|
23
|
-
return StringType.factory(...this.params.checks, zod.minLength(value))
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
pattern(pattern: string | RegExp) {
|
|
27
|
-
return StringType.factory(
|
|
28
|
-
...this.params.checks,
|
|
29
|
-
zod.regex(typeof pattern === 'string' ? new RegExp(pattern) : pattern),
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
email(options?: zod.core.$ZodEmailParams) {
|
|
34
|
-
return StringType.factory(...this.params.checks, zod.email(options))
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
url(options?: zod.core.$ZodURLParams) {
|
|
38
|
-
return StringType.factory(...this.params.checks, zod.url(options))
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
ipv4(options?: zod.core.$ZodIPv4Params) {
|
|
42
|
-
return StringType.factory(...this.params.checks, zod.ipv4(options))
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
ipv6(options?: zod.core.$ZodIPv6Params) {
|
|
46
|
-
return StringType.factory(...this.params.checks, zod.ipv6(options))
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
uuid(options?: zod.core.$ZodUUIDParams) {
|
|
50
|
-
return StringType.factory(...this.params.checks, zod.uuid(options))
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
emoji(options?: zod.core.$ZodEmojiParams) {
|
|
54
|
-
return StringType.factory(...this.params.checks, zod.emoji(options))
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
nanoid(options?: zod.core.$ZodNanoIDParams) {
|
|
58
|
-
return StringType.factory(...this.params.checks, zod.nanoid(options))
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
cuid(options?: zod.core.$ZodCUIDParams) {
|
|
62
|
-
return StringType.factory(...this.params.checks, zod.cuid(options))
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
cuid2(options?: zod.core.$ZodCUID2Params) {
|
|
66
|
-
return StringType.factory(...this.params.checks, zod.cuid2(options))
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
e164(options?: zod.core.$ZodE164Params) {
|
|
70
|
-
return StringType.factory(...this.params.checks, zod.e164(options))
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
jwt(options?: zod.core.$ZodJWTParams) {
|
|
74
|
-
return StringType.factory(...this.params.checks, zod.jwt(options))
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export const string = StringType.factory
|
package/src/types/temporal.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { Temporal } from 'temporal-polyfill'
|
|
2
|
-
import { iso, regex, string, type ZodMiniString } from 'zod/v4-mini'
|
|
3
|
-
import { CustomType, TransformType } from './custom.ts'
|
|
4
|
-
|
|
5
|
-
type Types = Exclude<
|
|
6
|
-
keyof typeof Temporal,
|
|
7
|
-
'Now' | 'Instant' | 'Calendar' | 'TimeZone'
|
|
8
|
-
>
|
|
9
|
-
|
|
10
|
-
type TemporalTransformer<T extends Types> = {
|
|
11
|
-
decode: (value: string) => ReturnType<(typeof Temporal)[T]['from']>
|
|
12
|
-
encode: (value: ReturnType<(typeof Temporal)[T]['from']>) => string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const createTemporalTransformer = <T extends Types>(
|
|
16
|
-
type: T,
|
|
17
|
-
decode = (value: string) => Temporal[type].from(value),
|
|
18
|
-
) => {
|
|
19
|
-
const encode = (value: ReturnType<(typeof Temporal)[T]['from']>) =>
|
|
20
|
-
value.toString({
|
|
21
|
-
calendarName: 'never',
|
|
22
|
-
smallestUnit: 'microsecond',
|
|
23
|
-
timeZoneName: 'never',
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
return {
|
|
27
|
-
decode,
|
|
28
|
-
encode,
|
|
29
|
-
} as TemporalTransformer<T>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
type EncodedType = ZodMiniString<string>
|
|
33
|
-
|
|
34
|
-
export class PlainDateType extends TransformType<
|
|
35
|
-
Temporal.PlainDate,
|
|
36
|
-
EncodedType
|
|
37
|
-
> {
|
|
38
|
-
static transformer = createTemporalTransformer('PlainDate')
|
|
39
|
-
|
|
40
|
-
static factory() {
|
|
41
|
-
return CustomType.factory<Temporal.PlainDate, EncodedType>({
|
|
42
|
-
decode: PlainDateType.transformer.decode,
|
|
43
|
-
encode: PlainDateType.transformer.encode,
|
|
44
|
-
type: iso.date(),
|
|
45
|
-
error: 'Invalid date format',
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export class PlainDateTimeType extends TransformType<
|
|
51
|
-
Temporal.PlainDateTime,
|
|
52
|
-
EncodedType
|
|
53
|
-
> {
|
|
54
|
-
static transformer = createTemporalTransformer('PlainDateTime')
|
|
55
|
-
|
|
56
|
-
static factory() {
|
|
57
|
-
return CustomType.factory<Temporal.PlainDateTime, EncodedType>({
|
|
58
|
-
decode: PlainDateTimeType.transformer.decode,
|
|
59
|
-
encode: PlainDateTimeType.transformer.encode,
|
|
60
|
-
type: iso.datetime({ local: true }),
|
|
61
|
-
error: 'Invalid datetime format',
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export class ZonedDateTimeType extends TransformType<
|
|
67
|
-
Temporal.ZonedDateTime,
|
|
68
|
-
EncodedType
|
|
69
|
-
> {
|
|
70
|
-
static transformer = createTemporalTransformer('ZonedDateTime', (value) =>
|
|
71
|
-
Temporal.Instant.from(value).toZonedDateTimeISO('UTC'),
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
static factory() {
|
|
75
|
-
return CustomType.factory<Temporal.ZonedDateTime, EncodedType>({
|
|
76
|
-
decode: ZonedDateTimeType.transformer.decode,
|
|
77
|
-
encode: ZonedDateTimeType.transformer.encode,
|
|
78
|
-
type: iso.datetime({ local: true }),
|
|
79
|
-
error: 'Invalid datetime format',
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export class PlainTimeType extends TransformType<
|
|
85
|
-
Temporal.PlainTime,
|
|
86
|
-
EncodedType
|
|
87
|
-
> {
|
|
88
|
-
static transformer = createTemporalTransformer('PlainTime')
|
|
89
|
-
|
|
90
|
-
static factory() {
|
|
91
|
-
return CustomType.factory<Temporal.PlainTime, EncodedType>({
|
|
92
|
-
decode: PlainTimeType.transformer.decode,
|
|
93
|
-
encode: PlainTimeType.transformer.encode,
|
|
94
|
-
type: iso.time(),
|
|
95
|
-
error: 'Invalid time format',
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export class DurationType extends TransformType<
|
|
101
|
-
Temporal.Duration,
|
|
102
|
-
EncodedType
|
|
103
|
-
> {
|
|
104
|
-
static transformer = createTemporalTransformer('Duration')
|
|
105
|
-
|
|
106
|
-
static factory() {
|
|
107
|
-
return CustomType.factory<Temporal.Duration, EncodedType>({
|
|
108
|
-
decode: DurationType.transformer.decode,
|
|
109
|
-
encode: DurationType.transformer.encode,
|
|
110
|
-
type: iso.duration(),
|
|
111
|
-
error: 'Invalid duration format',
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export class PlainYearMonthType extends TransformType<
|
|
117
|
-
Temporal.PlainYearMonth,
|
|
118
|
-
EncodedType
|
|
119
|
-
> {
|
|
120
|
-
static transformer = createTemporalTransformer('PlainYearMonth')
|
|
121
|
-
|
|
122
|
-
static factory() {
|
|
123
|
-
return CustomType.factory<Temporal.PlainYearMonth, EncodedType>({
|
|
124
|
-
decode: PlainYearMonthType.transformer.decode,
|
|
125
|
-
encode: PlainYearMonthType.transformer.encode,
|
|
126
|
-
type: string().check(regex(/^\d{4}-\d{2}$/)),
|
|
127
|
-
error: 'Invalid year-month format',
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export class PlainMonthDayType extends TransformType<
|
|
133
|
-
Temporal.PlainMonthDay,
|
|
134
|
-
EncodedType
|
|
135
|
-
> {
|
|
136
|
-
static transformer = createTemporalTransformer('PlainMonthDay')
|
|
137
|
-
|
|
138
|
-
static factory() {
|
|
139
|
-
return CustomType.factory<Temporal.PlainMonthDay, EncodedType>({
|
|
140
|
-
decode: PlainMonthDayType.transformer.decode,
|
|
141
|
-
encode: PlainMonthDayType.transformer.encode,
|
|
142
|
-
type: string().check(regex(/^\d{2}-\d{2}$/)),
|
|
143
|
-
error: 'Invalid month-day format',
|
|
144
|
-
})
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export const plainDate = PlainDateType.factory
|
|
149
|
-
export const plainDatetime = PlainDateTimeType.factory
|
|
150
|
-
export const plainTime = PlainTimeType.factory
|
|
151
|
-
export const zonedDatetime = ZonedDateTimeType.factory
|
|
152
|
-
export const duration = DurationType.factory
|
|
153
|
-
export const plainYearMonth = PlainYearMonthType.factory
|
|
154
|
-
export const plainMonthDay = PlainMonthDayType.factory
|
package/src/types/tuple.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { ArrayMap } from '@nmtjs/common'
|
|
2
|
-
import * as zod from 'zod/v4-mini'
|
|
3
|
-
import { BaseType } from './base.ts'
|
|
4
|
-
|
|
5
|
-
export class TupleType<
|
|
6
|
-
T extends readonly [BaseType, ...BaseType[]] = readonly [
|
|
7
|
-
BaseType,
|
|
8
|
-
...BaseType[],
|
|
9
|
-
],
|
|
10
|
-
R extends BaseType | null = BaseType | null,
|
|
11
|
-
> extends BaseType<
|
|
12
|
-
R extends BaseType
|
|
13
|
-
? zod.ZodMiniTuple<ArrayMap<T, 'encodedZodType'>, R['encodedZodType']>
|
|
14
|
-
: zod.ZodMiniTuple<ArrayMap<T, 'encodedZodType'>, null>,
|
|
15
|
-
R extends BaseType
|
|
16
|
-
? zod.ZodMiniTuple<ArrayMap<T, 'decodedZodType'>, R['decodedZodType']>
|
|
17
|
-
: zod.ZodMiniTuple<ArrayMap<T, 'decodedZodType'>, null>,
|
|
18
|
-
{ elements: T; rest?: R }
|
|
19
|
-
> {
|
|
20
|
-
static factory<
|
|
21
|
-
T extends readonly [BaseType, ...BaseType[]],
|
|
22
|
-
R extends BaseType | null = null,
|
|
23
|
-
>(elements: T, rest: R = null as R) {
|
|
24
|
-
const encoded = elements.map((el) => el.encodedZodType)
|
|
25
|
-
const decoded = elements.map((el) => el.decodedZodType)
|
|
26
|
-
return new TupleType<T, R>({
|
|
27
|
-
// @ts-expect-error
|
|
28
|
-
encodedZodType: zod.tuple(encoded, rest?.encodedZodType),
|
|
29
|
-
// @ts-expect-error
|
|
30
|
-
decodedZodType: zod.tuple(decoded, rest?.decodedZodType),
|
|
31
|
-
props: { elements, rest },
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const tuple = TupleType.factory
|
package/src/types/type.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { BaseTypeAny } from './base.ts'
|
|
2
|
-
|
|
3
|
-
export * from './any.ts'
|
|
4
|
-
export * from './array.ts'
|
|
5
|
-
export * from './boolean.ts'
|
|
6
|
-
export * from './custom.ts'
|
|
7
|
-
export * from './date.ts'
|
|
8
|
-
export * from './enum.ts'
|
|
9
|
-
export * from './literal.ts'
|
|
10
|
-
export * from './never.ts'
|
|
11
|
-
export * from './number.ts'
|
|
12
|
-
export * from './object.ts'
|
|
13
|
-
export * from './string.ts'
|
|
14
|
-
export * from './tuple.ts'
|
|
15
|
-
export * from './union.ts'
|
|
16
|
-
|
|
17
|
-
export namespace infer {
|
|
18
|
-
export namespace decoded {
|
|
19
|
-
export type input<T extends BaseTypeAny> =
|
|
20
|
-
T['decodedZodType']['_zod']['input']
|
|
21
|
-
export type output<T extends BaseTypeAny> =
|
|
22
|
-
T['decodedZodType']['_zod']['output']
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export namespace encoded {
|
|
26
|
-
export type input<T extends BaseTypeAny> =
|
|
27
|
-
T['encodedZodType']['_zod']['input']
|
|
28
|
-
export type output<T extends BaseTypeAny> =
|
|
29
|
-
T['encodedZodType']['_zod']['output']
|
|
30
|
-
}
|
|
31
|
-
}
|
package/src/types/union.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import type { ArrayMap } from '@nmtjs/common'
|
|
2
|
-
import * as zod from 'zod/v4-mini'
|
|
3
|
-
import { BaseType, type BaseTypeAny } from './base.ts'
|
|
4
|
-
import type { LiteralType } from './literal.ts'
|
|
5
|
-
import type { ObjectType, ObjectTypeProps } from './object.ts'
|
|
6
|
-
|
|
7
|
-
export class UnionType<
|
|
8
|
-
T extends readonly [BaseType, ...BaseType[]] = readonly [
|
|
9
|
-
BaseType,
|
|
10
|
-
...BaseType[],
|
|
11
|
-
],
|
|
12
|
-
> extends BaseType<
|
|
13
|
-
zod.ZodMiniUnion<ArrayMap<T, 'encodedZodType'>>,
|
|
14
|
-
zod.ZodMiniUnion<ArrayMap<T, 'decodedZodType'>>,
|
|
15
|
-
{ options: T }
|
|
16
|
-
> {
|
|
17
|
-
static factory<
|
|
18
|
-
T extends readonly [BaseType, ...BaseType[]] = readonly [
|
|
19
|
-
BaseType,
|
|
20
|
-
...BaseType[],
|
|
21
|
-
],
|
|
22
|
-
>(...options: T) {
|
|
23
|
-
const encoded = options.map((t) => t.encodedZodType) as ArrayMap<
|
|
24
|
-
T,
|
|
25
|
-
'encodedZodType'
|
|
26
|
-
>
|
|
27
|
-
const decoded = options.map((t) => t.decodedZodType) as ArrayMap<
|
|
28
|
-
T,
|
|
29
|
-
'decodedZodType'
|
|
30
|
-
>
|
|
31
|
-
return new UnionType<T>({
|
|
32
|
-
encodedZodType: zod.union(encoded),
|
|
33
|
-
decodedZodType: zod.union(decoded),
|
|
34
|
-
props: { options },
|
|
35
|
-
})
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export class IntersactionType<
|
|
40
|
-
T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],
|
|
41
|
-
> extends BaseType<
|
|
42
|
-
zod.ZodMiniIntersection<T[0]['encodedZodType'], T[1]['encodedZodType']>,
|
|
43
|
-
zod.ZodMiniIntersection<T[0]['decodedZodType'], T[1]['decodedZodType']>,
|
|
44
|
-
{ options: T }
|
|
45
|
-
> {
|
|
46
|
-
static factory<
|
|
47
|
-
T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],
|
|
48
|
-
>(...options: T) {
|
|
49
|
-
const [first, second] = options
|
|
50
|
-
return new IntersactionType<T>({
|
|
51
|
-
encodedZodType: zod.intersection(
|
|
52
|
-
first.encodedZodType,
|
|
53
|
-
second.encodedZodType,
|
|
54
|
-
),
|
|
55
|
-
decodedZodType: zod.intersection(
|
|
56
|
-
first.decodedZodType,
|
|
57
|
-
second.decodedZodType,
|
|
58
|
-
),
|
|
59
|
-
props: { options },
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type DiscriminatedUnionProperties<K extends string = string> = {
|
|
65
|
-
[OK in K]: LiteralType<string>
|
|
66
|
-
} & {
|
|
67
|
-
[OK in string]: any
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type DiscriminatedUnionOptionType<K extends string> = ObjectType<
|
|
71
|
-
ObjectTypeProps & { [_ in K]: BaseTypeAny }
|
|
72
|
-
>
|
|
73
|
-
|
|
74
|
-
export class DiscriminatedUnionType<
|
|
75
|
-
K extends string = string,
|
|
76
|
-
T extends
|
|
77
|
-
readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],
|
|
78
|
-
> extends BaseType<
|
|
79
|
-
zod.ZodMiniDiscriminatedUnion<ArrayMap<T, 'encodedZodType'>>,
|
|
80
|
-
zod.ZodMiniDiscriminatedUnion<ArrayMap<T, 'decodedZodType'>>,
|
|
81
|
-
{
|
|
82
|
-
key: K
|
|
83
|
-
options: T
|
|
84
|
-
}
|
|
85
|
-
> {
|
|
86
|
-
static factory<
|
|
87
|
-
K extends string = string,
|
|
88
|
-
T extends
|
|
89
|
-
readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],
|
|
90
|
-
>(key: K, ...options: T) {
|
|
91
|
-
const encoded = options.map((t) => t.encodedZodType) as ArrayMap<
|
|
92
|
-
T,
|
|
93
|
-
'encodedZodType'
|
|
94
|
-
>
|
|
95
|
-
const decoded = options.map((t) => t.decodedZodType) as ArrayMap<
|
|
96
|
-
T,
|
|
97
|
-
'decodedZodType'
|
|
98
|
-
>
|
|
99
|
-
return new DiscriminatedUnionType<K, T>({
|
|
100
|
-
// @ts-expect-error
|
|
101
|
-
encodedZodType: zod.discriminatedUnion(key, encoded),
|
|
102
|
-
// @ts-expect-error
|
|
103
|
-
decodedZodType: zod.discriminatedUnion(key, decoded),
|
|
104
|
-
props: { key, options },
|
|
105
|
-
})
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export const union = UnionType.factory
|
|
110
|
-
export const or = UnionType.factory
|
|
111
|
-
export const intersection = IntersactionType.factory
|
|
112
|
-
export const and = IntersactionType.factory
|
|
113
|
-
export const discriminatedUnion = DiscriminatedUnionType.factory
|