@nmtjs/type 0.6.5 → 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 +34 -31
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +0 -1
- 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 +21 -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 +9 -8
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +8 -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 +39 -92
- 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 +49 -47
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +75 -41
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +26 -18
- package/dist/types/union.js.map +1 -1
- package/package.json +7 -19
- package/src/index.ts +22 -24
- package/src/temporal.ts +1 -1
- package/src/types/any.ts +5 -3
- package/src/types/array.ts +37 -22
- package/src/types/base.ts +149 -81
- package/src/types/boolean.ts +5 -3
- package/src/types/custom.ts +59 -26
- package/src/types/date.ts +13 -17
- 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 +43 -90
- package/src/types/object.ts +45 -37
- package/src/types/string.ts +68 -37
- package/src/types/temporal.ts +69 -53
- package/src/types/union.ts +54 -50
- 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/dist/utils.js +0 -1
- package/dist/utils.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/src/utils.ts +0 -24
package/dist/types/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAEE,oBACA,cACA,aAIK,WAAW;AAClB,SAAS,gBAAkC,WAAW;AAItD,OAAO,MAAM,kBAEH,SAIR;CACA,OAAO,QACL,GAAG,SACH;AACA,SAAO,IAAI,UAAa;GACtB,gBAAgB,MAAM,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CAAC;GAC3D,gBAAgB,MAAM,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CAAC;GAC3D,OAAO,EAAE,QAAS;EACnB;CACF;AACF;AAED,OAAO,MAAM,yBAEH,SAIR;CACA,OAAO,QAEL,GAAG,SAAY;EACf,MAAM,CAAC,OAAO,OAAO,GAAG;AACxB,SAAO,IAAI,iBAAoB;GAC7B,gBAAgB,aAAa,MAAM,gBAAgB,OAAO,eAAe;GACzE,gBAAgB,aAAa,MAAM,gBAAgB,OAAO,eAAe;GACzE,OAAO,EAAE,QAAS;EACnB;CACF;AACF;AAYD,OAAO,MAAM,+BAIH,SAOR;CACA,OAAO,QAILA,KAAQ,GAAG,SAAY;AACvB,SAAO,IAAI,uBAA6B;GACtC,gBAAgB,mBACd,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CACrC;GACD,gBAAgB,mBACd,QAAQ,IAAI,CAAC,MAAM,EAAE,eAAe,CACrC;GACD,OAAO;IAAE;IAAK;GAAS;EACxB;CACF;AACF","names":["key: K"],"sources":["src/types/union.ts"],"sourcesContent":["import {\n type core,\n discriminatedUnion,\n intersection,\n union,\n type ZodMiniDiscriminatedUnion,\n type ZodMiniIntersection,\n type ZodMiniUnion,\n} from '@zod/mini'\nimport { BaseType, type BaseTypeAny } from './base.ts'\nimport type { LiteralType } from './literal.ts'\nimport type { ObjectType, ObjectTypeProps } from './object.ts'\n\nexport class UnionType<\n T extends readonly BaseType[] = readonly BaseType[],\n> extends BaseType<\n ZodMiniUnion<core.utils.Flatten<T[number]['encodedZodType'][]>>,\n ZodMiniUnion<core.utils.Flatten<T[number]['decodedZodType'][]>>,\n { options: T }\n> {\n static factory<T extends readonly BaseType[] = readonly BaseType[]>(\n ...options: T\n ) {\n return new UnionType<T>({\n encodedZodType: union(options.map((t) => t.encodedZodType)),\n decodedZodType: union(options.map((t) => t.decodedZodType)),\n props: { options },\n })\n }\n}\n\nexport class IntersactionType<\n T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],\n> extends BaseType<\n ZodMiniIntersection<T[0]['encodedZodType'], T[1]['encodedZodType']>,\n ZodMiniIntersection<T[0]['decodedZodType'], T[1]['decodedZodType']>,\n { options: T }\n> {\n static factory<\n T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],\n >(...options: T) {\n const [first, second] = options\n return new IntersactionType<T>({\n encodedZodType: intersection(first.encodedZodType, second.encodedZodType),\n decodedZodType: intersection(first.decodedZodType, second.decodedZodType),\n props: { options },\n })\n }\n}\n\nexport type DiscriminatedUnionProperties<K extends string = string> = {\n [OK in K]: LiteralType<string>\n} & {\n [OK in string]: any\n}\n\nexport type DiscriminatedUnionOptionType<K extends string> = ObjectType<\n ObjectTypeProps & { [_ in K]: BaseTypeAny }\n>\n\nexport class DiscriminatedUnionType<\n K extends string = string,\n T extends\n readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],\n> extends BaseType<\n ZodMiniDiscriminatedUnion<core.utils.Flatten<T[number]['encodedZodType'][]>>,\n ZodMiniDiscriminatedUnion<core.utils.Flatten<T[number]['decodedZodType'][]>>,\n {\n key: K\n options: T\n }\n> {\n static factory<\n K extends string = string,\n T extends\n readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],\n >(key: K, ...options: T) {\n return new DiscriminatedUnionType<K, T>({\n encodedZodType: discriminatedUnion(\n options.map((t) => t.encodedZodType) as any,\n ),\n decodedZodType: discriminatedUnion(\n options.map((t) => t.decodedZodType) as any,\n ),\n props: { key, options },\n })\n }\n}\n"],"version":3}
|
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.1"
|
|
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.1",
|
|
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,16 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
StaticInputDecode,
|
|
3
|
-
StaticInputEncode,
|
|
4
|
-
StaticOutputDecode,
|
|
5
|
-
StaticOutputEncode,
|
|
6
|
-
} from './inference.ts'
|
|
1
|
+
import * as zod from '@zod/mini'
|
|
7
2
|
import { AnyType } from './types/any.ts'
|
|
8
3
|
import { ArrayType } from './types/array.ts'
|
|
9
4
|
import type { BaseTypeAny } from './types/base.ts'
|
|
10
5
|
import { BooleanType } from './types/boolean.ts'
|
|
11
6
|
import { CustomType } from './types/custom.ts'
|
|
12
7
|
import { DateType } from './types/date.ts'
|
|
13
|
-
import { EnumType
|
|
8
|
+
import { EnumType } from './types/enum.ts'
|
|
14
9
|
import { LiteralType } from './types/literal.ts'
|
|
15
10
|
import { NeverType } from './types/never.ts'
|
|
16
11
|
import { BigIntType, IntegerType, NumberType } from './types/number.ts'
|
|
@@ -31,8 +26,9 @@ import {
|
|
|
31
26
|
UnionType,
|
|
32
27
|
} from './types/union.ts'
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
zod.config(zod.core.locales.en())
|
|
30
|
+
|
|
31
|
+
export { NeemataTypeError } from './types/base.ts'
|
|
36
32
|
export { BaseType, type BaseTypeAny } from './types/base.ts'
|
|
37
33
|
export {
|
|
38
34
|
ArrayType,
|
|
@@ -49,23 +45,24 @@ export {
|
|
|
49
45
|
UnionType,
|
|
50
46
|
AnyType,
|
|
51
47
|
NeverType,
|
|
48
|
+
DiscriminatedUnionType,
|
|
49
|
+
RecordType,
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
export namespace type {
|
|
55
53
|
export namespace infer {
|
|
56
|
-
export type decoded<T extends BaseTypeAny
|
|
57
|
-
T['
|
|
58
|
-
|
|
59
|
-
export type encoded<T extends BaseTypeAny
|
|
60
|
-
T['
|
|
61
|
-
|
|
54
|
+
export type decoded<T extends BaseTypeAny> =
|
|
55
|
+
T['decodedZodType']['_zod']['output']
|
|
56
|
+
|
|
57
|
+
export type encoded<T extends BaseTypeAny> =
|
|
58
|
+
T['encodedZodType']['_zod']['output']
|
|
59
|
+
|
|
62
60
|
export namespace input {
|
|
63
|
-
export type decoded<T extends BaseTypeAny
|
|
64
|
-
T['
|
|
65
|
-
|
|
66
|
-
export type encoded<T extends BaseTypeAny
|
|
67
|
-
T['
|
|
68
|
-
>
|
|
61
|
+
export type decoded<T extends BaseTypeAny> =
|
|
62
|
+
T['decodedZodType']['_zod']['input']
|
|
63
|
+
|
|
64
|
+
export type encoded<T extends BaseTypeAny> =
|
|
65
|
+
T['encodedZodType']['_zod']['input']
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
68
|
|
|
@@ -76,14 +73,15 @@ export namespace type {
|
|
|
76
73
|
export const integer = IntegerType.factory
|
|
77
74
|
export const bitint = BigIntType.factory
|
|
78
75
|
export const literal = LiteralType.factory
|
|
79
|
-
export const
|
|
80
|
-
export const arrayEnum = EnumType.factory
|
|
76
|
+
export const enumeration = EnumType.factory
|
|
81
77
|
export const date = DateType.factory
|
|
82
78
|
export const array = ArrayType.factory
|
|
83
79
|
export const record = RecordType.factory
|
|
84
80
|
export const any = AnyType.factory
|
|
85
81
|
export const or = UnionType.factory
|
|
86
82
|
export const and = IntersactionType.factory
|
|
83
|
+
export const union = UnionType.factory
|
|
84
|
+
export const intersaction = IntersactionType.factory
|
|
87
85
|
export const discriminatedUnion = DiscriminatedUnionType.factory
|
|
88
86
|
export const custom = CustomType.factory
|
|
89
87
|
export const object = Object.assign(ObjectType.factory.bind(ObjectType), {
|
|
@@ -96,4 +94,4 @@ export namespace type {
|
|
|
96
94
|
})
|
|
97
95
|
}
|
|
98
96
|
|
|
99
|
-
export { type as t }
|
|
97
|
+
export { type as t, zod }
|
package/src/temporal.ts
CHANGED
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,53 @@
|
|
|
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 }
|
|
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
|
+
params: { checks },
|
|
23
|
+
props: { element },
|
|
24
|
+
})
|
|
15
25
|
}
|
|
16
26
|
|
|
17
27
|
min(value: number) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
const check = minLength(value)
|
|
29
|
+
return ArrayType.factory<T>(
|
|
30
|
+
this.props.element,
|
|
31
|
+
...this.params.checks,
|
|
32
|
+
check,
|
|
33
|
+
)
|
|
22
34
|
}
|
|
23
35
|
|
|
24
36
|
max(value: number) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
const check = maxLength(value)
|
|
38
|
+
return ArrayType.factory<T>(
|
|
39
|
+
this.props.element,
|
|
40
|
+
...this.params.checks,
|
|
41
|
+
check,
|
|
42
|
+
)
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
length(value: number) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
const check = length(value)
|
|
47
|
+
return ArrayType.factory<T>(
|
|
48
|
+
this.props.element,
|
|
49
|
+
...this.params.checks,
|
|
50
|
+
check,
|
|
51
|
+
)
|
|
37
52
|
}
|
|
38
53
|
}
|
package/src/types/base.ts
CHANGED
|
@@ -1,139 +1,207 @@
|
|
|
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
|
|
62
|
+
checks: Array<core.CheckFn<any> | core.$ZodCheck<any>>
|
|
12
63
|
}
|
|
13
64
|
|
|
14
65
|
export type DefaultTypeParams = {
|
|
15
|
-
optional: false
|
|
16
|
-
nullable: false
|
|
17
|
-
hasDefault: false
|
|
18
66
|
encode?: TypeParams['encode']
|
|
67
|
+
metadata?: TypeMetadata
|
|
19
68
|
}
|
|
20
69
|
|
|
21
|
-
export type BaseTypeAny<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
70
|
+
export type BaseTypeAny<
|
|
71
|
+
EncodedZodType extends SimpleZodType = SimpleZodType,
|
|
72
|
+
DecodedZodType extends ZodType = ZodMiniType,
|
|
73
|
+
> = BaseType<EncodedZodType, DecodedZodType, TypeProps>
|
|
74
|
+
|
|
75
|
+
export const typesRegistry = registry<TypeMetadata>()
|
|
76
|
+
|
|
77
|
+
export const NeemataTypeError = core.$ZodError
|
|
78
|
+
export type NeemataTypeError = core.$ZodError
|
|
26
79
|
|
|
27
80
|
export abstract class BaseType<
|
|
28
|
-
|
|
81
|
+
EncodedZodType extends SimpleZodType = SimpleZodType,
|
|
82
|
+
DecodedZodType extends ZodType = EncodedZodType,
|
|
29
83
|
Props extends TypeProps = TypeProps,
|
|
30
|
-
ValueType = unknown,
|
|
31
84
|
> {
|
|
32
|
-
readonly
|
|
85
|
+
readonly encodedZodType: EncodedZodType
|
|
86
|
+
readonly decodedZodType: DecodedZodType
|
|
33
87
|
readonly props: Props
|
|
34
88
|
readonly params: TypeParams
|
|
35
89
|
|
|
36
|
-
constructor(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
90
|
+
constructor({
|
|
91
|
+
encodedZodType,
|
|
92
|
+
decodedZodType = encodedZodType as unknown as DecodedZodType,
|
|
93
|
+
props = {} as Props,
|
|
94
|
+
params = {} as Partial<TypeParams>,
|
|
95
|
+
}: {
|
|
96
|
+
encodedZodType: EncodedZodType
|
|
97
|
+
decodedZodType?: DecodedZodType
|
|
98
|
+
props?: Props
|
|
99
|
+
params?: Partial<TypeParams>
|
|
100
|
+
}) {
|
|
101
|
+
this.encodedZodType = encodedZodType
|
|
102
|
+
this.decodedZodType = decodedZodType
|
|
43
103
|
|
|
44
104
|
this.props = props
|
|
45
|
-
this.params = {
|
|
46
|
-
hasDefault,
|
|
47
|
-
nullable,
|
|
48
|
-
optional,
|
|
49
|
-
} as TypeParams
|
|
105
|
+
this.params = Object.assign({ checks: [] }, params)
|
|
50
106
|
}
|
|
51
107
|
|
|
52
|
-
optional(): OptionalType<
|
|
53
|
-
return OptionalType.factory(this)
|
|
108
|
+
optional(): OptionalType<this> {
|
|
109
|
+
return OptionalType.factory(this)
|
|
54
110
|
}
|
|
55
111
|
|
|
56
|
-
nullable(): NullableType<
|
|
57
|
-
return NullableType.factory(this)
|
|
112
|
+
nullable(): NullableType<this> {
|
|
113
|
+
return NullableType.factory(this)
|
|
58
114
|
}
|
|
59
115
|
|
|
60
116
|
nullish() {
|
|
61
117
|
return this.nullable().optional()
|
|
62
118
|
}
|
|
63
119
|
|
|
64
|
-
default(value:
|
|
65
|
-
return DefaultType.factory(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
120
|
+
default(value: this['encodedZodType']['_zod']['input']): DefaultType<this> {
|
|
121
|
+
return DefaultType.factory(this, value)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
title(title: string): this {
|
|
125
|
+
return this.meta({ title })
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
description(description: string): this {
|
|
129
|
+
return this.meta({ description })
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
examples(...examples: this['decodedZodType']['_zod']['input'][]): this {
|
|
133
|
+
return this.meta({
|
|
134
|
+
examples: this.params.encode
|
|
135
|
+
? examples.map(this.params.encode)
|
|
136
|
+
: examples,
|
|
137
|
+
})
|
|
69
138
|
}
|
|
70
139
|
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
description,
|
|
77
|
-
},
|
|
78
|
-
this.props,
|
|
79
|
-
this.params,
|
|
80
|
-
) as any
|
|
140
|
+
meta(newMetadata: TypeMetadata): this {
|
|
141
|
+
const metadata = typesRegistry.get(this.encodedZodType) ?? {}
|
|
142
|
+
Object.assign(metadata, newMetadata)
|
|
143
|
+
typesRegistry.add(this.encodedZodType, metadata)
|
|
144
|
+
return this
|
|
81
145
|
}
|
|
82
146
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
147
|
+
encode(
|
|
148
|
+
data: this['encodedZodType']['_zod']['input'],
|
|
149
|
+
): this['encodedZodType']['_zod']['output'] {
|
|
150
|
+
return this.encodedZodType.parse(data, { reportInput: true })
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
decode(
|
|
154
|
+
data: this['decodedZodType']['_zod']['input'],
|
|
155
|
+
): this['decodedZodType']['_zod']['output'] {
|
|
156
|
+
return this.decodedZodType.parse(data, { reportInput: true })
|
|
93
157
|
}
|
|
94
158
|
}
|
|
95
159
|
|
|
96
160
|
export class OptionalType<
|
|
97
161
|
Type extends BaseTypeAny = BaseTypeAny,
|
|
98
|
-
|
|
99
|
-
|
|
162
|
+
> extends BaseType<
|
|
163
|
+
ZodMiniOptional<Type['encodedZodType']>,
|
|
164
|
+
ZodMiniOptional<Type['decodedZodType']>,
|
|
165
|
+
{ inner: Type }
|
|
166
|
+
> {
|
|
100
167
|
static factory<T extends BaseTypeAny>(type: T) {
|
|
101
|
-
return new OptionalType<T>(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
168
|
+
return new OptionalType<T>({
|
|
169
|
+
encodedZodType: optional(type.encodedZodType) as any,
|
|
170
|
+
props: { inner: type },
|
|
171
|
+
})
|
|
105
172
|
}
|
|
106
173
|
}
|
|
107
174
|
|
|
108
175
|
export class NullableType<
|
|
109
176
|
Type extends BaseTypeAny<any> = BaseTypeAny<any>,
|
|
110
|
-
ValueType = unknown,
|
|
111
177
|
> extends BaseType<
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
178
|
+
ZodMiniNullable<Type['encodedZodType']>,
|
|
179
|
+
ZodMiniNullable<Type['decodedZodType']>,
|
|
180
|
+
{ inner: Type }
|
|
115
181
|
> {
|
|
116
182
|
static factory<T extends BaseTypeAny<any>>(type: T) {
|
|
117
|
-
return new NullableType<T>(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
183
|
+
return new NullableType<T>({
|
|
184
|
+
encodedZodType: nullable(type.encodedZodType),
|
|
185
|
+
props: { inner: type },
|
|
186
|
+
})
|
|
121
187
|
}
|
|
122
188
|
}
|
|
123
189
|
|
|
124
190
|
export class DefaultType<
|
|
125
191
|
Type extends BaseTypeAny = BaseTypeAny,
|
|
126
|
-
ValueType = unknown,
|
|
127
192
|
> extends BaseType<
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
193
|
+
ZodMiniDefault<Type['encodedZodType']>,
|
|
194
|
+
ZodMiniDefault<Type['decodedZodType']>,
|
|
195
|
+
{ inner: Type }
|
|
131
196
|
> {
|
|
132
197
|
static factory<T extends BaseTypeAny<any>>(type: T, defaultValue: any) {
|
|
133
|
-
return new DefaultType<T>(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
198
|
+
return new DefaultType<T>({
|
|
199
|
+
encodedZodType: _default(
|
|
200
|
+
type.encodedZodType,
|
|
201
|
+
type.params.encode?.(defaultValue) ?? defaultValue,
|
|
202
|
+
) as any,
|
|
203
|
+
decodedZodType: _default(type.decodedZodType, defaultValue) as any,
|
|
204
|
+
props: { inner: type },
|
|
205
|
+
})
|
|
138
206
|
}
|
|
139
207
|
}
|
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,66 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
any,
|
|
3
|
+
type core,
|
|
4
|
+
custom,
|
|
5
|
+
overwrite,
|
|
6
|
+
pipe,
|
|
7
|
+
type ZodMiniAny,
|
|
8
|
+
type ZodMiniCustom,
|
|
9
|
+
type ZodMiniPipe,
|
|
10
|
+
} from '@zod/mini'
|
|
11
|
+
import { BaseType, type SimpleZodType, type ZodType } from './base.ts'
|
|
9
12
|
|
|
10
|
-
export type CustomTypeDecode<
|
|
11
|
-
export type CustomTypeEncode<
|
|
13
|
+
export type CustomTypeDecode<I, O> = (value: I) => O
|
|
14
|
+
export type CustomTypeEncode<I, O> = (value: I) => O
|
|
12
15
|
|
|
13
16
|
export abstract class TransformType<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
Type,
|
|
18
|
+
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
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
|
+
> {}
|
|
17
30
|
|
|
18
|
-
export class CustomType<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
31
|
+
export class CustomType<
|
|
32
|
+
Type,
|
|
33
|
+
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
34
|
+
DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
|
|
35
|
+
> extends TransformType<Type, EncodedType, DecodedType> {
|
|
36
|
+
static factory<
|
|
37
|
+
Type,
|
|
38
|
+
EncodedType extends SimpleZodType = ZodMiniAny,
|
|
39
|
+
DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
|
|
40
|
+
>({
|
|
41
|
+
decode,
|
|
42
|
+
encode,
|
|
43
|
+
error,
|
|
44
|
+
type = any() as unknown as EncodedType,
|
|
45
|
+
}: {
|
|
46
|
+
decode: CustomTypeDecode<
|
|
47
|
+
EncodedType['_zod']['input'],
|
|
48
|
+
DecodedType['_zod']['output']
|
|
49
|
+
>
|
|
50
|
+
encode: CustomTypeEncode<
|
|
51
|
+
DecodedType['_zod']['input'],
|
|
52
|
+
EncodedType['_zod']['output']
|
|
53
|
+
>
|
|
54
|
+
error?: string | core.$ZodErrorMap<core.$ZodIssueBase>
|
|
55
|
+
type?: EncodedType
|
|
56
|
+
}) {
|
|
57
|
+
return new CustomType<Type, EncodedType, DecodedType>({
|
|
58
|
+
encodedZodType: pipe(custom().check(overwrite(encode)), type),
|
|
59
|
+
decodedZodType: pipe(
|
|
60
|
+
type,
|
|
61
|
+
custom(undefined, { error }).check(overwrite(decode)),
|
|
62
|
+
),
|
|
63
|
+
params: { encode },
|
|
64
|
+
})
|
|
32
65
|
}
|
|
33
66
|
}
|