@nmtjs/type 0.2.0 → 0.3.0

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 (55) hide show
  1. package/dist/compiler.js +2 -2
  2. package/dist/compiler.js.map +1 -1
  3. package/dist/constants.js +2 -0
  4. package/dist/constants.js.map +1 -0
  5. package/dist/index.js +2 -3
  6. package/dist/index.js.map +1 -1
  7. package/dist/temporal.js +3 -1
  8. package/dist/temporal.js.map +1 -1
  9. package/dist/types/any.js +37 -5
  10. package/dist/types/any.js.map +1 -1
  11. package/dist/types/array.js +55 -24
  12. package/dist/types/array.js.map +1 -1
  13. package/dist/types/base.js +31 -62
  14. package/dist/types/base.js.map +1 -1
  15. package/dist/types/boolean.js +37 -5
  16. package/dist/types/boolean.js.map +1 -1
  17. package/dist/types/custom.js +37 -8
  18. package/dist/types/custom.js.map +1 -1
  19. package/dist/types/datetime.js +41 -22
  20. package/dist/types/datetime.js.map +1 -1
  21. package/dist/types/enum.js +74 -16
  22. package/dist/types/enum.js.map +1 -1
  23. package/dist/types/literal.js +35 -8
  24. package/dist/types/literal.js.map +1 -1
  25. package/dist/types/never.js +17 -2
  26. package/dist/types/never.js.map +1 -1
  27. package/dist/types/number.js +116 -62
  28. package/dist/types/number.js.map +1 -1
  29. package/dist/types/object.js +58 -17
  30. package/dist/types/object.js.map +1 -1
  31. package/dist/types/string.js +57 -21
  32. package/dist/types/string.js.map +1 -1
  33. package/dist/types/temporal.js +292 -74
  34. package/dist/types/temporal.js.map +1 -1
  35. package/dist/types/union.js +75 -17
  36. package/dist/types/union.js.map +1 -1
  37. package/package.json +3 -3
  38. package/src/compiler.ts +2 -2
  39. package/src/constants.ts +5 -0
  40. package/src/index.ts +5 -6
  41. package/src/temporal.ts +4 -2
  42. package/src/types/any.ts +32 -9
  43. package/src/types/array.ts +59 -28
  44. package/src/types/base.ts +59 -112
  45. package/src/types/boolean.ts +31 -9
  46. package/src/types/custom.ts +61 -24
  47. package/src/types/datetime.ts +40 -35
  48. package/src/types/enum.ts +78 -21
  49. package/src/types/literal.ts +42 -12
  50. package/src/types/never.ts +24 -11
  51. package/src/types/number.ts +103 -67
  52. package/src/types/object.ts +87 -32
  53. package/src/types/string.ts +38 -30
  54. package/src/types/temporal.ts +378 -118
  55. package/src/types/union.ts +103 -31
@@ -1,53 +1,90 @@
1
- import { type TTransform, type TUnsafe, Type } from '@sinclair/typebox'
1
+ import {
2
+ type SchemaOptions,
3
+ type TTransform,
4
+ type TUnsafe,
5
+ Type,
6
+ } from '@sinclair/typebox'
2
7
  import { BaseType } from './base.ts'
3
8
 
9
+ type CustomDecode<T> = (value: any) => T
10
+ type CustomEncode<T> = (value: T) => any
11
+
4
12
  export class CustomType<
5
13
  T,
6
14
  S = T,
7
15
  N extends boolean = false,
8
16
  O extends boolean = false,
9
- > extends BaseType<TTransform<TUnsafe<S>, T>, N, O> {
17
+ D extends boolean = false,
18
+ > extends BaseType<TTransform<TUnsafe<S>, T>, N, O, D> {
10
19
  constructor(
11
- protected readonly decode: (value: any) => T,
12
- protected readonly encode: (value: T) => any,
13
- nullable: N = false as N,
14
- optional: O = false as O,
20
+ protected readonly decode: CustomDecode<T>,
21
+ protected readonly encode: CustomEncode<T>,
22
+ options: SchemaOptions = {},
23
+ isNullable: N = false as N,
24
+ isOptional: O = false as O,
25
+ hasDefault: D = false as D,
15
26
  ) {
16
- super(
17
- Type.Optional(
18
- Type.Transform(Type.Any() as unknown as TUnsafe<S>)
19
- .Decode(decode)
20
- .Encode(encode),
21
- ),
22
- nullable,
23
- optional,
24
- )
27
+ super(options, isNullable, isOptional, hasDefault, decode, encode)
28
+ }
29
+
30
+ protected _constructSchema(
31
+ options: SchemaOptions,
32
+ decode: CustomDecode<T>,
33
+ encode: CustomEncode<T>,
34
+ ): TTransform<TUnsafe<S>, T> {
35
+ return Type.Transform(Type.Any(options) as unknown as TUnsafe<S>)
36
+ .Decode(decode)
37
+ .Encode(encode)
25
38
  }
26
39
 
27
40
  nullable() {
28
- const [_, ...args] = this._nullable()
29
- return new CustomType<T, S, (typeof args)[0], (typeof args)[1]>(
41
+ return new CustomType<T, S, true, O, D>(
30
42
  this.decode,
31
43
  this.encode,
32
- ...args,
44
+ ...this._with({ isNullable: true }),
33
45
  )
34
46
  }
35
47
 
36
48
  optional() {
37
- const [_, ...args] = this._optional()
38
- return new CustomType<T, S, (typeof args)[0], (typeof args)[1]>(
49
+ return new CustomType<T, S, N, true, D>(
39
50
  this.decode,
40
51
  this.encode,
41
- ...args,
52
+ ...this._with({ isOptional: true }),
42
53
  )
43
54
  }
44
55
 
45
56
  nullish() {
46
- const [_, ...args] = this._nullish()
47
- return new CustomType<T, S, (typeof args)[0], (typeof args)[1]>(
57
+ return new CustomType<T, S, true, true, D>(
58
+ this.decode,
59
+ this.encode,
60
+ ...this._with({ isNullable: true, isOptional: true }),
61
+ )
62
+ }
63
+
64
+ default(value: T) {
65
+ return new CustomType<T, S, N, O, true>(
66
+ this.decode,
67
+ this.encode,
68
+ ...this._with({
69
+ options: { default: this.encode(value) },
70
+ hasDefault: true,
71
+ }),
72
+ )
73
+ }
74
+
75
+ description(description: string) {
76
+ return new CustomType<T, S, N, O, D>(
77
+ this.decode,
78
+ this.encode,
79
+ ...this._with({ options: { description } }),
80
+ )
81
+ }
82
+
83
+ examples(...examples: [T, ...T[]]) {
84
+ return new CustomType<T, S, N, O, D>(
48
85
  this.decode,
49
86
  this.encode,
50
- ...args,
87
+ ...this._with({ options: { examples: examples.map(this.encode) } }),
51
88
  )
52
89
  }
53
90
  }
@@ -1,60 +1,65 @@
1
- import { type TString, type TTransform, Type } from '@sinclair/typebox'
1
+ import {
2
+ type SchemaOptions,
3
+ type StringOptions,
4
+ type TString,
5
+ type TTransform,
6
+ Type,
7
+ } from '@sinclair/typebox'
2
8
  import { BaseType } from './base.ts'
3
9
 
10
+ const decode = (value: any): Date => new Date(value)
11
+ const encode = (value: Date): any => value.toISOString()
12
+
4
13
  export class DateType<
5
14
  N extends boolean = false,
6
15
  O extends boolean = false,
7
- > extends BaseType<TTransform<TString, Date>, N, O> {
16
+ D extends boolean = false,
17
+ > extends BaseType<TTransform<TString, Date>, N, O, D, StringOptions> {
8
18
  constructor(
9
- schema: TTransform<TString, Date> = Type.Transform(
10
- Type.String({ format: 'date' }),
11
- )
12
- .Decode((value) => new Date(value))
13
- .Encode((value) => value.toJSON()),
14
- nullable: N = false as N,
15
- optional: O = false as O,
19
+ options: SchemaOptions = {},
20
+ isNullable: N = false as N,
21
+ isOptional: O = false as O,
22
+ hasDefault: D = false as D,
16
23
  ) {
17
- super(schema, nullable, optional)
24
+ super(options, isNullable, isOptional, hasDefault)
25
+ }
26
+
27
+ protected _constructSchema(
28
+ options: StringOptions,
29
+ ): TTransform<TString, Date> {
30
+ return Type.Transform(Type.String({ ...options, format: 'date-time' }))
31
+ .Decode(decode)
32
+ .Encode(encode)
18
33
  }
19
34
 
20
35
  nullable() {
21
- return new DateType(...this._nullable())
36
+ return new DateType(...this._with({ isNullable: true }))
22
37
  }
23
38
 
24
39
  optional() {
25
- return new DateType(...this._optional())
40
+ return new DateType(...this._with({ isOptional: true }))
26
41
  }
27
42
 
28
43
  nullish() {
29
- return new DateType(...this._nullish())
44
+ return new DateType(...this._with({ isNullable: true, isOptional: true }))
30
45
  }
31
- }
32
46
 
33
- export class DateTimeType<
34
- N extends boolean = false,
35
- O extends boolean = false,
36
- > extends BaseType<TTransform<TString, Date>, N, O> {
37
- constructor(
38
- schema: TTransform<TString, Date> = Type.Transform(
39
- Type.String({ format: 'date-time' }),
47
+ default(value: Date) {
48
+ return new DateType(
49
+ ...this._with({
50
+ options: { default: encode(value) },
51
+ hasDefault: true,
52
+ }),
40
53
  )
41
- .Decode((value) => new Date(value))
42
- .Encode((value) => value.toJSON()),
43
- nullable: N = false as N,
44
- optional: O = false as O,
45
- ) {
46
- super(schema, nullable, optional)
47
54
  }
48
55
 
49
- nullable() {
50
- return new DateTimeType(...this._nullable())
56
+ description(value: string) {
57
+ return new DateType(...this._with({ options: { description: value } }))
51
58
  }
52
59
 
53
- optional() {
54
- return new DateTimeType(...this._optional())
55
- }
56
-
57
- nullish() {
58
- return new DateTimeType(...this._nullish())
60
+ examples(...values: [Date, ...Date[]]) {
61
+ return new DateType(
62
+ ...this._with({ options: { examples: values.map(encode) } }),
63
+ )
59
64
  }
60
65
  }
package/src/types/enum.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { SchemaOptions } from '@sinclair/typebox'
1
2
  import type { TNativeEnum } from '../schemas/native-enum.ts'
2
3
  import { NativeEnum } from '../schemas/native-enum.ts'
3
4
  import { type TUnionEnum, UnionEnum } from '../schemas/union-enum.ts'
@@ -7,28 +8,56 @@ export class NativeEnumType<
7
8
  T extends { [K in string]: K } = { [K in string]: K },
8
9
  N extends boolean = false,
9
10
  O extends boolean = false,
10
- > extends BaseType<TNativeEnum<T>, N, O> {
11
+ D extends boolean = false,
12
+ > extends BaseType<TNativeEnum<T>, N, O, D> {
11
13
  constructor(
12
14
  readonly values: T,
13
- nullable: N = false as N,
14
- optional: O = false as O,
15
+ options: SchemaOptions = {},
16
+ isNullable: N = false as N,
17
+ isOptional: O = false as O,
18
+ hasDefault: D = false as D,
15
19
  ) {
16
- super(NativeEnum(values), nullable, optional)
20
+ super(options, isNullable, isOptional, hasDefault)
21
+ }
22
+
23
+ protected _constructSchema(options: SchemaOptions): TNativeEnum<T> {
24
+ return NativeEnum(this.values, options)
17
25
  }
18
26
 
19
27
  nullable() {
20
- const [_, ...args] = this._nullable()
21
- return new NativeEnumType(this.values, ...args)
28
+ return new NativeEnumType(this.values, ...this._with({ isNullable: true }))
22
29
  }
23
30
 
24
31
  optional() {
25
- const [_, ...args] = this._optional()
26
- return new NativeEnumType(this.values, ...args)
32
+ return new NativeEnumType(this.values, ...this._with({ isOptional: true }))
27
33
  }
28
34
 
29
35
  nullish() {
30
- const [_, ...args] = this._nullish()
31
- return new NativeEnumType(this.values, ...args)
36
+ return new NativeEnumType(
37
+ this.values,
38
+ ...this._with({ isNullable: true, isOptional: true }),
39
+ )
40
+ }
41
+
42
+ default(value: keyof T) {
43
+ return new NativeEnumType(
44
+ this.values,
45
+ ...this._with({ options: { default: value }, hasDefault: true }),
46
+ )
47
+ }
48
+
49
+ description(description: string) {
50
+ return new NativeEnumType(
51
+ this.values,
52
+ ...this._with({ options: { description } }),
53
+ )
54
+ }
55
+
56
+ examples(...examples: (keyof T)[]) {
57
+ return new NativeEnumType(
58
+ this.values,
59
+ ...this._with({ options: { examples } }),
60
+ )
32
61
  }
33
62
  }
34
63
 
@@ -36,27 +65,55 @@ export class EnumType<
36
65
  T extends (string | number)[],
37
66
  N extends boolean = false,
38
67
  O extends boolean = false,
39
- > extends BaseType<TUnionEnum<T>, N, O> {
68
+ D extends boolean = false,
69
+ > extends BaseType<TUnionEnum<T>, N, O, D> {
40
70
  constructor(
41
- readonly values: [...T],
42
- nullable: N = false as N,
43
- optional: O = false as O,
71
+ protected readonly values: [...T],
72
+ options: SchemaOptions = {},
73
+ isNullable: N = false as N,
74
+ isOptional: O = false as O,
75
+ hasDefault: D = false as D,
44
76
  ) {
45
- super(UnionEnum(values), nullable, optional)
77
+ super(options, isNullable, isOptional, hasDefault, values)
78
+ }
79
+
80
+ protected _constructSchema(
81
+ options: SchemaOptions,
82
+ values: [...T],
83
+ ): TUnionEnum<T> {
84
+ return UnionEnum(values, options)
46
85
  }
47
86
 
48
87
  nullable() {
49
- const [_, ...args] = this._nullable()
50
- return new EnumType(this.values, ...args)
88
+ return new EnumType(this.values, ...this._with({ isNullable: true }))
51
89
  }
52
90
 
53
91
  optional() {
54
- const [_, ...args] = this._optional()
55
- return new EnumType(this.values, ...args)
92
+ return new EnumType(this.values, ...this._with({ isOptional: true }))
56
93
  }
57
94
 
58
95
  nullish() {
59
- const [_, ...args] = this._nullish()
60
- return new EnumType(this.values, ...args)
96
+ return new EnumType(
97
+ this.values,
98
+ ...this._with({ isNullable: true, isOptional: true }),
99
+ )
100
+ }
101
+
102
+ default(value: T[number]) {
103
+ return new EnumType(
104
+ this.values,
105
+ ...this._with({ options: { default: value }, hasDefault: true }),
106
+ )
107
+ }
108
+
109
+ description(description: string) {
110
+ return new EnumType(
111
+ this.values,
112
+ ...this._with({ options: { description } }),
113
+ )
114
+ }
115
+
116
+ examples(...examples: [T[number], ...T[number][]]) {
117
+ return new EnumType(this.values, ...this._with({ options: { examples } }))
61
118
  }
62
119
  }
@@ -1,31 +1,61 @@
1
- import { type TLiteral, type TLiteralValue, Type } from '@sinclair/typebox'
1
+ import {
2
+ type SchemaOptions,
3
+ type TLiteral,
4
+ type TLiteralValue,
5
+ Type,
6
+ } from '@sinclair/typebox'
2
7
  import { BaseType } from './base.ts'
3
8
 
4
9
  export class LiteralType<
5
10
  T extends TLiteralValue = TLiteralValue,
6
11
  N extends boolean = false,
7
12
  O extends boolean = false,
8
- > extends BaseType<TLiteral<T>, N, O> {
13
+ D extends boolean = false,
14
+ > extends BaseType<TLiteral<T>, N, O, D> {
9
15
  constructor(
10
- readonly value: T,
11
- nullable: N = false as N,
12
- optional: O = false as O,
16
+ protected readonly value: T,
17
+ options: SchemaOptions = {},
18
+ isNullable: N = false as N,
19
+ isOptional: O = false as O,
20
+ hasDefault: D = false as D,
13
21
  ) {
14
- super(Type.Literal(value), nullable, optional)
22
+ super(options, isNullable, isOptional, hasDefault, value)
23
+ }
24
+
25
+ protected _constructSchema(options: SchemaOptions, value: T): TLiteral<T> {
26
+ return Type.Literal(value, options)
15
27
  }
16
28
 
17
29
  nullable() {
18
- const [_, ...args] = this._nullable()
19
- return new LiteralType(this.value, ...args)
30
+ return new LiteralType(this.value, this.options, true, this.isOptional)
20
31
  }
21
32
 
22
33
  optional() {
23
- const [_, ...args] = this._optional()
24
- return new LiteralType(this.value, ...args)
34
+ return new LiteralType(this.value, ...this._with({ isOptional: true }))
25
35
  }
26
36
 
27
37
  nullish() {
28
- const [_, ...args] = this._nullish()
29
- return new LiteralType(this.value, ...args)
38
+ return new LiteralType(
39
+ this.value,
40
+ ...this._with({ isNullable: true, isOptional: true }),
41
+ )
42
+ }
43
+
44
+ default(value: T = this.value) {
45
+ return new LiteralType(
46
+ this.value,
47
+ ...this._with({ options: { default: value }, hasDefault: true }),
48
+ )
49
+ }
50
+
51
+ description(description: string) {
52
+ return new LiteralType(
53
+ this.value,
54
+ ...this._with({ options: { description } }),
55
+ )
56
+ }
57
+
58
+ examples(...examples: [T, ...T[]]) {
59
+ return new LiteralType(this.value, ...this._with({ options: { examples } }))
30
60
  }
31
61
  }
@@ -1,27 +1,40 @@
1
- import { type TNever, Type } from '@sinclair/typebox'
1
+ import { type SchemaOptions, type TNever, Type } from '@sinclair/typebox'
2
2
  import { BaseType } from './base.ts'
3
3
 
4
4
  export class NeverType<
5
5
  N extends boolean = false,
6
6
  O extends boolean = false,
7
- > extends BaseType<TNever, N, O> {
8
- constructor(
9
- schema = Type.Never(),
10
- nullable: N = false as N,
11
- optional: O = false as O,
12
- ) {
13
- super(schema, nullable, optional)
7
+ D extends boolean = false,
8
+ > extends BaseType<TNever, N, O, D> {
9
+ constructor(options: SchemaOptions = {}) {
10
+ super(options, false as N, false as O, false as D)
14
11
  }
15
12
 
16
- nullable(): NeverType<true, O> {
13
+ protected _constructSchema(options: SchemaOptions): TNever {
14
+ return Type.Never(options)
15
+ }
16
+
17
+ nullable(): NeverType<true, O, D> {
17
18
  throw new Error('NeverType cannot be nullable')
18
19
  }
19
20
 
20
- optional(): NeverType<N, true> {
21
+ optional(): NeverType<N, true, D> {
21
22
  throw new Error('NeverType cannot be optional')
22
23
  }
23
24
 
24
- nullish(): NeverType<true, true> {
25
+ nullish(): NeverType<true, true, D> {
25
26
  throw new Error('NeverType cannot be nullish')
26
27
  }
28
+
29
+ default(): NeverType<N, O, true> {
30
+ throw new Error('NeverType cannot have a default value')
31
+ }
32
+
33
+ description(description: string): NeverType<N, O, D> {
34
+ return new NeverType({ ...this.options, description })
35
+ }
36
+
37
+ examples(): NeverType<N, O, D> {
38
+ throw new Error('NeverType cannot have examples')
39
+ }
27
40
  }