@nmtjs/type 0.3.1 → 0.3.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/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/types/number.js +85 -1
- package/dist/types/number.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +2 -1
- package/src/types/number.ts +93 -0
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { CustomType } from "./types/custom.js";
|
|
|
4
4
|
import { DateType } from "./types/datetime.js";
|
|
5
5
|
import { EnumType, NativeEnumType } from "./types/enum.js";
|
|
6
6
|
import { LiteralType } from "./types/literal.js";
|
|
7
|
-
import { IntegerType, NumberType } from "./types/number.js";
|
|
7
|
+
import { BigIntType, IntegerType, NumberType } from "./types/number.js";
|
|
8
8
|
import { ObjectType } from "./types/object.js";
|
|
9
9
|
import { StringType } from "./types/string.js";
|
|
10
10
|
import { IntersactionType, UnionType } from "./types/union.js";
|
|
@@ -24,6 +24,7 @@ export var t;
|
|
|
24
24
|
t.string = ()=>new StringType();
|
|
25
25
|
t.number = ()=>new NumberType();
|
|
26
26
|
t.integer = ()=>new IntegerType();
|
|
27
|
+
t.bitint = ()=>new BigIntType();
|
|
27
28
|
t.literal = (value)=>new LiteralType(value);
|
|
28
29
|
t.nativeEnum = (enumLike)=>new NativeEnumType(enumLike);
|
|
29
30
|
t.arrayEnum = (enumLike)=>new EnumType(enumLike);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type { TLiteralValue } from '@sinclair/typebox'\nimport { ArrayType } from './types/array.ts'\nimport type { BaseType } from './types/base.ts'\nimport { BooleanType } from './types/boolean.ts'\nimport { CustomType } from './types/custom.ts'\nimport { DateType } from './types/datetime.ts'\nimport { EnumType, NativeEnumType } from './types/enum.ts'\nimport { LiteralType } from './types/literal.ts'\nimport { IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType } from './types/object.ts'\nimport { StringType } from './types/string.ts'\nimport { IntersactionType, UnionType } from './types/union.ts'\n\nimport type { typeStatic } from './constants.ts'\n// register ajv formats\nimport { register } from './formats.ts'\nimport { AnyType } from './types/any.ts'\nimport { NeverType } from './types/never.ts'\nregister()\n\nexport * from './schemas/native-enum.ts'\nexport * from './schemas/union-enum.ts'\nexport * from './schemas/nullable.ts'\nexport {\n BaseType,\n getTypeSchema,\n} from './types/base.ts'\nexport { type TSchema } from '@sinclair/typebox'\nexport {\n ArrayType,\n BooleanType,\n CustomType,\n DateType,\n EnumType,\n LiteralType,\n IntegerType,\n NumberType,\n ObjectType,\n StringType,\n IntersactionType,\n UnionType,\n AnyType,\n NeverType,\n}\n\nexport namespace t {\n export namespace infer {\n export type decoded<T extends BaseType> = T[typeStatic]['decoded']\n export type encoded<T extends BaseType> = T[typeStatic]['encoded']\n }\n export const never = () => new NeverType()\n export const boolean = () => new BooleanType()\n export const string = () => new StringType()\n export const number = () => new NumberType()\n export const integer = () => new IntegerType()\n export const literal = <T extends TLiteralValue>(value: T) =>\n new LiteralType(value)\n export const nativeEnum = <T extends { [K in string]: K }>(enumLike: T) =>\n new NativeEnumType(enumLike)\n export const arrayEnum = <T extends (string | number)[]>(enumLike: [...T]) =>\n new EnumType(enumLike)\n export const date = () => new DateType()\n export const array = <T extends BaseType>(element: T) =>\n new ArrayType(element)\n export const object = <T extends Record<string, BaseType>>(properties: T) =>\n new ObjectType(properties)\n export const any = () => new AnyType()\n export const or = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new UnionType(types)\n export const and = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new IntersactionType(types)\n export const custom = <T>(\n decode: (value: any) => T,\n encode: (value: T) => any,\n ) => new CustomType<T>(decode, encode)\n}\n"],"names":["ArrayType","BooleanType","CustomType","DateType","EnumType","NativeEnumType","LiteralType","IntegerType","NumberType","ObjectType","StringType","IntersactionType","UnionType","register","AnyType","NeverType","BaseType","getTypeSchema","t","never","boolean","string","number","integer","literal","value","nativeEnum","enumLike","arrayEnum","date","array","element","object","properties","any","or","types","and","custom","decode","encode"],"mappings":"AACA,SAASA,SAAS,QAAQ,mBAAkB;AAE5C,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,QAAQ,QAAQ,sBAAqB;AAC9C,SAASC,QAAQ,EAAEC,cAAc,QAAQ,kBAAiB;AAC1D,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,WAAW,EAAEC,UAAU,QAAQ,oBAAmB;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import type { TLiteralValue } from '@sinclair/typebox'\nimport { ArrayType } from './types/array.ts'\nimport type { BaseType } from './types/base.ts'\nimport { BooleanType } from './types/boolean.ts'\nimport { CustomType } from './types/custom.ts'\nimport { DateType } from './types/datetime.ts'\nimport { EnumType, NativeEnumType } from './types/enum.ts'\nimport { LiteralType } from './types/literal.ts'\nimport { BigIntType, IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType } from './types/object.ts'\nimport { StringType } from './types/string.ts'\nimport { IntersactionType, UnionType } from './types/union.ts'\n\nimport type { typeStatic } from './constants.ts'\n// register ajv formats\nimport { register } from './formats.ts'\nimport { AnyType } from './types/any.ts'\nimport { NeverType } from './types/never.ts'\nregister()\n\nexport * from './schemas/native-enum.ts'\nexport * from './schemas/union-enum.ts'\nexport * from './schemas/nullable.ts'\nexport {\n BaseType,\n getTypeSchema,\n} from './types/base.ts'\nexport { type TSchema } from '@sinclair/typebox'\nexport {\n ArrayType,\n BooleanType,\n CustomType,\n DateType,\n EnumType,\n LiteralType,\n IntegerType,\n NumberType,\n ObjectType,\n StringType,\n IntersactionType,\n UnionType,\n AnyType,\n NeverType,\n}\n\nexport namespace t {\n export namespace infer {\n export type decoded<T extends BaseType> = T[typeStatic]['decoded']\n export type encoded<T extends BaseType> = T[typeStatic]['encoded']\n }\n export const never = () => new NeverType()\n export const boolean = () => new BooleanType()\n export const string = () => new StringType()\n export const number = () => new NumberType()\n export const integer = () => new IntegerType()\n export const bitint = () => new BigIntType()\n export const literal = <T extends TLiteralValue>(value: T) =>\n new LiteralType(value)\n export const nativeEnum = <T extends { [K in string]: K }>(enumLike: T) =>\n new NativeEnumType(enumLike)\n export const arrayEnum = <T extends (string | number)[]>(enumLike: [...T]) =>\n new EnumType(enumLike)\n export const date = () => new DateType()\n export const array = <T extends BaseType>(element: T) =>\n new ArrayType(element)\n export const object = <T extends Record<string, BaseType>>(properties: T) =>\n new ObjectType(properties)\n export const any = () => new AnyType()\n export const or = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new UnionType(types)\n export const and = <T extends [BaseType, BaseType, ...BaseType[]]>(\n ...types: T\n ) => new IntersactionType(types)\n export const custom = <T>(\n decode: (value: any) => T,\n encode: (value: T) => any,\n ) => new CustomType<T>(decode, encode)\n}\n"],"names":["ArrayType","BooleanType","CustomType","DateType","EnumType","NativeEnumType","LiteralType","BigIntType","IntegerType","NumberType","ObjectType","StringType","IntersactionType","UnionType","register","AnyType","NeverType","BaseType","getTypeSchema","t","never","boolean","string","number","integer","bitint","literal","value","nativeEnum","enumLike","arrayEnum","date","array","element","object","properties","any","or","types","and","custom","decode","encode"],"mappings":"AACA,SAASA,SAAS,QAAQ,mBAAkB;AAE5C,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,QAAQ,QAAQ,sBAAqB;AAC9C,SAASC,QAAQ,EAAEC,cAAc,QAAQ,kBAAiB;AAC1D,SAASC,WAAW,QAAQ,qBAAoB;AAChD,SAASC,UAAU,EAAEC,WAAW,EAAEC,UAAU,QAAQ,oBAAmB;AACvE,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,UAAU,QAAQ,oBAAmB;AAC9C,SAASC,gBAAgB,EAAEC,SAAS,QAAQ,mBAAkB;AAI9D,SAASC,QAAQ,QAAQ,eAAc;AACvC,SAASC,OAAO,QAAQ,iBAAgB;AACxC,SAASC,SAAS,QAAQ,mBAAkB;AAC5CF;AAEA,cAAc,2BAA0B;AACxC,cAAc,0BAAyB;AACvC,cAAc,wBAAuB;AACrC,SACEG,QAAQ,EACRC,aAAa,QACR,kBAAiB;AAExB,SACElB,SAAS,EACTC,WAAW,EACXC,UAAU,EACVC,QAAQ,EACRC,QAAQ,EACRE,WAAW,EACXE,WAAW,EACXC,UAAU,EACVC,UAAU,EACVC,UAAU,EACVC,gBAAgB,EAChBC,SAAS,EACTE,OAAO,EACPC,SAAS,KACV;;UAEgBG;MAKFC,QAAQ,IAAM,IAAIJ;MAClBK,UAAU,IAAM,IAAIpB;MACpBqB,SAAS,IAAM,IAAIX;MACnBY,SAAS,IAAM,IAAId;MACnBe,UAAU,IAAM,IAAIhB;MACpBiB,SAAS,IAAM,IAAIlB;MACnBmB,UAAU,CAA0BC,QAC/C,IAAIrB,YAAYqB;MACLC,aAAa,CAAiCC,WACzD,IAAIxB,eAAewB;MACRC,YAAY,CAAgCD,WACvD,IAAIzB,SAASyB;MACFE,OAAO,IAAM,IAAI5B;MACjB6B,QAAQ,CAAqBC,UACxC,IAAIjC,UAAUiC;MACHC,SAAS,CAAqCC,aACzD,IAAIzB,WAAWyB;MACJC,MAAM,IAAM,IAAIrB;MAChBsB,KAAK,CAChB,GAAGC,QACA,IAAIzB,UAAUyB;MACNC,MAAM,CACjB,GAAGD,QACA,IAAI1B,iBAAiB0B;MACbE,SAAS,CACpBC,QACAC,SACG,IAAIxC,WAAcuC,QAAQC;AACjC,GAjCiBvB,MAAAA"}
|
package/dist/types/number.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Type } from '@sinclair/typebox';
|
|
1
|
+
import { Type, TypeBoxError } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class NumberType extends BaseType {
|
|
4
4
|
options;
|
|
@@ -146,3 +146,87 @@ export class IntegerType extends BaseType {
|
|
|
146
146
|
}));
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
|
+
export class BigIntType extends BaseType {
|
|
150
|
+
constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
151
|
+
super(options, isNullable, isOptional, hasDefault);
|
|
152
|
+
}
|
|
153
|
+
_constructSchema(options) {
|
|
154
|
+
return Type.Transform(Type.String({
|
|
155
|
+
...options,
|
|
156
|
+
pattern: '^\\d$'
|
|
157
|
+
})).Decode((v)=>{
|
|
158
|
+
try {
|
|
159
|
+
return BigInt(v);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
throw new TypeBoxError('Invalid bigint value');
|
|
162
|
+
}
|
|
163
|
+
}).Encode(this.encode);
|
|
164
|
+
}
|
|
165
|
+
encode = (value)=>{
|
|
166
|
+
return value.toString();
|
|
167
|
+
};
|
|
168
|
+
nullable() {
|
|
169
|
+
return new BigIntType(...this._with({
|
|
170
|
+
isNullable: true
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
optional() {
|
|
174
|
+
return new BigIntType(...this._with({
|
|
175
|
+
isOptional: true
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
nullish() {
|
|
179
|
+
return new BigIntType(...this._with({
|
|
180
|
+
isNullable: true,
|
|
181
|
+
isOptional: true
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
default(value) {
|
|
185
|
+
return new BigIntType(...this._with({
|
|
186
|
+
options: {
|
|
187
|
+
default: value
|
|
188
|
+
},
|
|
189
|
+
hasDefault: true
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
description(description) {
|
|
193
|
+
return new BigIntType(...this._with({
|
|
194
|
+
options: {
|
|
195
|
+
description
|
|
196
|
+
}
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
examples(...examples) {
|
|
200
|
+
return new BigIntType(...this._with({
|
|
201
|
+
options: {
|
|
202
|
+
examples: examples.map(this.encode)
|
|
203
|
+
}
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
positive() {
|
|
207
|
+
return this.min(0n, true);
|
|
208
|
+
}
|
|
209
|
+
negative() {
|
|
210
|
+
return this.max(0n, true);
|
|
211
|
+
}
|
|
212
|
+
max(value, exclusive) {
|
|
213
|
+
return new BigIntType(...this._with({
|
|
214
|
+
options: {
|
|
215
|
+
maximum: this.encode(value),
|
|
216
|
+
...exclusive ? {} : {
|
|
217
|
+
exclusiveMaximum: this.encode(value)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}));
|
|
221
|
+
}
|
|
222
|
+
min(value, exclusive) {
|
|
223
|
+
return new BigIntType(...this._with({
|
|
224
|
+
options: {
|
|
225
|
+
minimum: `${value}`,
|
|
226
|
+
...exclusive ? {} : {
|
|
227
|
+
exclusiveMinimum: `${value}`
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
232
|
+
}
|
package/dist/types/number.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/number.ts"],"sourcesContent":["import {\n type IntegerOptions,\n type NumberOptions,\n type TInteger,\n type TNumber,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class NumberType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TNumber, N, O, D, NumberOptions> {\n constructor(\n protected readonly options: NumberOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: NumberOptions): TNumber {\n return Type.Number(options)\n }\n\n nullable() {\n return new NumberType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new NumberType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new NumberType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: number) {\n return new NumberType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new NumberType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [number, ...number[]]) {\n return new NumberType(...this._with({ options: { examples } }))\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return new NumberType(\n ...this._with({\n options: {\n maximum: value,\n ...(exclusive ? {} : { exclusiveMaximum: value }),\n },\n }),\n )\n }\n\n min(value: number, exclusive?: true) {\n return new NumberType(\n ...this._with({\n options: {\n minimum: value,\n ...(exclusive ? {} : { exclusiveMinimum: value }),\n },\n }),\n )\n }\n}\n\nexport class IntegerType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TInteger, N, O, D, IntegerOptions> {\n constructor(\n options: IntegerOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: IntegerOptions): TInteger {\n return Type.Integer(options)\n }\n\n nullable() {\n return new IntegerType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new IntegerType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new IntegerType(\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: number) {\n return new IntegerType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new IntegerType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [number, ...number[]]) {\n return new IntegerType(...this._with({ options: { examples } }))\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return new IntegerType(\n ...this._with({\n options: {\n maximum: value,\n ...(exclusive ? {} : { exclusiveMaximum: value }),\n },\n }),\n )\n }\n\n min(value: number, exclusive?: true) {\n return new IntegerType(\n ...this._with({\n options: {\n minimum: value,\n ...(exclusive ? {} : { exclusiveMinimum: value }),\n },\n }),\n )\n }\n}\n"],"names":["Type","BaseType","NumberType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","Number","nullable","_with","optional","nullish","default","value","description","examples","positive","min","negative","max","exclusive","maximum","exclusiveMaximum","minimum","exclusiveMinimum","IntegerType","Integer"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,mBAIHD;;IACRE,YACE,AAAmBC,UAAyB,CAAC,CAAC,EAC9CC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;aALpBH,UAAAA;IAMrB;IAEUI,iBAAiBJ,OAAsB,EAAW;QAC1D,OAAOJ,KAAKS,MAAM,CAACL;IACrB;IAEAM,WAAW;QACT,OAAO,IAAIR,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;QAAK;IACzD;IAEAO,WAAW;QACT,OAAO,IAAIV,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEL,YAAY;QAAK;IACzD;IAEAO,UAAU;QACR,OAAO,IAAIX,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;YAAMC,YAAY;QAAK;IAC3E;IAEAQ,QAAQC,KAAa,EAAE;QACrB,OAAO,IAAIb,cACN,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEU,SAASC;YAAM;YAAGR,YAAY;QAAK;IAElE;IAEAS,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAId,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEY;YAAY;QAAE;IACjE;IAEAC,SAAS,GAAGA,QAA+B,EAAE;QAC3C,OAAO,IAAIf,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEa;YAAS;QAAE;IAC9D;IAEAC,WAAW;QACT,OAAO,IAAI,CAACC,GAAG,CAAC,GAAG;IACrB;IAEAC,WAAW;QACT,OAAO,I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S,GAAGA,QAA+B,EAAE;QAC3C,OAAO,IAAIU,eAAe,IAAI,CAAChB,KAAK,CAAC;YAAEP,SAAS;gBAAEa;YAAS;QAAE;IAC/D;IAEAC,WAAW;QACT,OAAO,IAAI,CAACC,GAAG,CAAC,GAAG;IACrB;IAEAC,WAAW;QACT,OAAO,IAAI,CAACC,GAAG,CAAC,GAAG;IACrB;IAEAA,IAAIN,KAAa,EAAEO,SAAgB,EAAE;QACnC,OAAO,IAAIK,eACN,IAAI,CAAChB,KAAK,CAAC;YACZP,SAAS;gBACPmB,SAASR;gBACT,GAAIO,YAAY,CAAC,IAAI;oBAAEE,kBAAkBT;gBAAM,CAAC;YAClD;QACF;IAEJ;IAEAI,IAAIJ,KAAa,EAAEO,SAAgB,EAAE;QACnC,OAAO,IAAIK,eACN,IAAI,CAAChB,KAAK,CAAC;YACZP,SAAS;gBACPqB,SAASV;gBACT,GAAIO,YAAY,CAAC,IAAI;oBAAEI,kBAAkBX;gBAAM,CAAC;YAClD;QACF;IAEJ;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/types/number.ts"],"sourcesContent":["import {\n type IntegerOptions,\n type NumberOptions,\n type StringOptions,\n type TInteger,\n type TNumber,\n type TString,\n type TTransform,\n Type,\n TypeBoxError,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class NumberType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TNumber, N, O, D, NumberOptions> {\n constructor(\n protected readonly options: NumberOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: NumberOptions): TNumber {\n return Type.Number(options)\n }\n\n nullable() {\n return new NumberType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new NumberType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new NumberType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: number) {\n return new NumberType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new NumberType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [number, ...number[]]) {\n return new NumberType(...this._with({ options: { examples } }))\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return new NumberType(\n ...this._with({\n options: {\n maximum: value,\n ...(exclusive ? {} : { exclusiveMaximum: value }),\n },\n }),\n )\n }\n\n min(value: number, exclusive?: true) {\n return new NumberType(\n ...this._with({\n options: {\n minimum: value,\n ...(exclusive ? {} : { exclusiveMinimum: value }),\n },\n }),\n )\n }\n}\n\nexport class IntegerType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TInteger, N, O, D, IntegerOptions> {\n constructor(\n options: IntegerOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: IntegerOptions): TInteger {\n return Type.Integer(options)\n }\n\n nullable() {\n return new IntegerType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new IntegerType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new IntegerType(\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: number) {\n return new IntegerType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new IntegerType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [number, ...number[]]) {\n return new IntegerType(...this._with({ options: { examples } }))\n }\n\n positive() {\n return this.min(0, true)\n }\n\n negative() {\n return this.max(0, true)\n }\n\n max(value: number, exclusive?: true) {\n return new IntegerType(\n ...this._with({\n options: {\n maximum: value,\n ...(exclusive ? {} : { exclusiveMaximum: value }),\n },\n }),\n )\n }\n\n min(value: number, exclusive?: true) {\n return new IntegerType(\n ...this._with({\n options: {\n minimum: value,\n ...(exclusive ? {} : { exclusiveMinimum: value }),\n },\n }),\n )\n }\n}\n\nexport class BigIntType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TTransform<TString, bigint>, N, O, D, StringOptions> {\n constructor(\n options: StringOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(options: StringOptions) {\n return Type.Transform(Type.String({ ...options, pattern: '^\\\\d$' }))\n .Decode((v: string) => {\n try {\n return BigInt(v)\n } catch (error) {\n throw new TypeBoxError('Invalid bigint value')\n }\n })\n .Encode(this.encode)\n }\n\n protected encode = (value: bigint): string => {\n return value.toString()\n }\n\n nullable() {\n return new BigIntType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new BigIntType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new BigIntType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: bigint) {\n return new BigIntType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new BigIntType(...this._with({ options: { description } }))\n }\n\n examples(...examples: [bigint, ...bigint[]]) {\n return new BigIntType(\n ...this._with({ options: { examples: examples.map(this.encode) } }),\n )\n }\n\n positive() {\n return this.min(0n, true)\n }\n\n negative() {\n return this.max(0n, true)\n }\n\n max(value: bigint, exclusive?: true) {\n return new BigIntType(\n ...this._with({\n options: {\n maximum: this.encode(value),\n ...(exclusive ? {} : { exclusiveMaximum: this.encode(value) }),\n },\n }),\n )\n }\n\n min(value: bigint, exclusive?: true) {\n return new BigIntType(\n ...this._with({\n options: {\n minimum: `${value}`,\n ...(exclusive ? {} : { exclusiveMinimum: `${value}` }),\n },\n }),\n )\n }\n}\n"],"names":["Type","TypeBoxError","BaseType","NumberType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","Number","nullable","_with","optional","nullish","default","value","description","examples","positive","min","negative","max","exclusive","maximum","exclusiveMaximum","minimum","exclusiveMinimum","IntegerType","Integer","BigIntType","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"peerDependencies": {
|
|
22
22
|
"@sinclair/typebox": "^0.33.7",
|
|
23
23
|
"temporal-polyfill": "^0.2.5",
|
|
24
|
-
"@nmtjs/common": "0.3.
|
|
24
|
+
"@nmtjs/common": "0.3.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@sinclair/typebox": "^0.33.7",
|
|
28
28
|
"temporal-polyfill": "^0.2.5",
|
|
29
|
-
"@nmtjs/common": "0.3.
|
|
29
|
+
"@nmtjs/common": "0.3.2"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"src",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"LICENSE.md",
|
|
36
36
|
"README.md"
|
|
37
37
|
],
|
|
38
|
-
"version": "0.3.
|
|
38
|
+
"version": "0.3.2",
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
41
41
|
"type-check": "tsc --noEmit"
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { CustomType } from './types/custom.ts'
|
|
|
6
6
|
import { DateType } from './types/datetime.ts'
|
|
7
7
|
import { EnumType, NativeEnumType } from './types/enum.ts'
|
|
8
8
|
import { LiteralType } from './types/literal.ts'
|
|
9
|
-
import { IntegerType, NumberType } from './types/number.ts'
|
|
9
|
+
import { BigIntType, IntegerType, NumberType } from './types/number.ts'
|
|
10
10
|
import { ObjectType } from './types/object.ts'
|
|
11
11
|
import { StringType } from './types/string.ts'
|
|
12
12
|
import { IntersactionType, UnionType } from './types/union.ts'
|
|
@@ -53,6 +53,7 @@ export namespace t {
|
|
|
53
53
|
export const string = () => new StringType()
|
|
54
54
|
export const number = () => new NumberType()
|
|
55
55
|
export const integer = () => new IntegerType()
|
|
56
|
+
export const bitint = () => new BigIntType()
|
|
56
57
|
export const literal = <T extends TLiteralValue>(value: T) =>
|
|
57
58
|
new LiteralType(value)
|
|
58
59
|
export const nativeEnum = <T extends { [K in string]: K }>(enumLike: T) =>
|
package/src/types/number.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type IntegerOptions,
|
|
3
3
|
type NumberOptions,
|
|
4
|
+
type StringOptions,
|
|
4
5
|
type TInteger,
|
|
5
6
|
type TNumber,
|
|
7
|
+
type TString,
|
|
8
|
+
type TTransform,
|
|
6
9
|
Type,
|
|
10
|
+
TypeBoxError,
|
|
7
11
|
} from '@sinclair/typebox'
|
|
8
12
|
import { BaseType } from './base.ts'
|
|
9
13
|
|
|
@@ -158,3 +162,92 @@ export class IntegerType<
|
|
|
158
162
|
)
|
|
159
163
|
}
|
|
160
164
|
}
|
|
165
|
+
|
|
166
|
+
export class BigIntType<
|
|
167
|
+
N extends boolean = false,
|
|
168
|
+
O extends boolean = false,
|
|
169
|
+
D extends boolean = false,
|
|
170
|
+
> extends BaseType<TTransform<TString, bigint>, N, O, D, StringOptions> {
|
|
171
|
+
constructor(
|
|
172
|
+
options: StringOptions = {},
|
|
173
|
+
isNullable: N = false as N,
|
|
174
|
+
isOptional: O = false as O,
|
|
175
|
+
hasDefault: D = false as D,
|
|
176
|
+
) {
|
|
177
|
+
super(options, isNullable, isOptional, hasDefault)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
protected _constructSchema(options: StringOptions) {
|
|
181
|
+
return Type.Transform(Type.String({ ...options, pattern: '^\\d$' }))
|
|
182
|
+
.Decode((v: string) => {
|
|
183
|
+
try {
|
|
184
|
+
return BigInt(v)
|
|
185
|
+
} catch (error) {
|
|
186
|
+
throw new TypeBoxError('Invalid bigint value')
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
.Encode(this.encode)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
protected encode = (value: bigint): string => {
|
|
193
|
+
return value.toString()
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
nullable() {
|
|
197
|
+
return new BigIntType(...this._with({ isNullable: true }))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
optional() {
|
|
201
|
+
return new BigIntType(...this._with({ isOptional: true }))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
nullish() {
|
|
205
|
+
return new BigIntType(...this._with({ isNullable: true, isOptional: true }))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
default(value: bigint) {
|
|
209
|
+
return new BigIntType(
|
|
210
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
211
|
+
)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
description(description: string) {
|
|
215
|
+
return new BigIntType(...this._with({ options: { description } }))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
examples(...examples: [bigint, ...bigint[]]) {
|
|
219
|
+
return new BigIntType(
|
|
220
|
+
...this._with({ options: { examples: examples.map(this.encode) } }),
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
positive() {
|
|
225
|
+
return this.min(0n, true)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
negative() {
|
|
229
|
+
return this.max(0n, true)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
max(value: bigint, exclusive?: true) {
|
|
233
|
+
return new BigIntType(
|
|
234
|
+
...this._with({
|
|
235
|
+
options: {
|
|
236
|
+
maximum: this.encode(value),
|
|
237
|
+
...(exclusive ? {} : { exclusiveMaximum: this.encode(value) }),
|
|
238
|
+
},
|
|
239
|
+
}),
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
min(value: bigint, exclusive?: true) {
|
|
244
|
+
return new BigIntType(
|
|
245
|
+
...this._with({
|
|
246
|
+
options: {
|
|
247
|
+
minimum: `${value}`,
|
|
248
|
+
...(exclusive ? {} : { exclusiveMinimum: `${value}` }),
|
|
249
|
+
},
|
|
250
|
+
}),
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
}
|