@nmtjs/type 0.7.0 → 0.7.2

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/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,18 +51,18 @@ export {
52
51
 
53
52
  export namespace type {
54
53
  export namespace infer {
55
- export type decoded<T extends BaseTypeAny<any>> =
56
- T['decodedZodType']['_zod']['output']
57
-
58
- export type encoded<T extends BaseTypeAny<any>> =
59
- T['encodedZodType']['_zod']['output']
60
-
61
- export namespace input {
62
- export type decoded<T extends BaseTypeAny<any>> =
54
+ export namespace decoded {
55
+ export type input<T extends BaseTypeAny> =
63
56
  T['decodedZodType']['_zod']['input']
57
+ export type output<T extends BaseTypeAny> =
58
+ T['decodedZodType']['_zod']['output']
59
+ }
64
60
 
65
- export type encoded<T extends BaseTypeAny<any>> =
61
+ export namespace encoded {
62
+ export type input<T extends BaseTypeAny> =
66
63
  T['encodedZodType']['_zod']['input']
64
+ export type output<T extends BaseTypeAny> =
65
+ T['encodedZodType']['_zod']['output']
67
66
  }
68
67
  }
69
68
 
@@ -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
- // export const bitint = BigIntType.factory
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,
@@ -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; checks: Check[] }
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
- props: { element, checks },
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>(this.props.element, ...this.props.checks, check)
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>(this.props.element, ...this.props.checks, check)
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>(this.props.element, ...this.props.checks, check)
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
@@ -7,6 +7,7 @@ import {
7
7
  type ZodMiniAny,
8
8
  type ZodMiniArray,
9
9
  type ZodMiniBoolean,
10
+ type ZodMiniCustom,
10
11
  type ZodMiniDefault,
11
12
  type ZodMiniEnum,
12
13
  type ZodMiniInterface,
@@ -26,7 +27,7 @@ import {
26
27
 
27
28
  export type PrimitiveValueType = string | number | boolean | null
28
29
 
29
- export type SimpleZodType =
30
+ export type PrimitiveZodType =
30
31
  | ZodMiniNever
31
32
  | ZodMiniDefault
32
33
  | ZodMiniNullable
@@ -43,7 +44,8 @@ export type SimpleZodType =
43
44
  | ZodMiniIntersection
44
45
  | ZodMiniInterface
45
46
  | ZodMiniRecord
46
- | ZodMiniPipe
47
+
48
+ export type SimpleZodType = ZodMiniType
47
49
 
48
50
  export type ZodType = SimpleZodType | ZodMiniType
49
51
 
@@ -59,6 +61,7 @@ export type TypeMetadata<T = any> = {
59
61
  export type TypeParams = {
60
62
  encode?: (value: any) => any
61
63
  metadata?: TypeMetadata
64
+ checks: Array<core.CheckFn<any> | core.$ZodCheck<any>>
62
65
  }
63
66
 
64
67
  export type DefaultTypeParams = {
@@ -90,18 +93,18 @@ export abstract class BaseType<
90
93
  encodedZodType,
91
94
  decodedZodType = encodedZodType as unknown as DecodedZodType,
92
95
  props = {} as Props,
93
- params = {} as TypeParams,
96
+ params = {} as Partial<TypeParams>,
94
97
  }: {
95
98
  encodedZodType: EncodedZodType
96
99
  decodedZodType?: DecodedZodType
97
100
  props?: Props
98
- params?: TypeParams
101
+ params?: Partial<TypeParams>
99
102
  }) {
100
103
  this.encodedZodType = encodedZodType
101
104
  this.decodedZodType = decodedZodType
102
105
 
103
106
  this.props = props
104
- this.params = params
107
+ this.params = Object.assign({ checks: [] }, params)
105
108
  }
106
109
 
107
110
  optional(): OptionalType<this> {
@@ -1,51 +1,62 @@
1
1
  import {
2
2
  any,
3
+ type core,
3
4
  custom,
4
5
  overwrite,
5
6
  pipe,
6
- type ZodMiniAny,
7
- type ZodMiniCustom,
8
7
  type ZodMiniPipe,
8
+ type ZodMiniType,
9
9
  } from '@zod/mini'
10
10
  import { BaseType, type SimpleZodType, type ZodType } from './base.ts'
11
11
 
12
- export type CustomTypeDecode<I, O> = (value: I) => O
13
- export type CustomTypeEncode<I, O> = (value: I) => O
14
-
12
+ export type CustomTransformFn<I, O> = (value: I) => O
15
13
  export abstract class TransformType<
16
14
  Type,
17
- EncodedType extends SimpleZodType = ZodMiniAny,
18
- DecodedType extends ZodType = ZodMiniCustom<Type>,
19
- > extends BaseType<EncodedType, DecodedType> {}
15
+ EncodedType extends SimpleZodType = ZodMiniType<Type, Type>,
16
+ DecodedType extends ZodType = ZodMiniType<Type, Type>,
17
+ > extends BaseType<
18
+ ZodMiniPipe<
19
+ ZodMiniType<DecodedType['_zod']['output'], DecodedType['_zod']['input']>,
20
+ EncodedType
21
+ >,
22
+ ZodMiniPipe<
23
+ EncodedType,
24
+ ZodMiniType<EncodedType['_zod']['output'], EncodedType['_zod']['input']>
25
+ >
26
+ > {}
20
27
 
21
28
  export class CustomType<
22
29
  Type,
23
- EncodedType extends SimpleZodType = ZodMiniAny,
24
- DecodedType extends ZodType = ZodMiniCustom<Type, Type>,
25
- > extends BaseType<
26
- ZodMiniPipe<DecodedType, EncodedType>,
27
- ZodMiniPipe<EncodedType, DecodedType>
28
- > {
30
+ EncodedType extends SimpleZodType = ZodMiniType<Type, Type>,
31
+ DecodedType extends ZodType = ZodMiniType<Type, Type>,
32
+ > extends TransformType<Type, EncodedType, DecodedType> {
29
33
  static factory<
30
34
  Type,
31
- EncodedType extends SimpleZodType = ZodMiniAny,
32
- DecodedType extends ZodType = ZodMiniCustom<Type>,
33
- >(
34
- decode: CustomTypeDecode<
35
- EncodedType['_zod']['output'],
35
+ EncodedType extends SimpleZodType = ZodMiniType<Type, Type>,
36
+ DecodedType extends ZodType = ZodMiniType<Type, Type>,
37
+ >({
38
+ decode,
39
+ encode,
40
+ error,
41
+ type = any() as unknown as EncodedType,
42
+ }: {
43
+ decode: CustomTransformFn<
44
+ EncodedType['_zod']['input'],
36
45
  DecodedType['_zod']['output']
37
- >,
38
- encode: CustomTypeEncode<
39
- DecodedType['_zod']['output'],
46
+ >
47
+ encode: CustomTransformFn<
48
+ DecodedType['_zod']['input'],
40
49
  EncodedType['_zod']['output']
41
- >,
42
- type: EncodedType = any() as unknown as EncodedType,
43
- ) {
50
+ >
51
+ error?: string | core.$ZodErrorMap<core.$ZodIssueBase>
52
+ type?: EncodedType
53
+ }) {
44
54
  return new CustomType<Type, EncodedType, DecodedType>({
45
- //@ts-expect-error
46
55
  encodedZodType: pipe(custom().check(overwrite(encode)), type),
47
- //@ts-expect-error
48
- decodedZodType: pipe(type, custom().check(overwrite(decode))),
56
+ decodedZodType: pipe(
57
+ type,
58
+ custom(undefined, { error }).check(overwrite(decode)),
59
+ ),
49
60
  params: { encode },
50
61
  })
51
62
  }
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
- >(decode, encode, union([iso.datetime(), iso.date()]))
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
  }
@@ -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.props.checks, gte(0))
31
+ return NumberType.factory(...this.params.checks, gte(0))
28
32
  }
29
33
 
30
34
  negative() {
31
- return NumberType.factory(...this.props.checks, lte(0))
35
+ return NumberType.factory(...this.params.checks, lte(0))
32
36
  }
33
37
 
34
38
  lt(value: number) {
35
- return NumberType.factory(...this.props.checks, lt(value))
39
+ return NumberType.factory(...this.params.checks, lt(value))
36
40
  }
37
41
 
38
42
  lte(value: number) {
39
- return NumberType.factory(...this.props.checks, lte(value))
43
+ return NumberType.factory(...this.params.checks, lte(value))
40
44
  }
41
45
 
42
46
  gte(value: number) {
43
- return NumberType.factory(...this.props.checks, gte(value))
47
+ return NumberType.factory(...this.params.checks, gte(value))
44
48
  }
45
49
 
46
50
  gt(value: number) {
47
- return NumberType.factory(...this.props.checks, gt(value))
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
+ }
@@ -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) {
@@ -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
- props: { checks },
32
+ params: { checks },
28
33
  })
29
34
  }
30
35
 
31
36
  max(value: number) {
32
- return StringType.factory(...this.props.checks, maxLength(value))
37
+ return StringType.factory(...this.params.checks, maxLength(value))
33
38
  }
34
39
 
35
40
  min(value: number) {
36
- return StringType.factory(...this.props.checks, minLength(value))
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.props.checks,
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.props.checks, email(options))
52
+ return StringType.factory(...this.params.checks, email(options))
48
53
  }
49
54
 
50
55
  url(options?: core.$ZodURLParams) {
51
- return StringType.factory(...this.props.checks, url(options))
56
+ return StringType.factory(...this.params.checks, url(options))
52
57
  }
53
58
 
54
59
  ipv4(options?: core.$ZodIPv4Params) {
55
- return StringType.factory(...this.props.checks, ipv4(options))
60
+ return StringType.factory(...this.params.checks, ipv4(options))
56
61
  }
57
62
 
58
63
  ipv6(options?: core.$ZodIPv6Params) {
59
- return StringType.factory(...this.props.checks, ipv6(options))
64
+ return StringType.factory(...this.params.checks, ipv6(options))
60
65
  }
61
66
 
62
67
  uuid(options?: core.$ZodUUIDParams) {
63
- return StringType.factory(...this.props.checks, uuid(options))
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
  }