@nmtjs/type 0.14.5 → 0.15.0-beta.10
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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -0
- package/dist/temporal/global.d.ts +8 -7
- package/dist/temporal/global.js +1 -0
- package/dist/temporal/global.js.map +1 -0
- package/dist/temporal/polyfill.d.ts +8 -8
- package/dist/temporal/polyfill.js +1 -0
- package/dist/temporal/polyfill.js.map +1 -0
- package/dist/types/_convert.d.ts +5 -0
- package/dist/types/_convert.js +56 -0
- package/dist/types/_convert.js.map +1 -0
- package/dist/types/_type.d.ts +1 -0
- package/dist/types/_type.js +2 -0
- package/dist/types/_type.js.map +1 -0
- package/dist/types/{_plain.d.ts → _utils.d.ts} +2 -0
- package/dist/types/{_plain.js → _utils.js} +1 -0
- package/dist/types/_utils.js.map +1 -0
- package/dist/types/any.js +1 -0
- package/dist/types/any.js.map +1 -0
- package/dist/types/array.d.ts +1 -1
- package/dist/types/array.js +1 -0
- package/dist/types/array.js.map +1 -0
- package/dist/types/base.js +1 -0
- package/dist/types/base.js.map +1 -0
- package/dist/types/boolean.js +1 -0
- package/dist/types/boolean.js.map +1 -0
- package/dist/types/custom.d.ts +7 -1
- package/dist/types/custom.js +22 -5
- package/dist/types/custom.js.map +1 -0
- package/dist/types/date.d.ts +2 -2
- package/dist/types/date.js +2 -0
- package/dist/types/date.js.map +1 -0
- package/dist/types/enum.js +1 -0
- package/dist/types/enum.js.map +1 -0
- package/dist/types/literal.js +1 -0
- package/dist/types/literal.js.map +1 -0
- package/dist/types/never.js +1 -0
- package/dist/types/never.js.map +1 -0
- package/dist/types/null.d.ts +7 -0
- package/dist/types/null.js +10 -0
- package/dist/types/null.js.map +1 -0
- package/dist/types/number.js +1 -0
- package/dist/types/number.js.map +1 -0
- package/dist/types/object.d.ts +42 -15
- package/dist/types/object.js +19 -2
- package/dist/types/object.js.map +1 -0
- package/dist/types/string.js +1 -0
- package/dist/types/string.js.map +1 -0
- package/dist/types/temporal.d.ts +8 -8
- package/dist/types/temporal.js +1 -0
- package/dist/types/temporal.js.map +1 -0
- package/dist/types/tuple.d.ts +1 -1
- package/dist/types/tuple.js +1 -0
- package/dist/types/tuple.js.map +1 -0
- package/dist/types/union.d.ts +1 -1
- package/dist/types/union.js +1 -0
- package/dist/types/union.js.map +1 -0
- package/package.json +13 -26
- package/src/index.ts +14 -0
- package/src/temporal/global.ts +32 -0
- package/src/temporal/polyfill.ts +31 -0
- package/src/types/_convert.ts +68 -0
- package/src/types/_type.ts +46 -0
- package/src/types/_utils.ts +24 -0
- package/src/types/any.ts +12 -0
- package/src/types/array.ts +53 -0
- package/src/types/base.ts +209 -0
- package/src/types/boolean.ts +12 -0
- package/src/types/custom.ts +109 -0
- package/src/types/date.ts +23 -0
- package/src/types/enum.ts +23 -0
- package/src/types/literal.ts +18 -0
- package/src/types/never.ts +12 -0
- package/src/types/null.ts +14 -0
- package/src/types/number.ts +73 -0
- package/src/types/object.ts +225 -0
- package/src/types/string.ts +95 -0
- package/src/types/temporal.ts +155 -0
- package/src/types/tuple.ts +49 -0
- package/src/types/union.ts +120 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ZodMiniBoolean } from 'zod/mini'
|
|
2
|
+
import { boolean as zodBoolean } from 'zod/mini'
|
|
3
|
+
|
|
4
|
+
import { BaseType } from './base.ts'
|
|
5
|
+
|
|
6
|
+
export class BooleanType extends BaseType<ZodMiniBoolean<boolean>> {
|
|
7
|
+
static factory() {
|
|
8
|
+
return new BooleanType({ encodeZodType: zodBoolean() })
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const boolean = BooleanType.factory
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Async } from '@nmtjs/common'
|
|
2
|
+
import type { core, ZodMiniType } from 'zod/mini'
|
|
3
|
+
import {
|
|
4
|
+
any,
|
|
5
|
+
overwrite,
|
|
6
|
+
pipe,
|
|
7
|
+
refine,
|
|
8
|
+
superRefine,
|
|
9
|
+
custom as zodCustom,
|
|
10
|
+
} from 'zod/mini'
|
|
11
|
+
|
|
12
|
+
import type { SimpleZodType, ZodType } from './base.ts'
|
|
13
|
+
import { BaseType } from './base.ts'
|
|
14
|
+
|
|
15
|
+
export type CustomTransformFn<I, O> = (value: I) => O
|
|
16
|
+
export abstract class TransformType<
|
|
17
|
+
Type,
|
|
18
|
+
EncodeType extends SimpleZodType = ZodMiniType<Type, Type>,
|
|
19
|
+
DecodeType extends ZodType = ZodMiniType<Type, Type>,
|
|
20
|
+
> extends BaseType<
|
|
21
|
+
ZodMiniType<EncodeType['_zod']['output'], DecodeType['_zod']['input']>,
|
|
22
|
+
ZodMiniType<DecodeType['_zod']['output'], EncodeType['_zod']['input']>
|
|
23
|
+
> {}
|
|
24
|
+
|
|
25
|
+
export class CustomType<
|
|
26
|
+
Type,
|
|
27
|
+
EncodeType extends SimpleZodType = ZodMiniType<Type, Type>,
|
|
28
|
+
DecodeType extends ZodType = ZodMiniType<Type, Type>,
|
|
29
|
+
> extends TransformType<Type, EncodeType, DecodeType> {
|
|
30
|
+
static factory<
|
|
31
|
+
Type,
|
|
32
|
+
EncodeType extends SimpleZodType = ZodMiniType<Type, Type>,
|
|
33
|
+
DecodeType extends ZodType = ZodMiniType<Type, Type>,
|
|
34
|
+
>({
|
|
35
|
+
decode,
|
|
36
|
+
encode,
|
|
37
|
+
validation,
|
|
38
|
+
error,
|
|
39
|
+
type = any() as unknown as EncodeType,
|
|
40
|
+
prototype,
|
|
41
|
+
}: {
|
|
42
|
+
decode: CustomTransformFn<
|
|
43
|
+
EncodeType['_zod']['input'],
|
|
44
|
+
DecodeType['_zod']['output']
|
|
45
|
+
>
|
|
46
|
+
encode: CustomTransformFn<
|
|
47
|
+
DecodeType['_zod']['input'],
|
|
48
|
+
EncodeType['_zod']['output']
|
|
49
|
+
>
|
|
50
|
+
validation?:
|
|
51
|
+
| ((
|
|
52
|
+
value: EncodeType['_zod']['input'] | DecodeType['_zod']['output'],
|
|
53
|
+
|
|
54
|
+
payload: core.$RefinementCtx<
|
|
55
|
+
EncodeType['_zod']['output'] | DecodeType['_zod']['output']
|
|
56
|
+
>,
|
|
57
|
+
) => Async<void>)
|
|
58
|
+
| {
|
|
59
|
+
encode?: (
|
|
60
|
+
value: EncodeType['_zod']['input'],
|
|
61
|
+
payload: core.$RefinementCtx<EncodeType['_zod']['output']>,
|
|
62
|
+
) => Async<void>
|
|
63
|
+
decode?: (
|
|
64
|
+
value: DecodeType['_zod']['output'],
|
|
65
|
+
payload: core.$RefinementCtx<DecodeType['_zod']['output']>,
|
|
66
|
+
) => Async<void>
|
|
67
|
+
}
|
|
68
|
+
error?: string | core.$ZodErrorMap<core.$ZodIssueBase>
|
|
69
|
+
type?: EncodeType
|
|
70
|
+
prototype?: object
|
|
71
|
+
}): CustomType<Type, EncodeType, DecodeType> {
|
|
72
|
+
const _validation = validation
|
|
73
|
+
? typeof validation === 'function'
|
|
74
|
+
? { encode: validation, decode: validation }
|
|
75
|
+
: validation
|
|
76
|
+
: undefined
|
|
77
|
+
|
|
78
|
+
const instance = new CustomType<Type, EncodeType, DecodeType>({
|
|
79
|
+
encodeZodType: pipe(
|
|
80
|
+
zodCustom().check(
|
|
81
|
+
...[
|
|
82
|
+
refine((val) => typeof val !== 'undefined', { abort: true }),
|
|
83
|
+
_validation?.encode ? superRefine(_validation.encode) : undefined,
|
|
84
|
+
overwrite(encode),
|
|
85
|
+
].filter((v) => !!v),
|
|
86
|
+
),
|
|
87
|
+
type,
|
|
88
|
+
),
|
|
89
|
+
decodeZodType: pipe(
|
|
90
|
+
type,
|
|
91
|
+
// @ts-expect-error
|
|
92
|
+
zodCustom().check(
|
|
93
|
+
...[
|
|
94
|
+
refine((val) => typeof val !== 'undefined', { abort: true }),
|
|
95
|
+
overwrite(decode),
|
|
96
|
+
_validation?.decode ? superRefine(_validation.decode) : undefined,
|
|
97
|
+
].filter((v) => !!v),
|
|
98
|
+
),
|
|
99
|
+
),
|
|
100
|
+
params: { encode },
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
if (prototype) Object.setPrototypeOf(instance, prototype)
|
|
104
|
+
|
|
105
|
+
return instance
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const custom = CustomType.factory
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ZodMiniUnion } from 'zod/mini'
|
|
2
|
+
import { iso, union } from 'zod/mini'
|
|
3
|
+
|
|
4
|
+
import { CustomType, TransformType } from './custom.ts'
|
|
5
|
+
|
|
6
|
+
export class DateType extends TransformType<
|
|
7
|
+
Date,
|
|
8
|
+
ZodMiniUnion<[iso.ZodMiniISODate, iso.ZodMiniISODateTime]>
|
|
9
|
+
> {
|
|
10
|
+
static factory(): DateType {
|
|
11
|
+
return CustomType.factory<
|
|
12
|
+
Date,
|
|
13
|
+
ZodMiniUnion<[iso.ZodMiniISODate, iso.ZodMiniISODateTime]>
|
|
14
|
+
>({
|
|
15
|
+
decode: (value) => new Date(value),
|
|
16
|
+
encode: (value) => value.toISOString(),
|
|
17
|
+
type: union([iso.date(), iso.datetime()]),
|
|
18
|
+
prototype: DateType.prototype,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const date = DateType.factory
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { core, ZodMiniEnum } from 'zod/mini'
|
|
2
|
+
import { enum as zodEnum } from 'zod/mini'
|
|
3
|
+
|
|
4
|
+
import { BaseType } from './base.ts'
|
|
5
|
+
|
|
6
|
+
export class EnumType<
|
|
7
|
+
T extends core.util.EnumLike = core.util.EnumLike,
|
|
8
|
+
> extends BaseType<ZodMiniEnum<T>, ZodMiniEnum<T>, { values: T }> {
|
|
9
|
+
static factory<T extends core.util.EnumLike>(values: T): EnumType<T>
|
|
10
|
+
static factory<T extends string[]>(
|
|
11
|
+
values: T,
|
|
12
|
+
): EnumType<core.util.ToEnum<T[number]>>
|
|
13
|
+
static factory<T extends core.util.EnumLike | string[]>(values: T) {
|
|
14
|
+
return new EnumType({
|
|
15
|
+
encodeZodType: zodEnum(values as any),
|
|
16
|
+
props: { values },
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const _enum = EnumType.factory
|
|
22
|
+
|
|
23
|
+
export { _enum as enum }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ZodMiniLiteral } from 'zod/mini'
|
|
2
|
+
import { literal as zodLiteral } from 'zod/mini'
|
|
3
|
+
|
|
4
|
+
import type { PrimitiveValueType } from './base.ts'
|
|
5
|
+
import { BaseType } from './base.ts'
|
|
6
|
+
|
|
7
|
+
export class LiteralType<
|
|
8
|
+
T extends PrimitiveValueType = PrimitiveValueType,
|
|
9
|
+
> extends BaseType<ZodMiniLiteral<T>, ZodMiniLiteral<T>, { value: T }> {
|
|
10
|
+
static factory<T extends PrimitiveValueType>(value: T) {
|
|
11
|
+
return new LiteralType<T>({
|
|
12
|
+
encodeZodType: zodLiteral(value),
|
|
13
|
+
props: { value },
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const literal = LiteralType.factory
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ZodMiniNever } from 'zod/mini'
|
|
2
|
+
import { never as zodNever } from 'zod/mini'
|
|
3
|
+
|
|
4
|
+
import { BaseType } from './base.ts'
|
|
5
|
+
|
|
6
|
+
export class NeverType extends BaseType<ZodMiniNever> {
|
|
7
|
+
static factory() {
|
|
8
|
+
return new NeverType({ encodeZodType: zodNever() })
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const never = NeverType.factory
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ZodMiniNull } from 'zod/mini'
|
|
2
|
+
import { null as zodNull } from 'zod/mini'
|
|
3
|
+
|
|
4
|
+
import { BaseType } from './base.ts'
|
|
5
|
+
|
|
6
|
+
export class NullType extends BaseType<ZodMiniNull> {
|
|
7
|
+
static factory() {
|
|
8
|
+
return new NullType({ encodeZodType: zodNull() })
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const _null = NullType.factory
|
|
13
|
+
|
|
14
|
+
export { _null as null }
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { core, ZodMiniNumber, ZodMiniString } from 'zod/mini'
|
|
2
|
+
import {
|
|
3
|
+
gt,
|
|
4
|
+
gte,
|
|
5
|
+
int,
|
|
6
|
+
lt,
|
|
7
|
+
lte,
|
|
8
|
+
regex,
|
|
9
|
+
number as zodNumber,
|
|
10
|
+
string as zodString,
|
|
11
|
+
} from 'zod/mini'
|
|
12
|
+
|
|
13
|
+
import { BaseType } from './base.ts'
|
|
14
|
+
import { CustomType, TransformType } from './custom.ts'
|
|
15
|
+
|
|
16
|
+
type Check = core.CheckFn<number> | core.$ZodCheck<number>
|
|
17
|
+
|
|
18
|
+
export class NumberType extends BaseType<
|
|
19
|
+
ZodMiniNumber<number>,
|
|
20
|
+
ZodMiniNumber<number>
|
|
21
|
+
> {
|
|
22
|
+
static factory(...checks: Check[]) {
|
|
23
|
+
return new NumberType({
|
|
24
|
+
encodeZodType: zodNumber().check(...checks),
|
|
25
|
+
params: { checks },
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
positive() {
|
|
30
|
+
return NumberType.factory(...this.params.checks, gte(0))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
negative() {
|
|
34
|
+
return NumberType.factory(...this.params.checks, lte(0))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
lt(value: number) {
|
|
38
|
+
return NumberType.factory(...this.params.checks, lt(value))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
lte(value: number) {
|
|
42
|
+
return NumberType.factory(...this.params.checks, lte(value))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
gte(value: number) {
|
|
46
|
+
return NumberType.factory(...this.params.checks, gte(value))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
gt(value: number) {
|
|
50
|
+
return NumberType.factory(...this.params.checks, gt(value))
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class IntegerType extends NumberType {
|
|
55
|
+
static factory(...checks: Check[]) {
|
|
56
|
+
return NumberType.factory(...checks, int())
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class BigIntType extends TransformType<bigint, ZodMiniString<string>> {
|
|
61
|
+
static factory() {
|
|
62
|
+
return CustomType.factory<bigint, ZodMiniString<string>>({
|
|
63
|
+
decode: (value) => BigInt(value),
|
|
64
|
+
encode: (value) => value.toString(),
|
|
65
|
+
type: zodString().check(regex(/^-?\d+$/)),
|
|
66
|
+
error: 'Invalid bigint format',
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const number = NumberType.factory
|
|
72
|
+
export const integer = IntegerType.factory
|
|
73
|
+
export const bigInt = BigIntType.factory
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { core, ZodMiniObject, ZodMiniRecord } from 'zod/mini'
|
|
2
|
+
import {
|
|
3
|
+
looseObject as zodLooseObject,
|
|
4
|
+
object as zodObject,
|
|
5
|
+
record as zodRecord,
|
|
6
|
+
} from 'zod/mini'
|
|
7
|
+
|
|
8
|
+
import type { ZodPlainType } from './_utils.ts'
|
|
9
|
+
import type { BaseTypeAny, OptionalType } from './base.ts'
|
|
10
|
+
import type { LiteralType } from './literal.ts'
|
|
11
|
+
import type { StringType } from './string.ts'
|
|
12
|
+
import { zodPlainType } from './_utils.ts'
|
|
13
|
+
import { BaseType } from './base.ts'
|
|
14
|
+
import { EnumType } from './enum.ts'
|
|
15
|
+
|
|
16
|
+
export type ObjectTypeProps = { [k: string]: BaseTypeAny }
|
|
17
|
+
|
|
18
|
+
export class ObjectType<T extends ObjectTypeProps = {}> extends BaseType<
|
|
19
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['encodeZodType'] }, core.$strip>,
|
|
20
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['decodeZodType'] }, core.$strip>,
|
|
21
|
+
{ properties: T },
|
|
22
|
+
ZodPlainType<
|
|
23
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['encodeZodType'] }, core.$strip>
|
|
24
|
+
>,
|
|
25
|
+
ZodPlainType<
|
|
26
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['decodeZodType'] }, core.$strip>
|
|
27
|
+
>
|
|
28
|
+
> {
|
|
29
|
+
static factory<T extends ObjectTypeProps = {}>(properties: T) {
|
|
30
|
+
const encodeProperties = {} as {
|
|
31
|
+
[K in keyof T]: T[K]['encodeZodType']
|
|
32
|
+
}
|
|
33
|
+
const decodeProperties = {} as {
|
|
34
|
+
[K in keyof T]: T[K]['decodeZodType']
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
for (const key in properties) {
|
|
38
|
+
encodeProperties[key] = properties[key].encodeZodType
|
|
39
|
+
decodeProperties[key] = properties[key].decodeZodType
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return new ObjectType<T>({
|
|
43
|
+
encodeZodType: zodObject(encodeProperties),
|
|
44
|
+
decodeZodType: zodObject(decodeProperties),
|
|
45
|
+
props: { properties },
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class LooseObjectType<T extends ObjectTypeProps = {}> extends BaseType<
|
|
51
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['encodeZodType'] }, core.$loose>,
|
|
52
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['decodeZodType'] }, core.$loose>,
|
|
53
|
+
{ properties: T },
|
|
54
|
+
ZodPlainType<
|
|
55
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['encodeZodType'] }, core.$loose>
|
|
56
|
+
>,
|
|
57
|
+
ZodPlainType<
|
|
58
|
+
ZodMiniObject<{ [K in keyof T]: T[K]['decodeZodType'] }, core.$loose>
|
|
59
|
+
>
|
|
60
|
+
> {
|
|
61
|
+
static factory<T extends ObjectTypeProps = {}>(properties: T) {
|
|
62
|
+
const encodeProperties = {} as {
|
|
63
|
+
[K in keyof T]: T[K]['encodeZodType']
|
|
64
|
+
}
|
|
65
|
+
const decodeProperties = {} as {
|
|
66
|
+
[K in keyof T]: T[K]['decodeZodType']
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const key in properties) {
|
|
70
|
+
encodeProperties[key] = properties[key].encodeZodType
|
|
71
|
+
decodeProperties[key] = properties[key].decodeZodType
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return new LooseObjectType<T>({
|
|
75
|
+
encodeZodType: zodLooseObject(encodeProperties),
|
|
76
|
+
decodeZodType: zodLooseObject(decodeProperties),
|
|
77
|
+
props: { properties },
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type ObjectLikeType<T extends ObjectTypeProps> =
|
|
83
|
+
| ObjectType<T>
|
|
84
|
+
| LooseObjectType<T>
|
|
85
|
+
|
|
86
|
+
export type AnyObjectType = ObjectType<ObjectTypeProps>
|
|
87
|
+
export type AnyLooseObjectType = LooseObjectType<ObjectTypeProps>
|
|
88
|
+
export type AnyObjectLikeType = AnyObjectType | AnyLooseObjectType
|
|
89
|
+
|
|
90
|
+
export class RecordType<
|
|
91
|
+
K extends LiteralType<string | number> | EnumType | StringType,
|
|
92
|
+
E extends BaseType,
|
|
93
|
+
> extends BaseType<
|
|
94
|
+
ZodMiniRecord<K['encodeZodType'], E['encodeZodType']>,
|
|
95
|
+
ZodMiniRecord<K['decodeZodType'], E['decodeZodType']>,
|
|
96
|
+
{ key: K; element: E },
|
|
97
|
+
ZodPlainType<ZodMiniRecord<K['encodeZodType'], E['encodeZodType']>>,
|
|
98
|
+
ZodPlainType<ZodMiniRecord<K['decodeZodType'], E['decodeZodType']>>
|
|
99
|
+
> {
|
|
100
|
+
static factory<
|
|
101
|
+
K extends LiteralType<string | number> | EnumType | StringType,
|
|
102
|
+
E extends BaseType,
|
|
103
|
+
>(key: K, element: E) {
|
|
104
|
+
return new RecordType<K, E>({
|
|
105
|
+
encodeZodType: zodPlainType(
|
|
106
|
+
zodRecord(key.encodeZodType, element.encodeZodType),
|
|
107
|
+
),
|
|
108
|
+
decodeZodType: zodPlainType(
|
|
109
|
+
zodRecord(key.decodeZodType, element.decodeZodType),
|
|
110
|
+
),
|
|
111
|
+
props: { key, element },
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type KeyofType<T extends AnyObjectLikeType> = EnumType<
|
|
117
|
+
core.util.ToEnum<Extract<keyof T['props']['properties'], string>>
|
|
118
|
+
>
|
|
119
|
+
|
|
120
|
+
export function keyof<T extends AnyObjectLikeType>(type: T): KeyofType<T> {
|
|
121
|
+
return EnumType.factory(Object.keys(type.props.properties) as any)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type PickObjectType<
|
|
125
|
+
T extends AnyObjectLikeType,
|
|
126
|
+
P extends { [K in keyof T['props']['properties']]?: true },
|
|
127
|
+
> = ObjectType<{
|
|
128
|
+
[K in keyof P]: P[K] extends true
|
|
129
|
+
? K extends keyof T['props']['properties']
|
|
130
|
+
? T['props']['properties'][K]
|
|
131
|
+
: never
|
|
132
|
+
: never
|
|
133
|
+
}>
|
|
134
|
+
export function pick<
|
|
135
|
+
T extends AnyObjectLikeType,
|
|
136
|
+
P extends { [K in keyof T['props']['properties']]?: true },
|
|
137
|
+
>(source: T, pick: P): PickObjectType<T, P> {
|
|
138
|
+
const properties = Object.fromEntries(
|
|
139
|
+
Object.entries(source.props.properties).filter(([key]) => pick[key]),
|
|
140
|
+
)
|
|
141
|
+
return ObjectType.factory(properties) as any
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type OmitObjectType<
|
|
145
|
+
T extends AnyObjectLikeType,
|
|
146
|
+
P extends { [K in keyof T['props']['properties']]?: true },
|
|
147
|
+
> = ObjectType<{
|
|
148
|
+
[K in keyof T['props']['properties'] as K extends keyof P
|
|
149
|
+
? never
|
|
150
|
+
: K]: T['props']['properties'][K]
|
|
151
|
+
}>
|
|
152
|
+
export function omit<
|
|
153
|
+
T extends AnyObjectLikeType,
|
|
154
|
+
P extends { [K in keyof T['props']['properties']]?: true },
|
|
155
|
+
>(source: T, omit: P): OmitObjectType<T, P> {
|
|
156
|
+
const properties = Object.fromEntries(
|
|
157
|
+
Object.entries(source.props.properties).filter(([key]) => !omit[key]),
|
|
158
|
+
)
|
|
159
|
+
return ObjectType.factory(properties) as any
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export type ExtendObjectType<
|
|
163
|
+
T extends AnyObjectLikeType,
|
|
164
|
+
P extends ObjectTypeProps,
|
|
165
|
+
> = ObjectType<{
|
|
166
|
+
[K in keyof T['props']['properties'] | keyof P]: K extends keyof P
|
|
167
|
+
? P[K]
|
|
168
|
+
: K extends keyof T['props']['properties']
|
|
169
|
+
? T['props']['properties'][K]
|
|
170
|
+
: never
|
|
171
|
+
}>
|
|
172
|
+
|
|
173
|
+
export function extend<T extends AnyObjectLikeType, P extends ObjectTypeProps>(
|
|
174
|
+
object1: T,
|
|
175
|
+
properties: P,
|
|
176
|
+
): ExtendObjectType<T, P> {
|
|
177
|
+
return ObjectType.factory({
|
|
178
|
+
...object1.props.properties,
|
|
179
|
+
...properties,
|
|
180
|
+
}) as any
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export type MergeObjectTypes<
|
|
184
|
+
T1 extends AnyObjectLikeType,
|
|
185
|
+
T2 extends AnyObjectLikeType,
|
|
186
|
+
> = ObjectType<{
|
|
187
|
+
[K in
|
|
188
|
+
| keyof T1['props']['properties']
|
|
189
|
+
| keyof T2['props']['properties']]: K extends keyof T2['props']['properties']
|
|
190
|
+
? T2['props']['properties'][K]
|
|
191
|
+
: K extends keyof T1['props']['properties']
|
|
192
|
+
? T1['props']['properties'][K]
|
|
193
|
+
: never
|
|
194
|
+
}>
|
|
195
|
+
|
|
196
|
+
export function merge<
|
|
197
|
+
T1 extends AnyObjectLikeType,
|
|
198
|
+
T2 extends AnyObjectLikeType,
|
|
199
|
+
>(object1: T1, object2: T2): MergeObjectTypes<T1, T2> {
|
|
200
|
+
return ObjectType.factory({
|
|
201
|
+
...object1.props.properties,
|
|
202
|
+
...object2.props.properties,
|
|
203
|
+
}) as any
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export type PartialObjectType<T extends AnyObjectLikeType> = ObjectType<{
|
|
207
|
+
[K in keyof T['props']['properties']]: OptionalType<
|
|
208
|
+
T['props']['properties'][K]
|
|
209
|
+
>
|
|
210
|
+
}>
|
|
211
|
+
export function partial<T extends AnyObjectLikeType>(
|
|
212
|
+
object: T,
|
|
213
|
+
): PartialObjectType<T> {
|
|
214
|
+
const properties = {} as any
|
|
215
|
+
|
|
216
|
+
for (const [key, value] of Object.entries(object.props.properties)) {
|
|
217
|
+
properties[key] = value.optional()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return ObjectType.factory(properties)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export const object = ObjectType.factory
|
|
224
|
+
export const looseObject = LooseObjectType.factory
|
|
225
|
+
export const record = RecordType.factory
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { core, ZodMiniString } from 'zod/mini'
|
|
2
|
+
import {
|
|
3
|
+
cuid,
|
|
4
|
+
cuid2,
|
|
5
|
+
e164,
|
|
6
|
+
email,
|
|
7
|
+
emoji,
|
|
8
|
+
ipv4,
|
|
9
|
+
ipv6,
|
|
10
|
+
jwt,
|
|
11
|
+
maxLength,
|
|
12
|
+
minLength,
|
|
13
|
+
nanoid,
|
|
14
|
+
regex,
|
|
15
|
+
url,
|
|
16
|
+
uuid,
|
|
17
|
+
string as zodString,
|
|
18
|
+
} from 'zod/mini'
|
|
19
|
+
|
|
20
|
+
import { BaseType } from './base.ts'
|
|
21
|
+
|
|
22
|
+
type Check = core.CheckFn<string> | core.$ZodCheck<string>
|
|
23
|
+
|
|
24
|
+
export class StringType extends BaseType<
|
|
25
|
+
ZodMiniString<string>,
|
|
26
|
+
ZodMiniString<string>
|
|
27
|
+
> {
|
|
28
|
+
static factory(...checks: Check[]) {
|
|
29
|
+
return new StringType({
|
|
30
|
+
encodeZodType: zodString().check(...checks),
|
|
31
|
+
params: { checks },
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
max(value: number) {
|
|
36
|
+
return StringType.factory(...this.params.checks, maxLength(value))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
min(value: number) {
|
|
40
|
+
return StringType.factory(...this.params.checks, minLength(value))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
pattern(pattern: string | RegExp) {
|
|
44
|
+
return StringType.factory(
|
|
45
|
+
...this.params.checks,
|
|
46
|
+
regex(typeof pattern === 'string' ? new RegExp(pattern) : pattern),
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
email(options?: core.$ZodEmailParams) {
|
|
51
|
+
return StringType.factory(...this.params.checks, email(options))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
url(options?: core.$ZodURLParams) {
|
|
55
|
+
return StringType.factory(...this.params.checks, url(options))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
ipv4(options?: core.$ZodIPv4Params) {
|
|
59
|
+
return StringType.factory(...this.params.checks, ipv4(options))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
ipv6(options?: core.$ZodIPv6Params) {
|
|
63
|
+
return StringType.factory(...this.params.checks, ipv6(options))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
uuid(options?: core.$ZodUUIDParams) {
|
|
67
|
+
return StringType.factory(...this.params.checks, uuid(options))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
emoji(options?: core.$ZodEmojiParams) {
|
|
71
|
+
return StringType.factory(...this.params.checks, emoji(options))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
nanoid(options?: core.$ZodNanoIDParams) {
|
|
75
|
+
return StringType.factory(...this.params.checks, nanoid(options))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
cuid(options?: core.$ZodCUIDParams) {
|
|
79
|
+
return StringType.factory(...this.params.checks, cuid(options))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
cuid2(options?: core.$ZodCUID2Params) {
|
|
83
|
+
return StringType.factory(...this.params.checks, cuid2(options))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
e164(options?: core.$ZodE164Params) {
|
|
87
|
+
return StringType.factory(...this.params.checks, e164(options))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
jwt(options?: core.$ZodJWTParams) {
|
|
91
|
+
return StringType.factory(...this.params.checks, jwt(options))
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export const string = StringType.factory
|