@nmtjs/type 0.7.0 → 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.
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +7 -7
- package/dist/temporal.js.map +1 -1
- package/dist/types/array.js +5 -7
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +1 -1
- package/dist/types/base.js.map +1 -1
- package/dist/types/custom.js +3 -3
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +6 -4
- package/dist/types/date.js.map +1 -1
- package/dist/types/number.js +22 -8
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +1 -1
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +28 -10
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +43 -8
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +3 -2
- package/dist/types/union.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +10 -11
- package/src/temporal.ts +7 -7
- package/src/types/array.ts +18 -5
- package/src/types/base.ts +4 -3
- package/src/types/custom.ts +31 -17
- package/src/types/date.ts +6 -11
- package/src/types/number.ts +23 -8
- package/src/types/object.ts +3 -2
- package/src/types/string.ts +40 -11
- package/src/types/temporal.ts +52 -76
- package/src/types/union.ts +3 -8
- package/dist/utils.js +0 -0
- package/dist/utils.js.map +0 -1
- package/src/utils.ts +0 -24
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as zod from '@zod/mini'
|
|
1
2
|
import { AnyType } from './types/any.ts'
|
|
2
3
|
import { ArrayType } from './types/array.ts'
|
|
3
4
|
import type { BaseTypeAny } from './types/base.ts'
|
|
@@ -7,11 +8,7 @@ import { DateType } from './types/date.ts'
|
|
|
7
8
|
import { EnumType } from './types/enum.ts'
|
|
8
9
|
import { LiteralType } from './types/literal.ts'
|
|
9
10
|
import { NeverType } from './types/never.ts'
|
|
10
|
-
import {
|
|
11
|
-
// BigIntType,
|
|
12
|
-
IntegerType,
|
|
13
|
-
NumberType,
|
|
14
|
-
} from './types/number.ts'
|
|
11
|
+
import { BigIntType, IntegerType, NumberType } from './types/number.ts'
|
|
15
12
|
import {
|
|
16
13
|
extend,
|
|
17
14
|
keyof,
|
|
@@ -29,6 +26,8 @@ import {
|
|
|
29
26
|
UnionType,
|
|
30
27
|
} from './types/union.ts'
|
|
31
28
|
|
|
29
|
+
zod.config(zod.core.locales.en())
|
|
30
|
+
|
|
32
31
|
export { NeemataTypeError } from './types/base.ts'
|
|
33
32
|
export { BaseType, type BaseTypeAny } from './types/base.ts'
|
|
34
33
|
export {
|
|
@@ -52,17 +51,17 @@ export {
|
|
|
52
51
|
|
|
53
52
|
export namespace type {
|
|
54
53
|
export namespace infer {
|
|
55
|
-
export type decoded<T extends BaseTypeAny
|
|
54
|
+
export type decoded<T extends BaseTypeAny> =
|
|
56
55
|
T['decodedZodType']['_zod']['output']
|
|
57
56
|
|
|
58
|
-
export type encoded<T extends BaseTypeAny
|
|
57
|
+
export type encoded<T extends BaseTypeAny> =
|
|
59
58
|
T['encodedZodType']['_zod']['output']
|
|
60
59
|
|
|
61
60
|
export namespace input {
|
|
62
|
-
export type decoded<T extends BaseTypeAny
|
|
61
|
+
export type decoded<T extends BaseTypeAny> =
|
|
63
62
|
T['decodedZodType']['_zod']['input']
|
|
64
63
|
|
|
65
|
-
export type encoded<T extends BaseTypeAny
|
|
64
|
+
export type encoded<T extends BaseTypeAny> =
|
|
66
65
|
T['encodedZodType']['_zod']['input']
|
|
67
66
|
}
|
|
68
67
|
}
|
|
@@ -72,7 +71,7 @@ export namespace type {
|
|
|
72
71
|
export const string = StringType.factory
|
|
73
72
|
export const number = NumberType.factory
|
|
74
73
|
export const integer = IntegerType.factory
|
|
75
|
-
|
|
74
|
+
export const bitint = BigIntType.factory
|
|
76
75
|
export const literal = LiteralType.factory
|
|
77
76
|
export const enumeration = EnumType.factory
|
|
78
77
|
export const date = DateType.factory
|
|
@@ -95,4 +94,4 @@ export namespace type {
|
|
|
95
94
|
})
|
|
96
95
|
}
|
|
97
96
|
|
|
98
|
-
export { type as t }
|
|
97
|
+
export { type as t, zod }
|
package/src/temporal.ts
CHANGED
|
@@ -8,13 +8,13 @@ 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.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
|
|
18
18
|
|
|
19
19
|
export type {
|
|
20
20
|
DurationType,
|
package/src/types/array.ts
CHANGED
|
@@ -13,28 +13,41 @@ type Check = core.CheckFn<any[]> | core.$ZodCheck<any[]>
|
|
|
13
13
|
export class ArrayType<T extends BaseType = BaseType> extends BaseType<
|
|
14
14
|
ZodMiniArray<T['encodedZodType']>,
|
|
15
15
|
ZodMiniArray<T['decodedZodType']>,
|
|
16
|
-
{ element: T
|
|
16
|
+
{ element: T }
|
|
17
17
|
> {
|
|
18
18
|
static factory<T extends BaseType>(element: T, ...checks: Check[]) {
|
|
19
19
|
return new ArrayType<T>({
|
|
20
20
|
encodedZodType: array(element.encodedZodType).check(...checks),
|
|
21
21
|
decodedZodType: array(element.decodedZodType).check(...checks),
|
|
22
|
-
|
|
22
|
+
params: { checks },
|
|
23
|
+
props: { element },
|
|
23
24
|
})
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
min(value: number) {
|
|
27
28
|
const check = minLength(value)
|
|
28
|
-
return ArrayType.factory<T>(
|
|
29
|
+
return ArrayType.factory<T>(
|
|
30
|
+
this.props.element,
|
|
31
|
+
...this.params.checks,
|
|
32
|
+
check,
|
|
33
|
+
)
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
max(value: number) {
|
|
32
37
|
const check = maxLength(value)
|
|
33
|
-
return ArrayType.factory<T>(
|
|
38
|
+
return ArrayType.factory<T>(
|
|
39
|
+
this.props.element,
|
|
40
|
+
...this.params.checks,
|
|
41
|
+
check,
|
|
42
|
+
)
|
|
34
43
|
}
|
|
35
44
|
|
|
36
45
|
length(value: number) {
|
|
37
46
|
const check = length(value)
|
|
38
|
-
return ArrayType.factory<T>(
|
|
47
|
+
return ArrayType.factory<T>(
|
|
48
|
+
this.props.element,
|
|
49
|
+
...this.params.checks,
|
|
50
|
+
check,
|
|
51
|
+
)
|
|
39
52
|
}
|
|
40
53
|
}
|
package/src/types/base.ts
CHANGED
|
@@ -59,6 +59,7 @@ export type TypeMetadata<T = any> = {
|
|
|
59
59
|
export type TypeParams = {
|
|
60
60
|
encode?: (value: any) => any
|
|
61
61
|
metadata?: TypeMetadata
|
|
62
|
+
checks: Array<core.CheckFn<any> | core.$ZodCheck<any>>
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
export type DefaultTypeParams = {
|
|
@@ -90,18 +91,18 @@ export abstract class BaseType<
|
|
|
90
91
|
encodedZodType,
|
|
91
92
|
decodedZodType = encodedZodType as unknown as DecodedZodType,
|
|
92
93
|
props = {} as Props,
|
|
93
|
-
params = {} as TypeParams
|
|
94
|
+
params = {} as Partial<TypeParams>,
|
|
94
95
|
}: {
|
|
95
96
|
encodedZodType: EncodedZodType
|
|
96
97
|
decodedZodType?: DecodedZodType
|
|
97
98
|
props?: Props
|
|
98
|
-
params?: TypeParams
|
|
99
|
+
params?: Partial<TypeParams>
|
|
99
100
|
}) {
|
|
100
101
|
this.encodedZodType = encodedZodType
|
|
101
102
|
this.decodedZodType = decodedZodType
|
|
102
103
|
|
|
103
104
|
this.props = props
|
|
104
|
-
this.params = params
|
|
105
|
+
this.params = Object.assign({ checks: [] }, params)
|
|
105
106
|
}
|
|
106
107
|
|
|
107
108
|
optional(): OptionalType<this> {
|
package/src/types/custom.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
any,
|
|
3
|
+
type core,
|
|
3
4
|
custom,
|
|
4
5
|
overwrite,
|
|
5
6
|
pipe,
|
|
@@ -15,37 +16,50 @@ export type CustomTypeEncode<I, O> = (value: I) => O
|
|
|
15
16
|
export abstract class TransformType<
|
|
16
17
|
Type,
|
|
17
18
|
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
18
|
-
DecodedType extends ZodType = ZodMiniCustom<Type>,
|
|
19
|
-
> extends BaseType<
|
|
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
|
+
> {}
|
|
20
30
|
|
|
21
31
|
export class CustomType<
|
|
22
32
|
Type,
|
|
23
33
|
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
24
34
|
DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
|
|
25
|
-
> extends
|
|
26
|
-
ZodMiniPipe<DecodedType, EncodedType>,
|
|
27
|
-
ZodMiniPipe<EncodedType, DecodedType>
|
|
28
|
-
> {
|
|
35
|
+
> extends TransformType<Type, EncodedType, DecodedType> {
|
|
29
36
|
static factory<
|
|
30
37
|
Type,
|
|
31
38
|
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
32
|
-
DecodedType extends ZodType = ZodMiniCustom<Type>,
|
|
33
|
-
>(
|
|
39
|
+
DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
|
|
40
|
+
>({
|
|
41
|
+
decode,
|
|
42
|
+
encode,
|
|
43
|
+
error,
|
|
44
|
+
type = any() as unknown as EncodedType,
|
|
45
|
+
}: {
|
|
34
46
|
decode: CustomTypeDecode<
|
|
35
|
-
EncodedType['_zod']['
|
|
47
|
+
EncodedType['_zod']['input'],
|
|
36
48
|
DecodedType['_zod']['output']
|
|
37
|
-
|
|
49
|
+
>
|
|
38
50
|
encode: CustomTypeEncode<
|
|
39
|
-
DecodedType['_zod']['
|
|
51
|
+
DecodedType['_zod']['input'],
|
|
40
52
|
EncodedType['_zod']['output']
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
>
|
|
54
|
+
error?: string | core.$ZodErrorMap<core.$ZodIssueBase>
|
|
55
|
+
type?: EncodedType
|
|
56
|
+
}) {
|
|
44
57
|
return new CustomType<Type, EncodedType, DecodedType>({
|
|
45
|
-
//@ts-expect-error
|
|
46
58
|
encodedZodType: pipe(custom().check(overwrite(encode)), type),
|
|
47
|
-
|
|
48
|
-
|
|
59
|
+
decodedZodType: pipe(
|
|
60
|
+
type,
|
|
61
|
+
custom(undefined, { error }).check(overwrite(decode)),
|
|
62
|
+
),
|
|
49
63
|
params: { encode },
|
|
50
64
|
})
|
|
51
65
|
}
|
package/src/types/date.ts
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
date,
|
|
3
|
-
iso,
|
|
4
|
-
union,
|
|
5
|
-
type ZodMiniDate,
|
|
6
|
-
type ZodMiniUnion,
|
|
7
|
-
} from '@zod/mini'
|
|
1
|
+
import { iso, union, type ZodMiniUnion } from '@zod/mini'
|
|
8
2
|
import { CustomType, TransformType } from './custom.ts'
|
|
9
3
|
|
|
10
|
-
const decode = (value: string): Date => new Date(value)
|
|
11
|
-
const encode = (value: Date): string => value.toISOString()
|
|
12
|
-
|
|
13
4
|
export class DateType extends TransformType<
|
|
14
5
|
Date,
|
|
15
6
|
ZodMiniUnion<[iso.ZodMiniISODate, iso.ZodMiniISODateTime]>
|
|
@@ -18,6 +9,10 @@ export class DateType extends TransformType<
|
|
|
18
9
|
return CustomType.factory<
|
|
19
10
|
Date,
|
|
20
11
|
ZodMiniUnion<Array<iso.ZodMiniISODate | iso.ZodMiniISODateTime>>
|
|
21
|
-
>(
|
|
12
|
+
>({
|
|
13
|
+
decode: (value: string): Date => new Date(value),
|
|
14
|
+
encode: (value: Date): string => value.toISOString(),
|
|
15
|
+
type: union([iso.datetime(), iso.date()]),
|
|
16
|
+
})
|
|
22
17
|
}
|
|
23
18
|
}
|
package/src/types/number.ts
CHANGED
|
@@ -6,45 +6,49 @@ import {
|
|
|
6
6
|
lt,
|
|
7
7
|
lte,
|
|
8
8
|
number,
|
|
9
|
+
regex,
|
|
10
|
+
string,
|
|
9
11
|
type ZodMiniNumber,
|
|
12
|
+
type ZodMiniString,
|
|
10
13
|
} from '@zod/mini'
|
|
11
14
|
import { BaseType } from './base.ts'
|
|
15
|
+
import { CustomType, TransformType } from './custom.ts'
|
|
12
16
|
|
|
13
17
|
type Check = core.CheckFn<number> | core.$ZodCheck<number>
|
|
14
18
|
|
|
15
19
|
export class NumberType extends BaseType<
|
|
16
20
|
ZodMiniNumber<number>,
|
|
17
|
-
ZodMiniNumber<number
|
|
18
|
-
{ checks: Check[] }
|
|
21
|
+
ZodMiniNumber<number>
|
|
19
22
|
> {
|
|
20
23
|
static factory(...checks: Check[]) {
|
|
21
24
|
return new NumberType({
|
|
22
25
|
encodedZodType: number().check(...checks),
|
|
26
|
+
params: { checks },
|
|
23
27
|
})
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
positive() {
|
|
27
|
-
return NumberType.factory(...this.
|
|
31
|
+
return NumberType.factory(...this.params.checks, gte(0))
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
negative() {
|
|
31
|
-
return NumberType.factory(...this.
|
|
35
|
+
return NumberType.factory(...this.params.checks, lte(0))
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
lt(value: number) {
|
|
35
|
-
return NumberType.factory(...this.
|
|
39
|
+
return NumberType.factory(...this.params.checks, lt(value))
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
lte(value: number) {
|
|
39
|
-
return NumberType.factory(...this.
|
|
43
|
+
return NumberType.factory(...this.params.checks, lte(value))
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
gte(value: number) {
|
|
43
|
-
return NumberType.factory(...this.
|
|
47
|
+
return NumberType.factory(...this.params.checks, gte(value))
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
gt(value: number) {
|
|
47
|
-
return NumberType.factory(...this.
|
|
51
|
+
return NumberType.factory(...this.params.checks, gt(value))
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
54
|
|
|
@@ -53,3 +57,14 @@ export class IntegerType extends NumberType {
|
|
|
53
57
|
return NumberType.factory(...checks, int())
|
|
54
58
|
}
|
|
55
59
|
}
|
|
60
|
+
|
|
61
|
+
export class BigIntType extends TransformType<bigint, ZodMiniString<string>> {
|
|
62
|
+
static factory() {
|
|
63
|
+
return CustomType.factory<bigint, ZodMiniString<string>>({
|
|
64
|
+
decode: (value) => BigInt(value),
|
|
65
|
+
encode: (value) => value.toString(),
|
|
66
|
+
type: string().check(regex(/^-?\d+$/)),
|
|
67
|
+
error: 'Invalid bigint format',
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/types/object.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
type core,
|
|
3
3
|
object,
|
|
4
4
|
record,
|
|
5
|
+
strictObject,
|
|
5
6
|
type ZodMiniObject,
|
|
6
7
|
type ZodMiniRecord,
|
|
7
8
|
} from '@zod/mini'
|
|
@@ -14,8 +15,8 @@ import type { StringType } from './string.ts'
|
|
|
14
15
|
export type ObjectTypeProps = { [k: string]: BaseTypeAny }
|
|
15
16
|
export type AnyObjectType = ObjectType<ObjectTypeProps>
|
|
16
17
|
export class ObjectType<T extends ObjectTypeProps = {}> extends BaseType<
|
|
17
|
-
ZodMiniObject<{ [K in keyof T]: T[K]['encodedZodType'] }>,
|
|
18
|
-
ZodMiniObject<{ [K in keyof T]: T[K]['decodedZodType'] }>,
|
|
18
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['encodedZodType'] }, {}>,
|
|
19
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['decodedZodType'] }, {}>,
|
|
19
20
|
{ properties: T }
|
|
20
21
|
> {
|
|
21
22
|
static factory<T extends ObjectTypeProps = {}>(properties: T) {
|
package/src/types/string.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type core,
|
|
3
|
+
cuid,
|
|
4
|
+
cuid2,
|
|
5
|
+
e164,
|
|
3
6
|
email,
|
|
7
|
+
emoji,
|
|
4
8
|
ipv4,
|
|
5
9
|
ipv6,
|
|
10
|
+
jwt,
|
|
6
11
|
maxLength,
|
|
7
12
|
minLength,
|
|
13
|
+
nanoid,
|
|
8
14
|
regex,
|
|
9
15
|
string,
|
|
10
16
|
url,
|
|
@@ -18,48 +24,71 @@ type Check = core.CheckFn<string> | core.$ZodCheck<string>
|
|
|
18
24
|
|
|
19
25
|
export class StringType extends BaseType<
|
|
20
26
|
ZodMiniString<string>,
|
|
21
|
-
ZodMiniString<string
|
|
22
|
-
{ checks: Check[] }
|
|
27
|
+
ZodMiniString<string>
|
|
23
28
|
> {
|
|
24
29
|
static factory(...checks: Check[]) {
|
|
25
30
|
return new StringType({
|
|
26
31
|
encodedZodType: string().check(...checks),
|
|
27
|
-
|
|
32
|
+
params: { checks },
|
|
28
33
|
})
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
max(value: number) {
|
|
32
|
-
return StringType.factory(...this.
|
|
37
|
+
return StringType.factory(...this.params.checks, maxLength(value))
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
min(value: number) {
|
|
36
|
-
return StringType.factory(...this.
|
|
41
|
+
return StringType.factory(...this.params.checks, minLength(value))
|
|
37
42
|
}
|
|
38
43
|
|
|
39
44
|
pattern(pattern: string | RegExp) {
|
|
40
45
|
return StringType.factory(
|
|
41
|
-
...this.
|
|
46
|
+
...this.params.checks,
|
|
42
47
|
regex(typeof pattern === 'string' ? new RegExp(pattern) : pattern),
|
|
43
48
|
)
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
email(options?: core.$ZodEmailParams) {
|
|
47
|
-
return StringType.factory(...this.
|
|
52
|
+
return StringType.factory(...this.params.checks, email(options))
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
url(options?: core.$ZodURLParams) {
|
|
51
|
-
return StringType.factory(...this.
|
|
56
|
+
return StringType.factory(...this.params.checks, url(options))
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
ipv4(options?: core.$ZodIPv4Params) {
|
|
55
|
-
return StringType.factory(...this.
|
|
60
|
+
return StringType.factory(...this.params.checks, ipv4(options))
|
|
56
61
|
}
|
|
57
62
|
|
|
58
63
|
ipv6(options?: core.$ZodIPv6Params) {
|
|
59
|
-
return StringType.factory(...this.
|
|
64
|
+
return StringType.factory(...this.params.checks, ipv6(options))
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
uuid(options?: core.$ZodUUIDParams) {
|
|
63
|
-
return StringType.factory(...this.
|
|
68
|
+
return StringType.factory(...this.params.checks, uuid(options))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
emoji(options?: core.$ZodEmojiParams) {
|
|
72
|
+
return StringType.factory(...this.params.checks, emoji(options))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
nanoid(options?: core.$ZodNanoIDParams) {
|
|
76
|
+
return StringType.factory(...this.params.checks, nanoid(options))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
cuid(options?: core.$ZodCUIDParams) {
|
|
80
|
+
return StringType.factory(...this.params.checks, cuid(options))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
cuid2(options?: core.$ZodCUID2Params) {
|
|
84
|
+
return StringType.factory(...this.params.checks, cuid2(options))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
e164(options?: core.$ZodE164Params) {
|
|
88
|
+
return StringType.factory(...this.params.checks, e164(options))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
jwt(options?: core.$ZodJWTParams) {
|
|
92
|
+
return StringType.factory(...this.params.checks, jwt(options))
|
|
64
93
|
}
|
|
65
94
|
}
|
package/src/types/temporal.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
custom,
|
|
3
|
-
string,
|
|
4
|
-
type ZodMiniCustom,
|
|
5
|
-
type ZodMiniString,
|
|
6
|
-
} from '@zod/mini'
|
|
1
|
+
import { iso, regex, string, type ZodMiniString } from '@zod/mini'
|
|
7
2
|
import { Temporal } from 'temporal-polyfill'
|
|
8
3
|
import { CustomType, TransformType } from './custom.ts'
|
|
9
4
|
|
|
@@ -34,137 +29,118 @@ const createTemporalTransformer = <T extends Types>(
|
|
|
34
29
|
} as TemporalTransformer<T>
|
|
35
30
|
}
|
|
36
31
|
|
|
32
|
+
type EncodedType = ZodMiniString<string>
|
|
33
|
+
|
|
37
34
|
export class PlainDateType extends TransformType<
|
|
38
35
|
Temporal.PlainDate,
|
|
39
|
-
|
|
36
|
+
EncodedType
|
|
40
37
|
> {
|
|
41
38
|
static transformer = createTemporalTransformer('PlainDate')
|
|
42
39
|
|
|
43
40
|
static factory() {
|
|
44
|
-
return CustomType.factory<
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
PlainDateType.transformer.encode,
|
|
51
|
-
string(),
|
|
52
|
-
)
|
|
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
|
+
})
|
|
53
47
|
}
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
export class PlainDateTimeType extends TransformType<
|
|
57
51
|
Temporal.PlainDateTime,
|
|
58
|
-
|
|
52
|
+
EncodedType
|
|
59
53
|
> {
|
|
60
54
|
static transformer = createTemporalTransformer('PlainDateTime')
|
|
61
55
|
|
|
62
56
|
static factory() {
|
|
63
|
-
return CustomType.factory<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
PlainDateTimeType.transformer.encode,
|
|
70
|
-
string(),
|
|
71
|
-
)
|
|
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
|
+
})
|
|
72
63
|
}
|
|
73
64
|
}
|
|
74
65
|
|
|
75
66
|
export class ZonedDateTimeType extends TransformType<
|
|
76
67
|
Temporal.ZonedDateTime,
|
|
77
|
-
|
|
68
|
+
EncodedType
|
|
78
69
|
> {
|
|
79
70
|
static transformer = createTemporalTransformer('ZonedDateTime', (value) =>
|
|
80
71
|
Temporal.Instant.from(value).toZonedDateTimeISO('UTC'),
|
|
81
72
|
)
|
|
82
73
|
|
|
83
74
|
static factory() {
|
|
84
|
-
return CustomType.factory<
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
ZonedDateTimeType.transformer.encode,
|
|
91
|
-
string(),
|
|
92
|
-
)
|
|
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
|
+
})
|
|
93
81
|
}
|
|
94
82
|
}
|
|
95
83
|
|
|
96
84
|
export class PlainTimeType extends TransformType<
|
|
97
85
|
Temporal.PlainTime,
|
|
98
|
-
|
|
86
|
+
EncodedType
|
|
99
87
|
> {
|
|
100
88
|
static transformer = createTemporalTransformer('PlainTime')
|
|
101
89
|
|
|
102
90
|
static factory() {
|
|
103
|
-
return CustomType.factory<
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
PlainTimeType.transformer.encode,
|
|
110
|
-
string(),
|
|
111
|
-
)
|
|
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
|
+
})
|
|
112
97
|
}
|
|
113
98
|
}
|
|
114
99
|
|
|
115
100
|
export class DurationType extends TransformType<
|
|
116
101
|
Temporal.Duration,
|
|
117
|
-
|
|
102
|
+
EncodedType
|
|
118
103
|
> {
|
|
119
104
|
static transformer = createTemporalTransformer('Duration')
|
|
120
105
|
|
|
121
106
|
static factory() {
|
|
122
|
-
return CustomType.factory<
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
DurationType.transformer.encode,
|
|
129
|
-
string(),
|
|
130
|
-
)
|
|
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
|
+
})
|
|
131
113
|
}
|
|
132
114
|
}
|
|
133
115
|
|
|
134
116
|
export class PlainYearMonthType extends TransformType<
|
|
135
117
|
Temporal.PlainYearMonth,
|
|
136
|
-
|
|
118
|
+
EncodedType
|
|
137
119
|
> {
|
|
138
120
|
static transformer = createTemporalTransformer('PlainYearMonth')
|
|
139
121
|
|
|
140
122
|
static factory() {
|
|
141
|
-
return CustomType.factory<
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
PlainYearMonthType.transformer.encode,
|
|
148
|
-
string(),
|
|
149
|
-
)
|
|
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
|
+
})
|
|
150
129
|
}
|
|
151
130
|
}
|
|
152
131
|
|
|
153
132
|
export class PlainMonthDayType extends TransformType<
|
|
154
133
|
Temporal.PlainMonthDay,
|
|
155
|
-
|
|
134
|
+
EncodedType
|
|
156
135
|
> {
|
|
157
136
|
static transformer = createTemporalTransformer('PlainMonthDay')
|
|
158
137
|
|
|
159
138
|
static factory() {
|
|
160
|
-
return CustomType.factory<
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
PlainMonthDayType.transformer.encode,
|
|
167
|
-
string(),
|
|
168
|
-
)
|
|
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
|
+
})
|
|
169
145
|
}
|
|
170
146
|
}
|
package/src/types/union.ts
CHANGED
|
@@ -39,15 +39,10 @@ export class IntersactionType<
|
|
|
39
39
|
static factory<
|
|
40
40
|
T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],
|
|
41
41
|
>(...options: T) {
|
|
42
|
+
const [first, second] = options
|
|
42
43
|
return new IntersactionType<T>({
|
|
43
|
-
encodedZodType: intersection(
|
|
44
|
-
|
|
45
|
-
options[1].encodedZodType,
|
|
46
|
-
),
|
|
47
|
-
decodedZodType: intersection(
|
|
48
|
-
options[0].decodedZodType,
|
|
49
|
-
options[1].decodedZodType,
|
|
50
|
-
),
|
|
44
|
+
encodedZodType: intersection(first.encodedZodType, second.encodedZodType),
|
|
45
|
+
decodedZodType: intersection(first.decodedZodType, second.decodedZodType),
|
|
51
46
|
props: { options },
|
|
52
47
|
})
|
|
53
48
|
}
|
package/dist/utils.js
DELETED
|
File without changes
|