@nmtjs/type 0.2.1 → 0.3.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 (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,124 +1,160 @@
1
- import { type TInteger, type TNumber, Type } from '@sinclair/typebox'
1
+ import {
2
+ type IntegerOptions,
3
+ type NumberOptions,
4
+ type TInteger,
5
+ type TNumber,
6
+ Type,
7
+ } from '@sinclair/typebox'
2
8
  import { BaseType } from './base.ts'
3
9
 
4
10
  export class NumberType<
5
11
  N extends boolean = false,
6
12
  O extends boolean = false,
7
- > extends BaseType<TNumber, N, O> {
13
+ D extends boolean = false,
14
+ > extends BaseType<TNumber, N, O, D, NumberOptions> {
8
15
  constructor(
9
- schema: TNumber = Type.Number(),
10
- nullable: N = false as N,
11
- optional: O = false as O,
16
+ protected readonly options: NumberOptions = {},
17
+ isNullable: N = false as N,
18
+ isOptional: O = false as O,
19
+ hasDefault: D = false as D,
12
20
  ) {
13
- super(schema, nullable, optional)
21
+ super(options, isNullable, isOptional, hasDefault)
14
22
  }
15
23
 
16
- positive() {
24
+ protected _constructSchema(options: NumberOptions): TNumber {
25
+ return Type.Number(options)
26
+ }
27
+
28
+ nullable() {
29
+ return new NumberType(...this._with({ isNullable: true }))
30
+ }
31
+
32
+ optional() {
33
+ return new NumberType(...this._with({ isOptional: true }))
34
+ }
35
+
36
+ nullish() {
37
+ return new NumberType(...this._with({ isNullable: true, isOptional: true }))
38
+ }
39
+
40
+ default(value: number) {
17
41
  return new NumberType(
18
- Type.Number({ ...this._schema, minimum: 0, exclusiveMinimum: 0 }),
19
- ...this._isNullableOptional,
42
+ ...this._with({ options: { default: value }, hasDefault: true }),
20
43
  )
21
44
  }
22
45
 
46
+ description(description: string) {
47
+ return new NumberType(...this._with({ options: { description } }))
48
+ }
49
+
50
+ examples(...examples: [number, ...number[]]) {
51
+ return new NumberType(...this._with({ options: { examples } }))
52
+ }
53
+
54
+ positive() {
55
+ return this.min(0, true)
56
+ }
57
+
23
58
  negative() {
24
- return new NumberType(
25
- Type.Number({ ...this._schema, maximum: 0, exclusiveMaximum: 0 }),
26
- ...this._isNullableOptional,
27
- )
59
+ return this.max(0, true)
28
60
  }
29
61
 
30
62
  max(value: number, exclusive?: true) {
31
63
  return new NumberType(
32
- Type.Number({
33
- ...this._schema,
34
- maximum: value,
35
- ...(exclusive ? {} : { exclusiveMaximum: value }),
64
+ ...this._with({
65
+ options: {
66
+ maximum: value,
67
+ ...(exclusive ? {} : { exclusiveMaximum: value }),
68
+ },
36
69
  }),
37
- ...this._isNullableOptional,
38
70
  )
39
71
  }
40
72
 
41
73
  min(value: number, exclusive?: true) {
42
74
  return new NumberType(
43
- Type.Number({
44
- ...this._schema,
45
- minimum: value,
46
- ...(exclusive ? {} : { exclusiveMinimum: value }),
75
+ ...this._with({
76
+ options: {
77
+ minimum: value,
78
+ ...(exclusive ? {} : { exclusiveMinimum: value }),
79
+ },
47
80
  }),
48
- ...this._isNullableOptional,
49
81
  )
50
82
  }
83
+ }
84
+
85
+ export class IntegerType<
86
+ N extends boolean = false,
87
+ O extends boolean = false,
88
+ D extends boolean = false,
89
+ > extends BaseType<TInteger, N, O, D, IntegerOptions> {
90
+ constructor(
91
+ options: IntegerOptions = {},
92
+ isNullable: N = false as N,
93
+ isOptional: O = false as O,
94
+ hasDefault: D = false as D,
95
+ ) {
96
+ super(options, isNullable, isOptional, hasDefault)
97
+ }
98
+
99
+ protected _constructSchema(options: IntegerOptions): TInteger {
100
+ return Type.Integer(options)
101
+ }
51
102
 
52
103
  nullable() {
53
- return new NumberType(...this._nullable())
104
+ return new IntegerType(...this._with({ isNullable: true }))
54
105
  }
55
106
 
56
107
  optional() {
57
- return new NumberType(...this._optional())
108
+ return new IntegerType(...this._with({ isOptional: true }))
58
109
  }
59
110
 
60
111
  nullish() {
61
- return new NumberType(...this._nullish())
112
+ return new IntegerType(
113
+ ...this._with({ isNullable: true, isOptional: true }),
114
+ )
62
115
  }
63
- }
64
116
 
65
- export class IntegerType<
66
- N extends boolean = false,
67
- O extends boolean = false,
68
- > extends BaseType<TInteger, N, O> {
69
- constructor(
70
- schema: TInteger = Type.Integer(),
71
- nullable: N = false as N,
72
- optional: O = false as O,
73
- ) {
74
- super(schema, nullable, optional)
117
+ default(value: number) {
118
+ return new IntegerType(
119
+ ...this._with({ options: { default: value }, hasDefault: true }),
120
+ )
121
+ }
122
+
123
+ description(description: string) {
124
+ return new IntegerType(...this._with({ options: { description } }))
125
+ }
126
+
127
+ examples(...examples: [number, ...number[]]) {
128
+ return new IntegerType(...this._with({ options: { examples } }))
75
129
  }
76
130
 
77
131
  positive() {
78
- return new IntegerType(
79
- Type.Integer({ ...this._schema, minimum: 0, exclusiveMinimum: 0 }),
80
- ...this._isNullableOptional,
81
- )
132
+ return this.min(0, true)
82
133
  }
83
134
 
84
135
  negative() {
85
- return new IntegerType(
86
- Type.Integer({ ...this._schema, maximum: 0, exclusiveMaximum: 0 }),
87
- ...this._isNullableOptional,
88
- )
136
+ return this.max(0, true)
89
137
  }
90
138
 
91
139
  max(value: number, exclusive?: true) {
92
140
  return new IntegerType(
93
- Type.Integer({
94
- ...this._schema,
95
- maximum: value,
96
- ...(exclusive ? {} : { exclusiveMaximum: value }),
141
+ ...this._with({
142
+ options: {
143
+ maximum: value,
144
+ ...(exclusive ? {} : { exclusiveMaximum: value }),
145
+ },
97
146
  }),
98
- ...this._isNullableOptional,
99
147
  )
100
148
  }
101
149
 
102
150
  min(value: number, exclusive?: true) {
103
151
  return new IntegerType(
104
- Type.Integer({
105
- ...this._schema,
106
- minimum: value,
107
- ...(exclusive ? {} : { exclusiveMinimum: value }),
152
+ ...this._with({
153
+ options: {
154
+ minimum: value,
155
+ ...(exclusive ? {} : { exclusiveMinimum: value }),
156
+ },
108
157
  }),
109
- ...this._isNullableOptional,
110
158
  )
111
159
  }
112
-
113
- nullable() {
114
- return new IntegerType(...this._nullable())
115
- }
116
-
117
- optional() {
118
- return new IntegerType(...this._optional())
119
- }
120
-
121
- nullish() {
122
- return new IntegerType(...this._nullish())
123
- }
124
160
  }
@@ -1,55 +1,97 @@
1
- import { type TObject, Type } from '@sinclair/typebox'
1
+ import {
2
+ type ObjectOptions,
3
+ type StaticEncode,
4
+ type TObject,
5
+ Type,
6
+ } from '@sinclair/typebox'
7
+ import type { typeStatic } from '../constants.ts'
2
8
  import type { UnionToTupleString } from '../utils.ts'
3
- import { BaseType, typeFinalSchema } from './base.ts'
9
+ import { BaseType, getTypeSchema } from './base.ts'
4
10
  import { EnumType } from './enum.ts'
5
11
 
6
12
  export class ObjectType<
7
13
  T extends Record<string, BaseType> = Record<string, BaseType>,
8
14
  N extends boolean = false,
9
15
  O extends boolean = false,
10
- > extends BaseType<TObject<{ [K in keyof T]: T[K][typeFinalSchema] }>, N, O> {
16
+ D extends boolean = false,
17
+ > extends BaseType<
18
+ TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,
19
+ N,
20
+ O,
21
+ D
22
+ > {
11
23
  constructor(
12
- readonly properties: T = {} as T,
13
- nullable: N = false as N,
14
- optional: O = false as O,
24
+ protected readonly properties: T = {} as T,
25
+ options: ObjectOptions = {},
26
+ isNullable: N = false as N,
27
+ isOptional: O = false as O,
28
+ hasDefault: D = false as D,
15
29
  ) {
16
- const schemaProperties = Object.fromEntries(
17
- Object.entries(properties).map(([key, value]) => [
18
- key,
19
- value[typeFinalSchema],
20
- ]),
21
- )
22
- super(
23
- Type.Object(
24
- schemaProperties as { [K in keyof T]: T[K][typeFinalSchema] },
25
- ),
26
- nullable,
27
- optional,
28
- )
30
+ super(options, isNullable, isOptional, hasDefault, properties)
31
+ }
32
+
33
+ protected _constructSchema(
34
+ options: ObjectOptions,
35
+ properties: T,
36
+ ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {
37
+ const schemaProperties = {} as {
38
+ [K in keyof T]: T[K][typeStatic]['schema']
39
+ }
40
+
41
+ for (const [key, value] of Object.entries(properties)) {
42
+ // @ts-expect-error
43
+ schemaProperties[key] = getTypeSchema(value)
44
+ }
45
+ return Type.Object(schemaProperties, options)
29
46
  }
30
47
 
31
48
  nullable() {
32
- const [_, ...args] = this._nullable()
33
- return new ObjectType(this.properties, ...args)
49
+ return new ObjectType(this.properties, ...this._with({ isNullable: true }))
34
50
  }
35
51
 
36
52
  optional() {
37
- const [_, ...args] = this._optional()
38
- return new ObjectType(this.properties, ...args)
53
+ return new ObjectType(this.properties, ...this._with({ isOptional: true }))
39
54
  }
40
55
 
41
56
  nullish() {
42
- const [_, ...args] = this._nullish()
43
- return new ObjectType(this.properties, ...args)
57
+ return new ObjectType(
58
+ this.properties,
59
+ ...this._with({ isNullable: true, isOptional: true }),
60
+ )
61
+ }
62
+
63
+ default(value: this[typeStatic]['encoded']) {
64
+ return new ObjectType(
65
+ this.properties,
66
+ ...this._with({ options: { default: value }, hasDefault: true }),
67
+ )
68
+ }
69
+
70
+ description(description: string) {
71
+ return new ObjectType(
72
+ this.properties,
73
+ ...this._with({ options: { description } }),
74
+ )
75
+ }
76
+
77
+ examples(
78
+ ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
79
+ ) {
80
+ return new ObjectType(
81
+ this.properties,
82
+ ...this._with({ options: { examples } }),
83
+ )
44
84
  }
45
85
 
46
86
  pick<P extends { [K in keyof T]?: true }>(pick: P) {
47
87
  const properties = Object.fromEntries(
48
88
  Object.entries(this.properties).filter(([key]) => pick[key]),
49
89
  )
90
+ const [_, ...args] = this._with()
50
91
  return new ObjectType(
51
92
  properties as Pick<T, Extract<keyof P, keyof T>>,
52
- ...this._isNullableOptional,
93
+ {},
94
+ ...args,
53
95
  )
54
96
  }
55
97
 
@@ -57,26 +99,39 @@ export class ObjectType<
57
99
  const properties = Object.fromEntries(
58
100
  Object.entries(this.properties).filter(([key]) => !omit[key]),
59
101
  )
102
+ const [_, ...args] = this._with()
60
103
  return new ObjectType(
61
104
  properties as Omit<T, Extract<keyof P, keyof T>>,
62
- ...this._isNullableOptional,
105
+ {},
106
+ ...args,
63
107
  )
64
108
  }
65
109
 
66
110
  extend<P extends Record<string, BaseType>>(properties: P) {
67
- return new ObjectType(
68
- { ...this.properties, ...properties },
69
- ...this._isNullableOptional,
70
- )
111
+ const [_, ...args] = this._with()
112
+ return new ObjectType({ ...this.properties, ...properties }, {}, ...args)
71
113
  }
72
114
 
73
115
  merge<T extends ObjectType>(object: T) {
116
+ const [_, ...args] = this._with()
74
117
  return new ObjectType(
75
118
  { ...this.properties, ...object.properties },
76
- ...this._isNullableOptional,
119
+ {},
120
+ ...args,
77
121
  )
78
122
  }
79
123
 
124
+ partial() {
125
+ const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =
126
+ {} as any
127
+ for (const [key, value] of Object.entries(this.properties)) {
128
+ // @ts-expect-error
129
+ properties[key] = value.optional()
130
+ }
131
+ const [_, ...args] = this._with()
132
+ return new ObjectType(properties, {}, ...args)
133
+ }
134
+
80
135
  keyof(): EnumType<UnionToTupleString<keyof T>> {
81
136
  return new EnumType(Object.keys(this.properties) as any)
82
137
  }
@@ -1,67 +1,75 @@
1
- import { type TString, Type } from '@sinclair/typebox'
1
+ import { type StringOptions, type TString, Type } from '@sinclair/typebox'
2
2
  import { BaseType } from './base.ts'
3
3
 
4
4
  export class StringType<
5
5
  N extends boolean = false,
6
6
  O extends boolean = false,
7
- > extends BaseType<TString, N, O> {
7
+ D extends boolean = false,
8
+ > extends BaseType<TString, N, O, D, StringOptions> {
8
9
  constructor(
9
- schema = Type.String(),
10
- nullable: N = false as N,
11
- optional: O = false as O,
10
+ options: StringOptions = {},
11
+ isNullable: N = false as N,
12
+ isOptional: O = false as O,
13
+ hasDefault: D = false as D,
12
14
  ) {
13
- super(schema, nullable, optional)
15
+ super(options, isNullable, isOptional, hasDefault)
16
+ }
17
+
18
+ protected _constructSchema(options: StringOptions): TString {
19
+ return Type.String(options)
14
20
  }
15
21
 
16
22
  nullable() {
17
- return new StringType(...this._nullable())
23
+ return new StringType(...this._with({ isNullable: true }))
18
24
  }
19
25
 
20
26
  optional() {
21
- return new StringType(...this._optional())
27
+ return new StringType(...this._with({ isOptional: true }))
22
28
  }
23
29
 
24
30
  nullish() {
25
- return new StringType(...this._nullish())
31
+ return new StringType(...this._with({ isNullable: true, isOptional: true }))
26
32
  }
27
33
 
28
- format(format: TString['format']) {
34
+ default(value: string) {
29
35
  return new StringType(
30
- {
31
- ...this._schema,
32
- format,
33
- },
34
- ...this._isNullableOptional,
36
+ ...this._with({ options: { default: value }, hasDefault: true }),
35
37
  )
36
38
  }
37
39
 
40
+ description(description: string) {
41
+ return new StringType(...this._with({ options: { description } }))
42
+ }
43
+
44
+ examples(...examples: string[]) {
45
+ return new StringType(...this._with({ options: { examples } }))
46
+ }
47
+
48
+ format(format: TString['format']) {
49
+ return new StringType(...this._with({ options: { format } }))
50
+ }
51
+
38
52
  max(value: number) {
39
53
  return new StringType(
40
- {
41
- ...this._schema,
42
- maxLength: value,
43
- },
44
- ...this._isNullableOptional,
54
+ ...this._with({
55
+ options: { maxLength: value },
56
+ }),
45
57
  )
46
58
  }
47
59
 
48
60
  min(value: number) {
49
61
  return new StringType(
50
- {
51
- ...this._schema,
52
- minLength: value,
53
- },
54
- ...this._isNullableOptional,
62
+ ...this._with({
63
+ options: { minLength: value },
64
+ }),
55
65
  )
56
66
  }
57
67
 
58
68
  pattern(pattern: string) {
59
69
  return new StringType(
60
- {
61
- ...this._schema,
62
- pattern,
63
- },
64
- ...this._isNullableOptional,
70
+ ...this._with({
71
+ options: { pattern },
72
+ }),
65
73
  )
66
74
  }
67
75