@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.
Files changed (76) hide show
  1. package/dist/index.js +34 -31
  2. package/dist/index.js.map +1 -1
  3. package/dist/temporal.js +0 -1
  4. package/dist/temporal.js.map +1 -1
  5. package/dist/types/any.js +4 -4
  6. package/dist/types/any.js.map +1 -1
  7. package/dist/types/array.js +21 -23
  8. package/dist/types/array.js.map +1 -1
  9. package/dist/types/base.js +65 -66
  10. package/dist/types/base.js.map +1 -1
  11. package/dist/types/boolean.js +4 -4
  12. package/dist/types/boolean.js.map +1 -1
  13. package/dist/types/custom.js +9 -8
  14. package/dist/types/custom.js.map +1 -1
  15. package/dist/types/date.js +8 -13
  16. package/dist/types/date.js.map +1 -1
  17. package/dist/types/enum.js +7 -16
  18. package/dist/types/enum.js.map +1 -1
  19. package/dist/types/literal.js +7 -6
  20. package/dist/types/literal.js.map +1 -1
  21. package/dist/types/never.js +4 -4
  22. package/dist/types/never.js.map +1 -1
  23. package/dist/types/number.js +39 -92
  24. package/dist/types/number.js.map +1 -1
  25. package/dist/types/object.js +42 -31
  26. package/dist/types/object.js.map +1 -1
  27. package/dist/types/string.js +49 -47
  28. package/dist/types/string.js.map +1 -1
  29. package/dist/types/temporal.js +75 -41
  30. package/dist/types/temporal.js.map +1 -1
  31. package/dist/types/union.js +26 -18
  32. package/dist/types/union.js.map +1 -1
  33. package/package.json +7 -19
  34. package/src/index.ts +22 -24
  35. package/src/temporal.ts +1 -1
  36. package/src/types/any.ts +5 -3
  37. package/src/types/array.ts +37 -22
  38. package/src/types/base.ts +149 -81
  39. package/src/types/boolean.ts +5 -3
  40. package/src/types/custom.ts +59 -26
  41. package/src/types/date.ts +13 -17
  42. package/src/types/enum.ts +12 -22
  43. package/src/types/literal.ts +9 -6
  44. package/src/types/never.ts +5 -3
  45. package/src/types/number.ts +43 -90
  46. package/src/types/object.ts +45 -37
  47. package/src/types/string.ts +68 -37
  48. package/src/types/temporal.ts +69 -53
  49. package/src/types/union.ts +54 -50
  50. package/dist/compiler.js +0 -55
  51. package/dist/compiler.js.map +0 -1
  52. package/dist/formats.js +0 -127
  53. package/dist/formats.js.map +0 -1
  54. package/dist/inference.js +0 -1
  55. package/dist/inference.js.map +0 -1
  56. package/dist/parse.js +0 -145
  57. package/dist/parse.js.map +0 -1
  58. package/dist/runtime.js +0 -73
  59. package/dist/runtime.js.map +0 -1
  60. package/dist/schemas/default.js +0 -6
  61. package/dist/schemas/default.js.map +0 -1
  62. package/dist/schemas/discriminated-union.js +0 -9
  63. package/dist/schemas/discriminated-union.js.map +0 -1
  64. package/dist/schemas/nullable.js +0 -11
  65. package/dist/schemas/nullable.js.map +0 -1
  66. package/dist/utils.js +0 -1
  67. package/dist/utils.js.map +0 -1
  68. package/src/compiler.ts +0 -100
  69. package/src/formats.ts +0 -182
  70. package/src/inference.ts +0 -128
  71. package/src/parse.ts +0 -217
  72. package/src/runtime.ts +0 -137
  73. package/src/schemas/default.ts +0 -12
  74. package/src/schemas/discriminated-union.ts +0 -49
  75. package/src/schemas/nullable.ts +0 -20
  76. package/src/utils.ts +0 -24
package/src/types/date.ts CHANGED
@@ -1,22 +1,18 @@
1
- import { type TString, Type } from '@sinclair/typebox'
1
+ import { iso, union, type ZodMiniUnion } from '@zod/mini'
2
2
  import { CustomType, TransformType } from './custom.ts'
3
3
 
4
- const decode = (value: any): Date => new Date(value)
5
- const encode = (value: Date): any => value.toISOString()
6
-
7
- export class DateType extends TransformType<Date> {
4
+ export class DateType extends TransformType<
5
+ Date,
6
+ ZodMiniUnion<[iso.ZodMiniISODate, iso.ZodMiniISODateTime]>
7
+ > {
8
8
  static factory() {
9
- return CustomType.factory<Date, TString>(
10
- decode,
11
- encode,
12
- Type.Union([
13
- Type.String({
14
- format: 'date',
15
- }),
16
- Type.String({
17
- format: 'date-time',
18
- }),
19
- ]) as unknown as TString,
20
- )
9
+ return CustomType.factory<
10
+ Date,
11
+ ZodMiniUnion<Array<iso.ZodMiniISODate | iso.ZodMiniISODateTime>>
12
+ >({
13
+ decode: (value: string): Date => new Date(value),
14
+ encode: (value: Date): string => value.toISOString(),
15
+ type: union([iso.datetime(), iso.date()]),
16
+ })
21
17
  }
22
18
  }
package/src/types/enum.ts CHANGED
@@ -1,27 +1,17 @@
1
- import { type TEnum, Type } from '@sinclair/typebox'
1
+ import { type core, enum as enum_, type ZodMiniEnum } from '@zod/mini'
2
2
  import { BaseType } from './base.ts'
3
3
 
4
- export class ObjectEnumType<
5
- T extends { [K in string]: K } = { [K in string]: K },
6
- > extends BaseType<TEnum<T>, { values: T[keyof T] }, T[keyof T]> {
7
- static factory<T extends { [K in string]: K }>(values: T) {
8
- return new ObjectEnumType<T>(Type.Enum(values as any), {
9
- values: Object.values(values) as unknown as T[keyof T],
10
- })
11
- }
12
- }
13
-
14
4
  export class EnumType<
15
- T extends (string | number)[] = (string | number)[],
16
- > extends BaseType<
17
- TEnum<Record<string, T[number]>>,
18
- { values: [...T] },
19
- T[keyof T]
20
- > {
21
- static factory<T extends (string | number)[]>(values: [...T]) {
22
- return new EnumType<T>(
23
- Type.Enum(Object.fromEntries(values.map((v) => [v, v])) as any),
24
- { values },
25
- )
5
+ T extends core.utils.EnumLike = core.utils.EnumLike,
6
+ > extends BaseType<ZodMiniEnum<T>, ZodMiniEnum<T>, { values: T }> {
7
+ static factory<T extends core.utils.EnumLike>(values: T): EnumType<T>
8
+ static factory<T extends string[]>(
9
+ values: T,
10
+ ): EnumType<core.utils.ToEnum<T[number]>>
11
+ static factory<T extends core.utils.EnumLike | string[]>(values: T) {
12
+ return new EnumType({
13
+ encodedZodType: enum_(values as any),
14
+ props: { values },
15
+ })
26
16
  }
27
17
  }
@@ -1,10 +1,13 @@
1
- import { type TLiteral, type TLiteralValue, Type } from '@sinclair/typebox'
2
- import { BaseType } from './base.ts'
1
+ import { literal, type ZodMiniLiteral } from '@zod/mini'
2
+ import { BaseType, type PrimitiveValueType } from './base.ts'
3
3
 
4
4
  export class LiteralType<
5
- T extends TLiteralValue = TLiteralValue,
6
- > extends BaseType<TLiteral<T>, { value: TLiteralValue }, T> {
7
- static factory<T extends TLiteralValue>(value: T) {
8
- return new LiteralType<T>(Type.Literal(value), { value })
5
+ T extends PrimitiveValueType = PrimitiveValueType,
6
+ > extends BaseType<ZodMiniLiteral<T>, ZodMiniLiteral<T>, { value: T }> {
7
+ static factory<T extends PrimitiveValueType>(value: T) {
8
+ return new LiteralType<T>({
9
+ encodedZodType: literal(value),
10
+ props: { value },
11
+ })
9
12
  }
10
13
  }
@@ -1,8 +1,10 @@
1
- import { type TNever, Type } from '@sinclair/typebox'
1
+ import { never, type ZodMiniNever } from '@zod/mini'
2
2
  import { BaseType } from './base.ts'
3
3
 
4
- export class NeverType extends BaseType<TNever, {}, never> {
4
+ export class NeverType extends BaseType<ZodMiniNever> {
5
5
  static factory() {
6
- return new NeverType(Type.Never())
6
+ return new NeverType({
7
+ encodedZodType: never(),
8
+ })
7
9
  }
8
10
  }
@@ -1,117 +1,70 @@
1
1
  import {
2
- type BigIntOptions,
3
- type IntegerOptions,
4
- type NumberOptions,
5
- type TBigInt,
6
- type TInteger,
7
- type TNumber,
8
- Type,
9
- } from '@sinclair/typebox'
2
+ type core,
3
+ gt,
4
+ gte,
5
+ int,
6
+ lt,
7
+ lte,
8
+ number,
9
+ regex,
10
+ string,
11
+ type ZodMiniNumber,
12
+ type ZodMiniString,
13
+ } from '@zod/mini'
10
14
  import { BaseType } from './base.ts'
15
+ import { CustomType, TransformType } from './custom.ts'
16
+
17
+ type Check = core.CheckFn<number> | core.$ZodCheck<number>
11
18
 
12
19
  export class NumberType extends BaseType<
13
- TNumber,
14
- { options: NumberOptions },
15
- number
20
+ ZodMiniNumber<number>,
21
+ ZodMiniNumber<number>
16
22
  > {
17
- static factory(options: NumberOptions = {}) {
18
- return new NumberType(Type.Number(options), { options })
19
- }
20
-
21
- positive() {
22
- return this.min(0, true)
23
- }
24
-
25
- negative() {
26
- return this.max(0, true)
27
- }
28
-
29
- max(value: number, exclusive?: true) {
30
- return NumberType.factory({
31
- ...this.props.options,
32
- maximum: value,
33
- ...(!exclusive ? {} : { exclusiveMaximum: value }),
23
+ static factory(...checks: Check[]) {
24
+ return new NumberType({
25
+ encodedZodType: number().check(...checks),
26
+ params: { checks },
34
27
  })
35
28
  }
36
29
 
37
- min(value: number, exclusive?: true) {
38
- return NumberType.factory({
39
- ...this.props.options,
40
- minimum: value,
41
- ...(!exclusive ? {} : { exclusiveMinimum: value }),
42
- })
43
- }
44
- }
45
-
46
- export class IntegerType extends BaseType<
47
- TInteger,
48
- { options: IntegerOptions },
49
- number
50
- > {
51
- static factory(options: IntegerOptions = {}) {
52
- return new IntegerType(Type.Integer(options), { options })
53
- }
54
-
55
30
  positive() {
56
- return this.min(0, true)
31
+ return NumberType.factory(...this.params.checks, gte(0))
57
32
  }
58
33
 
59
34
  negative() {
60
- return this.max(0, true)
35
+ return NumberType.factory(...this.params.checks, lte(0))
61
36
  }
62
37
 
63
- max(value: number, exclusive?: true) {
64
- return IntegerType.factory({
65
- ...this.props.options,
66
- maximum: value,
67
- ...(!exclusive ? {} : { exclusiveMaximum: value }),
68
- })
38
+ lt(value: number) {
39
+ return NumberType.factory(...this.params.checks, lt(value))
69
40
  }
70
41
 
71
- min(value: number, exclusive?: true) {
72
- return IntegerType.factory({
73
- ...this.props.options,
74
- minimum: value,
75
- ...(!exclusive ? {} : { exclusiveMinimum: value }),
76
- })
42
+ lte(value: number) {
43
+ return NumberType.factory(...this.params.checks, lte(value))
77
44
  }
78
- }
79
45
 
80
- // TODO: this is not json schema compatible
81
- export class BigIntType extends BaseType<
82
- TBigInt,
83
- { options: BigIntOptions },
84
- bigint
85
- > {
86
- static factory(options: BigIntOptions = {}) {
87
- return new BigIntType(
88
- Type.BigInt(options),
89
- { options },
90
- { encode: (value) => `${value}` },
91
- )
46
+ gte(value: number) {
47
+ return NumberType.factory(...this.params.checks, gte(value))
92
48
  }
93
49
 
94
- positive() {
95
- return this.min(0n, true)
96
- }
97
-
98
- negative() {
99
- return this.max(0n, true)
50
+ gt(value: number) {
51
+ return NumberType.factory(...this.params.checks, gt(value))
100
52
  }
53
+ }
101
54
 
102
- max(value: bigint, exclusive?: true) {
103
- return BigIntType.factory({
104
- ...this.props.options,
105
- maximum: value,
106
- ...(!exclusive ? {} : { exclusiveMaximum: value }),
107
- })
55
+ export class IntegerType extends NumberType {
56
+ static factory(...checks: Check[]) {
57
+ return NumberType.factory(...checks, int())
108
58
  }
59
+ }
109
60
 
110
- min(value: bigint, exclusive?: true) {
111
- return BigIntType.factory({
112
- ...this.props.options,
113
- minimum: value,
114
- ...(!exclusive ? {} : { exclusiveMinimum: value }),
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',
115
68
  })
116
69
  }
117
70
  }
@@ -1,64 +1,74 @@
1
1
  import {
2
- type ObjectOptions,
3
- type TObject,
4
- type TRecordOrObject,
5
- type TSchema,
6
- Type,
7
- } from '@sinclair/typebox'
8
- import type { StaticInputDecode } from '../inference.ts'
9
- import type { UnionToTupleString } from '../utils.ts'
2
+ type core,
3
+ object,
4
+ record,
5
+ strictObject,
6
+ type ZodMiniObject,
7
+ type ZodMiniRecord,
8
+ } from '@zod/mini'
9
+
10
10
  import { BaseType, type BaseTypeAny, type OptionalType } from './base.ts'
11
- import { EnumType, type ObjectEnumType } from './enum.ts'
11
+ import { EnumType } from './enum.ts'
12
12
  import type { LiteralType } from './literal.ts'
13
13
  import type { StringType } from './string.ts'
14
14
 
15
15
  export type ObjectTypeProps = { [k: string]: BaseTypeAny }
16
16
  export type AnyObjectType = ObjectType<ObjectTypeProps>
17
17
  export class ObjectType<T extends ObjectTypeProps = {}> extends BaseType<
18
- TObject<{ [K in keyof T]: T[K]['schema'] }>,
19
- { properties: T },
20
- StaticInputDecode<TObject<{ [K in keyof T]: T[K]['schema'] }>>
18
+ ZodMiniObject<{ [K in keyof T]: T[K]['encodedZodType'] }, {}>,
19
+ ZodMiniObject<{ [K in keyof T]: T[K]['decodedZodType'] }, {}>,
20
+ { properties: T }
21
21
  > {
22
- static factory<T extends ObjectTypeProps = {}>(
23
- properties: T,
24
- options: ObjectOptions = {},
25
- ) {
26
- const schemaProperties = {} as {
27
- [K in keyof T]: T[K]['schema']
22
+ static factory<T extends ObjectTypeProps = {}>(properties: T) {
23
+ const encodeProperties = {} as {
24
+ [K in keyof T]: T[K]['encodedZodType']
25
+ }
26
+ const decodeProperties = {} as {
27
+ [K in keyof T]: T[K]['decodedZodType']
28
28
  }
29
29
 
30
30
  for (const key in properties) {
31
- schemaProperties[key] = properties[key].schema
31
+ encodeProperties[key] = properties[key].encodedZodType
32
+ decodeProperties[key] = properties[key].decodedZodType
32
33
  }
33
34
 
34
- return new ObjectType<T>(Type.Object(schemaProperties, options) as any, {
35
- properties,
35
+ return new ObjectType<T>({
36
+ encodedZodType: object(encodeProperties),
37
+ decodedZodType: object(decodeProperties),
38
+ props: { properties },
36
39
  })
37
40
  }
38
41
  }
39
42
 
40
43
  export class RecordType<
41
- K extends LiteralType | EnumType | ObjectEnumType | StringType,
44
+ K extends LiteralType<string | number> | EnumType | StringType,
42
45
  E extends BaseType,
43
- > extends BaseType<TRecordOrObject<K['schema'], E['schema']>> {
46
+ > extends BaseType<
47
+ ZodMiniRecord<K['encodedZodType'], E['encodedZodType']>,
48
+ ZodMiniRecord<K['decodedZodType'], E['decodedZodType']>
49
+ > {
44
50
  static factory<
45
- K extends
46
- | LiteralType<any>
47
- | EnumType<any>
48
- | ObjectEnumType<any>
49
- | StringType,
51
+ K extends LiteralType<string | number> | EnumType | StringType,
50
52
  E extends BaseType,
51
- >(key: K, element: E, options: ObjectOptions = {}) {
52
- return new RecordType<K, E>(
53
- Type.Record(key.schema, element.schema, options) as any,
54
- )
53
+ >(key: K, element: E) {
54
+ return new RecordType<K, E>({
55
+ encodedZodType: record(
56
+ (key as any).encodedZodType,
57
+ element.encodedZodType,
58
+ ),
59
+ decodedZodType: record(
60
+ (key as any).decodedZodType,
61
+ element.decodedZodType,
62
+ ),
63
+ props: { key, element },
64
+ })
55
65
  }
56
66
  }
57
67
 
58
68
  export function keyof<T extends ObjectType>(
59
69
  type: T,
60
70
  ): EnumType<
61
- UnionToTupleString<T extends ObjectType<infer Props> ? keyof Props : never>
71
+ core.utils.ToEnum<Extract<keyof T['props']['properties'], string>>
62
72
  > {
63
73
  return EnumType.factory(Object.keys(type.props.properties) as any)
64
74
  }
@@ -139,9 +149,7 @@ export function partial<
139
149
  >(
140
150
  object: T,
141
151
  ): ObjectType<{
142
- [K in keyof P]: P[K] extends BaseType<any, any, infer T>
143
- ? OptionalType<P[K], T>
144
- : never
152
+ [K in keyof P]: OptionalType<P[K]>
145
153
  }> {
146
154
  const properties = {} as any
147
155
 
@@ -149,5 +157,5 @@ export function partial<
149
157
  properties[key] = value.optional()
150
158
  }
151
159
 
152
- return ObjectType.factory(properties, {}) as any
160
+ return ObjectType.factory(properties)
153
161
  }
@@ -1,63 +1,94 @@
1
- import { type StringOptions, type TString, Type } from '@sinclair/typebox'
2
- import type { StaticOutputDecode } from '../inference.ts'
1
+ import {
2
+ type core,
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
+ string,
16
+ url,
17
+ uuid,
18
+ type ZodMiniString,
19
+ } from '@zod/mini'
20
+
3
21
  import { BaseType } from './base.ts'
4
22
 
23
+ type Check = core.CheckFn<string> | core.$ZodCheck<string>
24
+
5
25
  export class StringType extends BaseType<
6
- TString,
7
- { options: StringOptions },
8
- string
26
+ ZodMiniString<string>,
27
+ ZodMiniString<string>
9
28
  > {
10
- static factory(options: StringOptions = {}) {
11
- return new StringType(Type.String(options), { options })
29
+ static factory(...checks: Check[]) {
30
+ return new StringType({
31
+ encodedZodType: string().check(...checks),
32
+ params: { checks },
33
+ })
12
34
  }
13
35
 
14
36
  max(value: number) {
15
- return StringType.factory({
16
- ...this.props.options,
17
- maxLength: value,
18
- })
37
+ return StringType.factory(...this.params.checks, maxLength(value))
19
38
  }
20
39
 
21
40
  min(value: number) {
22
- return StringType.factory({
23
- ...this.props.options,
24
- minLength: value,
25
- })
41
+ return StringType.factory(...this.params.checks, minLength(value))
26
42
  }
27
43
 
28
- format(format: TString['format']) {
29
- return StringType.factory({
30
- ...this.props.options,
31
- pattern: undefined,
32
- format,
33
- })
44
+ pattern(pattern: string | RegExp) {
45
+ return StringType.factory(
46
+ ...this.params.checks,
47
+ regex(typeof pattern === 'string' ? new RegExp(pattern) : pattern),
48
+ )
34
49
  }
35
50
 
36
- pattern(pattern: string) {
37
- return StringType.factory({
38
- ...this.props.options,
39
- format: undefined,
40
- pattern,
41
- })
51
+ email(options?: core.$ZodEmailParams) {
52
+ return StringType.factory(...this.params.checks, email(options))
53
+ }
54
+
55
+ url(options?: core.$ZodURLParams) {
56
+ return StringType.factory(...this.params.checks, url(options))
57
+ }
58
+
59
+ ipv4(options?: core.$ZodIPv4Params) {
60
+ return StringType.factory(...this.params.checks, ipv4(options))
61
+ }
62
+
63
+ ipv6(options?: core.$ZodIPv6Params) {
64
+ return StringType.factory(...this.params.checks, ipv6(options))
65
+ }
66
+
67
+ uuid(options?: core.$ZodUUIDParams) {
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))
42
73
  }
43
74
 
44
- email() {
45
- return this.format('email')
75
+ nanoid(options?: core.$ZodNanoIDParams) {
76
+ return StringType.factory(...this.params.checks, nanoid(options))
46
77
  }
47
78
 
48
- url() {
49
- return this.format('uri')
79
+ cuid(options?: core.$ZodCUIDParams) {
80
+ return StringType.factory(...this.params.checks, cuid(options))
50
81
  }
51
82
 
52
- ipv4() {
53
- return this.format('ipv4')
83
+ cuid2(options?: core.$ZodCUID2Params) {
84
+ return StringType.factory(...this.params.checks, cuid2(options))
54
85
  }
55
86
 
56
- ipv6() {
57
- return this.format('ipv6')
87
+ e164(options?: core.$ZodE164Params) {
88
+ return StringType.factory(...this.params.checks, e164(options))
58
89
  }
59
90
 
60
- uuid() {
61
- return this.format('uuid')
91
+ jwt(options?: core.$ZodJWTParams) {
92
+ return StringType.factory(...this.params.checks, jwt(options))
62
93
  }
63
94
  }