@nmtjs/type 0.3.3 → 0.3.4
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 +3 -2
- package/dist/index.js.map +1 -1
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js.map +1 -1
- package/dist/types/object.js +50 -0
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js.map +1 -1
- package/dist/types/union.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +29 -7
- package/src/types/enum.ts +5 -2
- package/src/types/literal.ts +8 -1
- package/src/types/object.ts +95 -4
- package/src/types/string.ts +1 -0
- package/src/types/union.ts +7 -1
package/dist/index.js
CHANGED
|
@@ -5,12 +5,12 @@ import { DateType } from "./types/datetime.js";
|
|
|
5
5
|
import { EnumType, NativeEnumType } from "./types/enum.js";
|
|
6
6
|
import { LiteralType } from "./types/literal.js";
|
|
7
7
|
import { BigIntType, IntegerType, NumberType } from "./types/number.js";
|
|
8
|
-
import { ObjectType } from "./types/object.js";
|
|
8
|
+
import { ObjectType, RecordType } from "./types/object.js";
|
|
9
9
|
import { StringType } from "./types/string.js";
|
|
10
10
|
import { IntersactionType, UnionType } from "./types/union.js";
|
|
11
|
-
import { register } from "./formats.js";
|
|
12
11
|
import { AnyType } from "./types/any.js";
|
|
13
12
|
import { NeverType } from "./types/never.js";
|
|
13
|
+
import { register } from "./formats.js";
|
|
14
14
|
register();
|
|
15
15
|
export * from "./schemas/native-enum.js";
|
|
16
16
|
export * from "./schemas/union-enum.js";
|
|
@@ -31,6 +31,7 @@ export var t;
|
|
|
31
31
|
t.date = ()=>new DateType();
|
|
32
32
|
t.array = (element)=>new ArrayType(element);
|
|
33
33
|
t.object = (properties)=>new ObjectType(properties);
|
|
34
|
+
t.record = (key, value)=>new RecordType(key, value);
|
|
34
35
|
t.any = ()=>new AnyType();
|
|
35
36
|
t.or = (...types)=>new UnionType(types);
|
|
36
37
|
t.and = (...types)=>new IntersactionType(types);
|
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
|
|
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 {\n type AnyEnumType,\n type AnyNativeEnumType,\n EnumType,\n NativeEnumType,\n} from './types/enum.ts'\nimport { type AnyLiteralType, LiteralType } from './types/literal.ts'\nimport { BigIntType, IntegerType, NumberType } from './types/number.ts'\nimport { ObjectType, RecordType } from './types/object.ts'\nimport { type AnyStringType, StringType } from './types/string.ts'\nimport {\n type AnyUnionType,\n IntersactionType,\n UnionType,\n} from './types/union.ts'\n\nimport type { typeStatic } from './constants.ts'\nimport { AnyType } from './types/any.ts'\nimport { NeverType } from './types/never.ts'\n\n// register ajv formats\nimport { register } from './formats.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 record = <\n K extends\n | AnyLiteralType\n | AnyEnumType\n | AnyNativeEnumType\n | AnyStringType\n | AnyUnionType,\n E extends BaseType,\n >(\n key: K,\n value: E,\n ) => new RecordType(key, value)\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","RecordType","StringType","IntersactionType","UnionType","AnyType","NeverType","register","BaseType","getTypeSchema","t","never","boolean","string","number","integer","bitint","literal","value","nativeEnum","enumLike","arrayEnum","date","array","element","object","properties","record","key","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,SAGEC,QAAQ,EACRC,cAAc,QACT,kBAAiB;AACxB,SAA8BC,WAAW,QAAQ,qBAAoB;AACrE,SAASC,UAAU,EAAEC,WAAW,EAAEC,UAAU,QAAQ,oBAAmB;AACvE,SAASC,UAAU,EAAEC,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 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import type { SchemaOptions } from '@sinclair/typebox'\nimport type { TNativeEnum } from '../schemas/native-enum.ts'\nimport { NativeEnum } from '../schemas/native-enum.ts'\nimport { type TUnionEnum, UnionEnum } from '../schemas/union-enum.ts'\nimport { BaseType } from './base.ts'\n\nexport
|
|
1
|
+
{"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import type { SchemaOptions } from '@sinclair/typebox'\nimport type { TNativeEnum } from '../schemas/native-enum.ts'\nimport { NativeEnum } from '../schemas/native-enum.ts'\nimport { type TUnionEnum, UnionEnum } from '../schemas/union-enum.ts'\nimport { BaseType } from './base.ts'\n\nexport type AnyNativeEnumType<T extends { [K in string]: K } = any> =\n NativeEnumType<T, boolean, boolean, boolean>\nexport class NativeEnumType<\n T extends { [K in string]: K },\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TNativeEnum<T>, N, O, D> {\n constructor(\n readonly values: T,\n options: SchemaOptions = {},\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: SchemaOptions): TNativeEnum<T> {\n return NativeEnum(this.values, options)\n }\n\n nullable() {\n return new NativeEnumType(this.values, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new NativeEnumType(this.values, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new NativeEnumType(\n this.values,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: keyof T) {\n return new NativeEnumType(\n this.values,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new NativeEnumType(\n this.values,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: (keyof T)[]) {\n return new NativeEnumType(\n this.values,\n ...this._with({ options: { examples } }),\n )\n }\n}\n\nexport type AnyEnumType = EnumType<any[], boolean, boolean, boolean>\nexport class EnumType<\n T extends (string | number)[] = (string | number)[],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TUnionEnum<T>, N, O, D> {\n constructor(\n protected readonly values: [...T],\n options: SchemaOptions = {},\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, values)\n }\n\n protected _constructSchema(\n options: SchemaOptions,\n values: [...T],\n ): TUnionEnum<T> {\n return UnionEnum(values, options)\n }\n\n nullable() {\n return new EnumType(this.values, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new EnumType(this.values, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new EnumType(\n this.values,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: T[number]) {\n return new EnumType(\n this.values,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new EnumType(\n this.values,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: [T[number], ...T[number][]]) {\n return new EnumType(this.values, ...this._with({ options: { examples } }))\n }\n}\n"],"names":["NativeEnum","UnionEnum","BaseType","NativeEnumType","constructor","values","options","isNullable","isOptional","hasDefault","_constructSchema","nullable","_with","optional","nullish","default","value","description","examples","EnumType"],"mappings":"AAEA,SAASA,UAAU,QAAQ,4BAA2B;AACtD,SAA0BC,SAAS,QAAQ,2BAA0B;AACrE,SAASC,QAAQ,QAAQ,YAAW;AAIpC,OAAO,MAAMC,uBAKHD;;IACRE,YACE,AAASC,MAAS,EAClBC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;aAN9BJ,SAAAA;IAOX;IAEUK,iBAAiBJ,OAAsB,EAAkB;QACjE,OAAON,WAAW,IAAI,CAACK,MAAM,EAAEC;IACjC;IAEAK,WAAW;QACT,OAAO,IAAIR,eAAe,IAAI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O,KAAK,CAAC;YAAEJ,YAAY;QAAK;IACpE;IAEAM,UAAU;QACR,OAAO,IAAIK,SACT,IAAI,CAACd,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEL,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAO,QAAQC,KAAgB,EAAE;QACxB,OAAO,IAAIG,SACT,IAAI,CAACd,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEN,SAAS;gBAAES,SAASC;YAAM;YAAGP,YAAY;QAAK;IAElE;IAEAQ,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAIE,SACT,IAAI,CAACd,MAAM,KACR,IAAI,CAACO,KAAK,CAAC;YAAEN,SAAS;gBAAEW;YAAY;QAAE;IAE7C;IAEAC,SAAS,GAAGA,QAAqC,EAAE;QACjD,OAAO,IAAIC,SAAS,IAAI,CAACd,MAAM,KAAK,IAAI,CAACO,KAAK,CAAC;YAAEN,SAAS;gBAAEY;YAAS;QAAE;IACzE;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/literal.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TLiteral,\n type TLiteralValue,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class LiteralType<\n T extends TLiteralValue
|
|
1
|
+
{"version":3,"sources":["../../../src/types/literal.ts"],"sourcesContent":["import {\n Extends,\n type SchemaOptions,\n type TLiteral,\n type TLiteralValue,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport type AnyLiteralType<T extends TLiteralValue = any> = LiteralType<\n T,\n boolean,\n boolean,\n boolean\n>\nexport class LiteralType<\n T extends TLiteralValue,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TLiteral<T>, N, O, D> {\n constructor(\n protected readonly value: T,\n options: SchemaOptions = {},\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, value)\n }\n\n protected _constructSchema(options: SchemaOptions, value: T): TLiteral<T> {\n return Type.Literal(value, options)\n }\n\n nullable() {\n return new LiteralType(this.value, this.options, true, this.isOptional)\n }\n\n optional() {\n return new LiteralType(this.value, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new LiteralType(\n this.value,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: T = this.value) {\n return new LiteralType(\n this.value,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new LiteralType(\n this.value,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(...examples: [T, ...T[]]) {\n return new LiteralType(this.value, ...this._with({ options: { examples } }))\n }\n}\n"],"names":["Type","BaseType","LiteralType","constructor","value","options","isNullable","isOptional","hasDefault","_constructSchema","Literal","nullable","optional","_with","nullish","default","description","examples"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAQpC,OAAO,MAAMC,oBAKHD;;IACRE,YACE,AAAmBC,KAAQ,EAC3BC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANhCA,QAAAA;IAOrB;IAEUK,iBAAiBJ,OAAsB,EAAED,KAAQ,EAAe;QACxE,OAAOJ,KAAKU,OAAO,CAACN,OAAOC;IAC7B;IAEAM,WAAW;QACT,OAAO,IAAIT,YAAY,IAAI,CAACE,KAAK,EAAE,IAAI,CAACC,OAAO,EAAE,MAAM,IAAI,CAACE,UAAU;IACxE;IAEAK,WAAW;QACT,OAAO,IAAIV,YAAY,IAAI,CAACE,KAAK,KAAK,IAAI,CAACS,KAAK,CAAC;YAAEN,YAAY;QAAK;IACtE;IAEAO,UAAU;QACR,OAAO,IAAIZ,YACT,IAAI,CAACE,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAEP,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAQ,QAAQX,QAAW,IAAI,CAACA,KAAK,EAAE;QAC7B,OAAO,IAAIF,YACT,IAAI,CAACE,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEU,SAASX;YAAM;YAAGI,YAAY;QAAK;IAElE;IAEAQ,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAId,YACT,IAAI,CAACE,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEW;YAAY;QAAE;IAE7C;IAEAC,SAAS,GAAGA,QAAqB,EAAE;QACjC,OAAO,IAAIf,YAAY,IAAI,CAACE,KAAK,KAAK,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEY;YAAS;QAAE;IAC3E;AACF"}
|
package/dist/types/object.js
CHANGED
|
@@ -88,3 +88,53 @@ export class ObjectType extends BaseType {
|
|
|
88
88
|
return new EnumType(Object.keys(this.properties));
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
+
export class RecordType extends BaseType {
|
|
92
|
+
key;
|
|
93
|
+
element;
|
|
94
|
+
constructor(key, element, options = {}, isNullable = false, isOptional = false, hasDefault = false){
|
|
95
|
+
super(options, isNullable, isOptional, hasDefault, key, element);
|
|
96
|
+
this.key = key;
|
|
97
|
+
this.element = element;
|
|
98
|
+
}
|
|
99
|
+
_constructSchema(options, key, element) {
|
|
100
|
+
return Type.Record(getTypeSchema(key), getTypeSchema(element), options);
|
|
101
|
+
}
|
|
102
|
+
nullable() {
|
|
103
|
+
return new RecordType(this.key, this.element, ...this._with({
|
|
104
|
+
isNullable: true
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
optional() {
|
|
108
|
+
return new RecordType(this.key, this.element, ...this._with({
|
|
109
|
+
isOptional: true
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
nullish() {
|
|
113
|
+
return new RecordType(this.key, this.element, ...this._with({
|
|
114
|
+
isNullable: true,
|
|
115
|
+
isOptional: true
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
default(value) {
|
|
119
|
+
return new RecordType(this.key, this.element, ...this._with({
|
|
120
|
+
options: {
|
|
121
|
+
default: value
|
|
122
|
+
},
|
|
123
|
+
hasDefault: true
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
description(description) {
|
|
127
|
+
return new RecordType(this.key, this.element, ...this._with({
|
|
128
|
+
options: {
|
|
129
|
+
description
|
|
130
|
+
}
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
examples(...examples) {
|
|
134
|
+
return new RecordType(this.key, this.element, ...this._with({
|
|
135
|
+
options: {
|
|
136
|
+
examples
|
|
137
|
+
}
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
}
|
package/dist/types/object.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/object.ts"],"sourcesContent":["import {\n type ObjectOptions,\n type StaticEncode,\n type TObject,\n Type,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTupleString } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\nimport { EnumType } from './enum.ts'\n\nexport class ObjectType<\n T extends Record<string, BaseType> = Record<string, BaseType>,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,\n N,\n O,\n D\n> {\n constructor(\n protected readonly properties: T = {} as T,\n options: ObjectOptions = {},\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, properties)\n }\n\n protected _constructSchema(\n options: ObjectOptions,\n properties: T,\n ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {\n const schemaProperties = {} as {\n [K in keyof T]: T[K][typeStatic]['schema']\n }\n\n for (const [key, value] of Object.entries(properties)) {\n // @ts-expect-error\n schemaProperties[key] = getTypeSchema(value)\n }\n return Type.Object(schemaProperties, options)\n }\n\n nullable() {\n return new ObjectType(this.properties, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectType(this.properties, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectType(\n this.properties,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { examples } }),\n )\n }\n\n pick<P extends { [K in keyof T]?: true }>(pick: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => pick[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Pick<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n omit<P extends { [K in keyof T]?: true }>(omit: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => !omit[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Omit<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n extend<P extends Record<string, BaseType>>(properties: P) {\n const [_, ...args] = this._with()\n return new ObjectType({ ...this.properties, ...properties }, {}, ...args)\n }\n\n merge<T extends ObjectType>(object: T) {\n const [_, ...args] = this._with()\n return new ObjectType(\n { ...this.properties, ...object.properties },\n {},\n ...args,\n )\n }\n\n partial() {\n const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =\n {} as any\n for (const [key, value] of Object.entries(this.properties)) {\n // @ts-expect-error\n properties[key] = value.optional()\n }\n const [_, ...args] = this._with()\n return new ObjectType(properties, {}, ...args)\n }\n\n keyof(): EnumType<UnionToTupleString<keyof T>> {\n return new EnumType(Object.keys(this.properties) as any)\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","EnumType","ObjectType","constructor","properties","options","isNullable","isOptional","hasDefault","_constructSchema","schemaProperties","key","value","Object","entries","nullable","_with","optional","nullish","default","description","examples","pick","fromEntries","filter","_","args","omit","extend","merge","object","partial","keyof","keys"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAG1B,SAASC,QAAQ,EAAEC,aAAa,QAAQ,YAAW;AACnD,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,mBAKHH;;IAMRI,YACE,AAAmBC,aAAgB,CAAC,CAAM,EAC1CC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC,YAAYJ;aANhCA,aAAAA;IAOrB;IAEUK,iBACRJ,OAAsB,EACtBD,UAAa,EAC4C;QACzD,MAAMM,mBAAmB,CAAC;QAI1B,KAAK,MAAM,CAACC,KAAKC,MAAM,IAAIC,OAAOC,OAAO,CAACV,YAAa;YAErDM,gBAAgB,CAACC,IAAI,GAAGX,cAAcY;QACxC;QACA,OAAOd,KAAKe,MAAM,CAACH,kBAAkBL;IACvC;IAEAU,WAAW;QACT,OAAO,IAAIb,WAAW,IAAI,CAACE,UAAU,KAAK,IAAI,CAACY,KAAK,CAAC;YAAEV,YAAY;QAAK;IAC1E;IAEAW,WAAW;QACT,OAAO,IAAIf,WAAW,IAAI,CAACE,UAAU,KAAK,IAAI,CAACY,KAAK,CAAC;YAAET,YAAY;QAAK;IAC1E;IAEAW,UAAU;QACR,OAAO,IAAIhB,WACT,IAAI,CAACE,UAAU,KACZ,IAAI,CAACY,KAAK,CAAC;YAAEV,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAY,QAAQP,KAAkC,EAAE;QAC1C,OAAO,IAAIV,WACT,IAAI,CAACE,UAAU,KACZ,IAAI,CAACY,KAAK,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AId,WACT;YAAE,GAAG,IAAI,CAACE,UAAU;YAAE,GAAG0B,OAAO1B,UAAU;QAAC,GAC3C,CAAC,MACEsB;IAEP;IAEAK,UAAU;QACR,MAAM3B,aACJ,CAAC;QACH,KAAK,MAAM,CAACO,KAAKC,MAAM,IAAIC,OAAOC,OAAO,CAAC,IAAI,CAACV,UAAU,EAAG;YAE1DA,UAAU,CAACO,IAAI,GAAGC,MAAMK,QAAQ;QAClC;QACA,MAAM,CAACQ,GAAG,GAAGC,KAAK,GAAG,IAAI,CAACV,KAAK;QAC/B,OAAO,IAAId,WAAWE,YAAY,CAAC,MAAMsB;IAC3C;IAEAM,QAA+C;QAC7C,OAAO,IAAI/B,SAASY,OAAOoB,IAAI,CAAC,IAAI,CAAC7B,UAAU;IACjD;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/types/object.ts"],"sourcesContent":["import {\n type ObjectOptions,\n type TObject,\n type TRecord,\n Type,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTupleString } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\nimport { type AnyEnumType, type AnyNativeEnumType, EnumType } from './enum.ts'\nimport type { AnyLiteralType } from './literal.ts'\nimport type { AnyStringType } from './string.ts'\nimport type { AnyUnionType } from './union.ts'\n\nexport class ObjectType<\n T extends Record<string, BaseType>,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }>,\n N,\n O,\n D\n> {\n constructor(\n protected readonly properties: T = {} as T,\n options: ObjectOptions = {},\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, properties)\n }\n\n protected _constructSchema(\n options: ObjectOptions,\n properties: T,\n ): TObject<{ [K in keyof T]: T[K][typeStatic]['schema'] }> {\n const schemaProperties = {} as {\n [K in keyof T]: T[K][typeStatic]['schema']\n }\n\n for (const [key, value] of Object.entries(properties)) {\n // @ts-expect-error\n schemaProperties[key] = getTypeSchema(value)\n }\n return Type.Object(schemaProperties, options)\n }\n\n nullable() {\n return new ObjectType(this.properties, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new ObjectType(this.properties, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new ObjectType(\n this.properties,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new ObjectType(\n this.properties,\n ...this._with({ options: { examples } }),\n )\n }\n\n pick<P extends { [K in keyof T]?: true }>(pick: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => pick[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Pick<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n omit<P extends { [K in keyof T]?: true }>(omit: P) {\n const properties = Object.fromEntries(\n Object.entries(this.properties).filter(([key]) => !omit[key]),\n )\n const [_, ...args] = this._with()\n return new ObjectType(\n properties as Omit<T, Extract<keyof P, keyof T>>,\n {},\n ...args,\n )\n }\n\n extend<P extends Record<string, BaseType>>(properties: P) {\n const [_, ...args] = this._with()\n return new ObjectType({ ...this.properties, ...properties }, {}, ...args)\n }\n\n merge<T extends ObjectType<Record<string, BaseType>>>(object: T) {\n const [_, ...args] = this._with()\n return new ObjectType(\n { ...this.properties, ...object.properties },\n {},\n ...args,\n )\n }\n\n partial() {\n const properties: { [K in keyof T]: ReturnType<T[K]['optional']> } =\n {} as any\n for (const [key, value] of Object.entries(this.properties)) {\n // @ts-expect-error\n properties[key] = value.optional()\n }\n const [_, ...args] = this._with()\n return new ObjectType(properties, {}, ...args)\n }\n\n keyof(): EnumType<UnionToTupleString<keyof T>> {\n return new EnumType(Object.keys(this.properties) as any)\n }\n}\n\nexport class RecordType<\n K extends\n | AnyLiteralType\n | AnyEnumType\n | AnyNativeEnumType\n | AnyStringType\n | AnyUnionType,\n E extends BaseType,\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>,\n N,\n O,\n D,\n ObjectOptions\n> {\n constructor(\n protected readonly key: K,\n protected readonly element: E,\n options: ObjectOptions = {},\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, key, element)\n }\n\n protected _constructSchema(options: ObjectOptions, key: K, element: E) {\n return Type.Record(\n getTypeSchema(key),\n getTypeSchema(element),\n options,\n ) as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>\n }\n\n nullable() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true }),\n )\n }\n\n optional() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isOptional: true }),\n )\n }\n\n nullish() {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new RecordType(\n this.key,\n this.element,\n ...this._with({ options: { examples } }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","EnumType","ObjectType","constructor","properties","options","isNullable","isOptional","hasDefault","_constructSchema","schemaProperties","key","value","Object","entries","nullable","_with","optional","nullish","default","description","examples","pick","fromEntries","filter","_","args","omit","extend","merge","object","partial","keyof","keys","RecordType","element","Record"],"mappings":"AAAA,SAIEA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AAEX,SAAS;gBAAEc,SAASP;YAAM;YAAGJ,YAAY;QAAK;IAElE;IAEAY,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAIc,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEX,SAAS;gBAAEe;YAAY;QAAE;IAE7C;IAEAC,SACE,GAAGA,QAAyE,EAC5E;QACA,OAAO,IAAIa,WACT,IAAI,CAACvB,GAAG,EACR,IAAI,CAACwB,OAAO,KACT,IAAI,CAACnB,KAAK,CAAC;YAAEX,SAAS;gBAAEgB;YAAS;QAAE;IAE1C;AACF"}
|
package/dist/types/string.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/string.ts"],"sourcesContent":["import { type StringOptions, type TString, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class StringType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TString, 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): TString {\n return Type.String(options)\n }\n\n nullable() {\n return new StringType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new StringType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new StringType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: string) {\n return new StringType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new StringType(...this._with({ options: { description } }))\n }\n\n examples(...examples: string[]) {\n return new StringType(...this._with({ options: { examples } }))\n }\n\n format(format: TString['format']) {\n return new StringType(...this._with({ options: { format } }))\n }\n\n max(value: number) {\n return new StringType(\n ...this._with({\n options: { maxLength: value },\n }),\n )\n }\n\n min(value: number) {\n return new StringType(\n ...this._with({\n options: { minLength: value },\n }),\n )\n }\n\n pattern(pattern: string) {\n return new StringType(\n ...this._with({\n options: { pattern },\n }),\n )\n }\n\n email() {\n return this.format('email')\n }\n\n url() {\n return this.format('uri')\n }\n\n ipv4() {\n return this.format('ipv4')\n }\n\n ipv6() {\n return this.format('ipv6')\n }\n\n uuid() {\n return this.format('uuid')\n }\n}\n"],"names":["Type","BaseType","StringType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","String","nullable","_with","optional","nullish","default","value","description","examples","format","max","maxLength","min","minLength","pattern","email","url","ipv4","ipv6","uuid"],"mappings":"AAAA,SAA2CA,IAAI,QAAQ,oBAAmB;AAC1E,SAASC,QAAQ,QAAQ,YAAW;
|
|
1
|
+
{"version":3,"sources":["../../../src/types/string.ts"],"sourcesContent":["import { type StringOptions, type TString, Type } from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport type AnyStringType = StringType<boolean, boolean, boolean>\nexport class StringType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TString, 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): TString {\n return Type.String(options)\n }\n\n nullable() {\n return new StringType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new StringType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new StringType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: string) {\n return new StringType(\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new StringType(...this._with({ options: { description } }))\n }\n\n examples(...examples: string[]) {\n return new StringType(...this._with({ options: { examples } }))\n }\n\n format(format: TString['format']) {\n return new StringType(...this._with({ options: { format } }))\n }\n\n max(value: number) {\n return new StringType(\n ...this._with({\n options: { maxLength: value },\n }),\n )\n }\n\n min(value: number) {\n return new StringType(\n ...this._with({\n options: { minLength: value },\n }),\n )\n }\n\n pattern(pattern: string) {\n return new StringType(\n ...this._with({\n options: { pattern },\n }),\n )\n }\n\n email() {\n return this.format('email')\n }\n\n url() {\n return this.format('uri')\n }\n\n ipv4() {\n return this.format('ipv4')\n }\n\n ipv6() {\n return this.format('ipv6')\n }\n\n uuid() {\n return this.format('uuid')\n }\n}\n"],"names":["Type","BaseType","StringType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","String","nullable","_with","optional","nullish","default","value","description","examples","format","max","maxLength","min","minLength","pattern","email","url","ipv4","ipv6","uuid"],"mappings":"AAAA,SAA2CA,IAAI,QAAQ,oBAAmB;AAC1E,SAASC,QAAQ,QAAQ,YAAW;AAGpC,OAAO,MAAMC,mBAIHD;IACRE,YACEC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;IACzC;IAEUC,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,QAAkB,EAAE;QAC9B,OAAO,IAAIf,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEa;YAAS;QAAE;IAC9D;IAEAC,OAAOA,MAAyB,EAAE;QAChC,OAAO,IAAIhB,cAAc,IAAI,CAACS,KAAK,CAAC;YAAEP,SAAS;gBAAEc;YAAO;QAAE;IAC5D;IAEAC,IAAIJ,KAAa,EAAE;QACjB,OAAO,IAAIb,cACN,IAAI,CAACS,KAAK,CAAC;YACZP,SAAS;gBAAEgB,WAAWL;YAAM;QAC9B;IAEJ;IAEAM,IAAIN,KAAa,EAAE;QACjB,OAAO,IAAIb,cACN,IAAI,CAACS,KAAK,CAAC;YACZP,SAAS;gBAAEkB,WAAWP;YAAM;QAC9B;IAEJ;IAEAQ,QAAQA,OAAe,EAAE;QACvB,OAAO,IAAIrB,cACN,IAAI,CAACS,KAAK,CAAC;YACZP,SAAS;gBAAEmB;YAAQ;QACrB;IAEJ;IAEAC,QAAQ;QACN,OAAO,IAAI,CAACN,MAAM,CAAC;IACrB;IAEAO,MAAM;QACJ,OAAO,IAAI,CAACP,MAAM,CAAC;IACrB;IAEAQ,OAAO;QACL,OAAO,IAAI,CAACR,MAAM,CAAC;IACrB;IAEAS,OAAO;QACL,OAAO,IAAI,CAACT,MAAM,CAAC;IACrB;IAEAU,OAAO;QACL,OAAO,IAAI,CAACV,MAAM,CAAC;IACrB;AACF"}
|
package/dist/types/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/union.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TIntersect,\n type TUnion,\n Type,\n
|
|
1
|
+
{"version":3,"sources":["../../../src/types/union.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type TIntersect,\n type TUnion,\n Type,\n} from '@sinclair/typebox'\nimport type { typeStatic } from '../constants.ts'\nimport type { UnionToTuple } from '../utils.ts'\nimport { BaseType, getTypeSchema } from './base.ts'\n\nexport type AnyUnionType<\n T extends [BaseType, BaseType, ...BaseType[]] = [\n BaseType,\n BaseType,\n ...BaseType[],\n ],\n> = UnionType<T, boolean, boolean, boolean>\nexport class UnionType<\n T extends [BaseType, BaseType, ...BaseType[]] = [\n BaseType,\n BaseType,\n ...BaseType[],\n ],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n //@ts-expect-error\n TUnion<UnionToTuple<T[number][typeStatic]['schema']>>,\n N,\n O,\n D\n> {\n constructor(\n readonly types: T,\n options: SchemaOptions = {},\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(\n options: SchemaOptions,\n //@ts-expect-error\n ): TUnion<UnionToTuple<T[number][typeStatic]['schema']>> {\n return Type.Union(this.types.map(getTypeSchema), options) as any\n }\n\n nullable() {\n return new UnionType(this.types, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new UnionType(this.types, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new UnionType(\n this.types,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new UnionType(\n this.types,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new UnionType(\n this.types,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new UnionType(this.types, ...this._with({ options: { examples } }))\n }\n}\n\nexport class IntersactionType<\n T extends [BaseType, BaseType, ...BaseType[]] = [\n BaseType,\n BaseType,\n ...BaseType[],\n ],\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<\n // @ts-expect-error\n TIntersect<UnionToTuple<T[number][typeStatic]['schema']>>,\n N,\n O,\n D\n> {\n constructor(\n readonly types: T,\n options: SchemaOptions = {},\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(\n options: SchemaOptions,\n // @ts-expect-error\n ): TIntersect<UnionToTuple<T[number][typeStatic]['schema']>> {\n return Type.Intersect(this.types.map(getTypeSchema), options) as any\n }\n\n nullable() {\n return new IntersactionType(this.types, ...this._with({ isNullable: true }))\n }\n\n optional() {\n return new IntersactionType(this.types, ...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new IntersactionType(\n this.types,\n ...this._with({ isNullable: true, isOptional: true }),\n )\n }\n\n default(value: this[typeStatic]['encoded']) {\n return new IntersactionType(\n this.types,\n ...this._with({ options: { default: value }, hasDefault: true }),\n )\n }\n\n description(description: string) {\n return new IntersactionType(\n this.types,\n ...this._with({ options: { description } }),\n )\n }\n\n examples(\n ...values: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]\n ) {\n return new IntersactionType(\n this.types,\n ...this._with({ options: { examples: values } }),\n )\n }\n}\n"],"names":["Type","BaseType","getTypeSchema","UnionType","constructor","types","options","isNullable","isOptional","hasDefault","_constructSchema","Union","map","nullable","_with","optional","nullish","default","value","description","examples","IntersactionType","Intersect","values"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAG1B,SAASC,QAAQ,EAAEC,aAAa,QAAQ,YAAW;AASnD,OAAO,MAAMC,kBASHF;;IAORG,YACE,AAASC,KAAQ,EACjBC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;aAN9BJ,QAAAA;IAOX;IAEUK,iBACRJ,OAAsB,EAEiC;QACvD,OAAON,KAAKW,KAAK,CAAC,IAAI,CAACN,KAAK,CAACO,GAAG,CAACV,gBAAgBI;IACnD;IAEAO,WAAW;QACT,OAAO,IAAIV,UAAU,IAAI,CAACE,KAAK,KAAK,IAAI,CAACS,KAAK,CAAC;YAAEP,YAAY;QAAK;IACpE;IAEAQ,WAAW;QACT,OAAO,IAA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;QACR,OAAO,IAAIK,iBACT,IAAI,CAAChB,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAEP,YAAY;YAAMC,YAAY;QAAK;IAEvD;IAEAS,QAAQC,KAAkC,EAAE;QAC1C,OAAO,IAAIG,iBACT,IAAI,CAAChB,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEW,SAASC;YAAM;YAAGT,YAAY;QAAK;IAElE;IAEAU,YAAYA,WAAmB,EAAE;QAC/B,OAAO,IAAIE,iBACT,IAAI,CAAChB,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEa;YAAY;QAAE;IAE7C;IAEAC,SACE,GAAGG,MAAuE,EAC1E;QACA,OAAO,IAAIF,iBACT,IAAI,CAAChB,KAAK,KACP,IAAI,CAACS,KAAK,CAAC;YAAER,SAAS;gBAAEc,UAAUG;YAAO;QAAE;IAElD;AACF"}
|
package/package.json
CHANGED
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
"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.4"
|
|
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.4"
|
|
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.4",
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
41
41
|
"type-check": "tsc --noEmit"
|
package/src/index.ts
CHANGED
|
@@ -4,18 +4,28 @@ import type { BaseType } from './types/base.ts'
|
|
|
4
4
|
import { BooleanType } from './types/boolean.ts'
|
|
5
5
|
import { CustomType } from './types/custom.ts'
|
|
6
6
|
import { DateType } from './types/datetime.ts'
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
type AnyEnumType,
|
|
9
|
+
type AnyNativeEnumType,
|
|
10
|
+
EnumType,
|
|
11
|
+
NativeEnumType,
|
|
12
|
+
} from './types/enum.ts'
|
|
13
|
+
import { type AnyLiteralType, LiteralType } from './types/literal.ts'
|
|
9
14
|
import { BigIntType, IntegerType, NumberType } from './types/number.ts'
|
|
10
|
-
import { ObjectType } from './types/object.ts'
|
|
11
|
-
import { StringType } from './types/string.ts'
|
|
12
|
-
import {
|
|
15
|
+
import { ObjectType, RecordType } from './types/object.ts'
|
|
16
|
+
import { type AnyStringType, StringType } from './types/string.ts'
|
|
17
|
+
import {
|
|
18
|
+
type AnyUnionType,
|
|
19
|
+
IntersactionType,
|
|
20
|
+
UnionType,
|
|
21
|
+
} from './types/union.ts'
|
|
13
22
|
|
|
14
23
|
import type { typeStatic } from './constants.ts'
|
|
15
|
-
// register ajv formats
|
|
16
|
-
import { register } from './formats.ts'
|
|
17
24
|
import { AnyType } from './types/any.ts'
|
|
18
25
|
import { NeverType } from './types/never.ts'
|
|
26
|
+
|
|
27
|
+
// register ajv formats
|
|
28
|
+
import { register } from './formats.ts'
|
|
19
29
|
register()
|
|
20
30
|
|
|
21
31
|
export * from './schemas/native-enum.ts'
|
|
@@ -65,6 +75,18 @@ export namespace t {
|
|
|
65
75
|
new ArrayType(element)
|
|
66
76
|
export const object = <T extends Record<string, BaseType>>(properties: T) =>
|
|
67
77
|
new ObjectType(properties)
|
|
78
|
+
export const record = <
|
|
79
|
+
K extends
|
|
80
|
+
| AnyLiteralType
|
|
81
|
+
| AnyEnumType
|
|
82
|
+
| AnyNativeEnumType
|
|
83
|
+
| AnyStringType
|
|
84
|
+
| AnyUnionType,
|
|
85
|
+
E extends BaseType,
|
|
86
|
+
>(
|
|
87
|
+
key: K,
|
|
88
|
+
value: E,
|
|
89
|
+
) => new RecordType(key, value)
|
|
68
90
|
export const any = () => new AnyType()
|
|
69
91
|
export const or = <T extends [BaseType, BaseType, ...BaseType[]]>(
|
|
70
92
|
...types: T
|
package/src/types/enum.ts
CHANGED
|
@@ -4,8 +4,10 @@ import { NativeEnum } from '../schemas/native-enum.ts'
|
|
|
4
4
|
import { type TUnionEnum, UnionEnum } from '../schemas/union-enum.ts'
|
|
5
5
|
import { BaseType } from './base.ts'
|
|
6
6
|
|
|
7
|
+
export type AnyNativeEnumType<T extends { [K in string]: K } = any> =
|
|
8
|
+
NativeEnumType<T, boolean, boolean, boolean>
|
|
7
9
|
export class NativeEnumType<
|
|
8
|
-
T extends { [K in string]: K }
|
|
10
|
+
T extends { [K in string]: K },
|
|
9
11
|
N extends boolean = false,
|
|
10
12
|
O extends boolean = false,
|
|
11
13
|
D extends boolean = false,
|
|
@@ -61,8 +63,9 @@ export class NativeEnumType<
|
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
|
|
66
|
+
export type AnyEnumType = EnumType<any[], boolean, boolean, boolean>
|
|
64
67
|
export class EnumType<
|
|
65
|
-
T extends (string | number)[],
|
|
68
|
+
T extends (string | number)[] = (string | number)[],
|
|
66
69
|
N extends boolean = false,
|
|
67
70
|
O extends boolean = false,
|
|
68
71
|
D extends boolean = false,
|
package/src/types/literal.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
Extends,
|
|
2
3
|
type SchemaOptions,
|
|
3
4
|
type TLiteral,
|
|
4
5
|
type TLiteralValue,
|
|
@@ -6,8 +7,14 @@ import {
|
|
|
6
7
|
} from '@sinclair/typebox'
|
|
7
8
|
import { BaseType } from './base.ts'
|
|
8
9
|
|
|
10
|
+
export type AnyLiteralType<T extends TLiteralValue = any> = LiteralType<
|
|
11
|
+
T,
|
|
12
|
+
boolean,
|
|
13
|
+
boolean,
|
|
14
|
+
boolean
|
|
15
|
+
>
|
|
9
16
|
export class LiteralType<
|
|
10
|
-
T extends TLiteralValue
|
|
17
|
+
T extends TLiteralValue,
|
|
11
18
|
N extends boolean = false,
|
|
12
19
|
O extends boolean = false,
|
|
13
20
|
D extends boolean = false,
|
package/src/types/object.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type ObjectOptions,
|
|
3
|
-
type StaticEncode,
|
|
4
3
|
type TObject,
|
|
4
|
+
type TRecord,
|
|
5
5
|
Type,
|
|
6
6
|
} from '@sinclair/typebox'
|
|
7
7
|
import type { typeStatic } from '../constants.ts'
|
|
8
8
|
import type { UnionToTupleString } from '../utils.ts'
|
|
9
9
|
import { BaseType, getTypeSchema } from './base.ts'
|
|
10
|
-
import { EnumType } from './enum.ts'
|
|
10
|
+
import { type AnyEnumType, type AnyNativeEnumType, EnumType } from './enum.ts'
|
|
11
|
+
import type { AnyLiteralType } from './literal.ts'
|
|
12
|
+
import type { AnyStringType } from './string.ts'
|
|
13
|
+
import type { AnyUnionType } from './union.ts'
|
|
11
14
|
|
|
12
15
|
export class ObjectType<
|
|
13
|
-
T extends Record<string, BaseType
|
|
16
|
+
T extends Record<string, BaseType>,
|
|
14
17
|
N extends boolean = false,
|
|
15
18
|
O extends boolean = false,
|
|
16
19
|
D extends boolean = false,
|
|
@@ -112,7 +115,7 @@ export class ObjectType<
|
|
|
112
115
|
return new ObjectType({ ...this.properties, ...properties }, {}, ...args)
|
|
113
116
|
}
|
|
114
117
|
|
|
115
|
-
merge<T extends ObjectType
|
|
118
|
+
merge<T extends ObjectType<Record<string, BaseType>>>(object: T) {
|
|
116
119
|
const [_, ...args] = this._with()
|
|
117
120
|
return new ObjectType(
|
|
118
121
|
{ ...this.properties, ...object.properties },
|
|
@@ -136,3 +139,91 @@ export class ObjectType<
|
|
|
136
139
|
return new EnumType(Object.keys(this.properties) as any)
|
|
137
140
|
}
|
|
138
141
|
}
|
|
142
|
+
|
|
143
|
+
export class RecordType<
|
|
144
|
+
K extends
|
|
145
|
+
| AnyLiteralType
|
|
146
|
+
| AnyEnumType
|
|
147
|
+
| AnyNativeEnumType
|
|
148
|
+
| AnyStringType
|
|
149
|
+
| AnyUnionType,
|
|
150
|
+
E extends BaseType,
|
|
151
|
+
N extends boolean = false,
|
|
152
|
+
O extends boolean = false,
|
|
153
|
+
D extends boolean = false,
|
|
154
|
+
> extends BaseType<
|
|
155
|
+
TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>,
|
|
156
|
+
N,
|
|
157
|
+
O,
|
|
158
|
+
D,
|
|
159
|
+
ObjectOptions
|
|
160
|
+
> {
|
|
161
|
+
constructor(
|
|
162
|
+
protected readonly key: K,
|
|
163
|
+
protected readonly element: E,
|
|
164
|
+
options: ObjectOptions = {},
|
|
165
|
+
isNullable: N = false as N,
|
|
166
|
+
isOptional: O = false as O,
|
|
167
|
+
hasDefault: D = false as D,
|
|
168
|
+
) {
|
|
169
|
+
super(options, isNullable, isOptional, hasDefault, key, element)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
protected _constructSchema(options: ObjectOptions, key: K, element: E) {
|
|
173
|
+
return Type.Record(
|
|
174
|
+
getTypeSchema(key),
|
|
175
|
+
getTypeSchema(element),
|
|
176
|
+
options,
|
|
177
|
+
) as TRecord<K[typeStatic]['schema'], E[typeStatic]['schema']>
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
nullable() {
|
|
181
|
+
return new RecordType(
|
|
182
|
+
this.key,
|
|
183
|
+
this.element,
|
|
184
|
+
...this._with({ isNullable: true }),
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
optional() {
|
|
189
|
+
return new RecordType(
|
|
190
|
+
this.key,
|
|
191
|
+
this.element,
|
|
192
|
+
...this._with({ isOptional: true }),
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
nullish() {
|
|
197
|
+
return new RecordType(
|
|
198
|
+
this.key,
|
|
199
|
+
this.element,
|
|
200
|
+
...this._with({ isNullable: true, isOptional: true }),
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
default(value: this[typeStatic]['encoded']) {
|
|
205
|
+
return new RecordType(
|
|
206
|
+
this.key,
|
|
207
|
+
this.element,
|
|
208
|
+
...this._with({ options: { default: value }, hasDefault: true }),
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
description(description: string) {
|
|
213
|
+
return new RecordType(
|
|
214
|
+
this.key,
|
|
215
|
+
this.element,
|
|
216
|
+
...this._with({ options: { description } }),
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
examples(
|
|
221
|
+
...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
|
|
222
|
+
) {
|
|
223
|
+
return new RecordType(
|
|
224
|
+
this.key,
|
|
225
|
+
this.element,
|
|
226
|
+
...this._with({ options: { examples } }),
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
}
|
package/src/types/string.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type StringOptions, type TString, Type } from '@sinclair/typebox'
|
|
2
2
|
import { BaseType } from './base.ts'
|
|
3
3
|
|
|
4
|
+
export type AnyStringType = StringType<boolean, boolean, boolean>
|
|
4
5
|
export class StringType<
|
|
5
6
|
N extends boolean = false,
|
|
6
7
|
O extends boolean = false,
|
package/src/types/union.ts
CHANGED
|
@@ -3,12 +3,18 @@ import {
|
|
|
3
3
|
type TIntersect,
|
|
4
4
|
type TUnion,
|
|
5
5
|
Type,
|
|
6
|
-
// type UnionToTuple,
|
|
7
6
|
} from '@sinclair/typebox'
|
|
8
7
|
import type { typeStatic } from '../constants.ts'
|
|
9
8
|
import type { UnionToTuple } from '../utils.ts'
|
|
10
9
|
import { BaseType, getTypeSchema } from './base.ts'
|
|
11
10
|
|
|
11
|
+
export type AnyUnionType<
|
|
12
|
+
T extends [BaseType, BaseType, ...BaseType[]] = [
|
|
13
|
+
BaseType,
|
|
14
|
+
BaseType,
|
|
15
|
+
...BaseType[],
|
|
16
|
+
],
|
|
17
|
+
> = UnionType<T, boolean, boolean, boolean>
|
|
12
18
|
export class UnionType<
|
|
13
19
|
T extends [BaseType, BaseType, ...BaseType[]] = [
|
|
14
20
|
BaseType,
|