diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index c8d8269..8bd6bb8 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -38,30 +38,30 @@ declare global { prototype: Schema resolve: Resolve from(source?: X): From - extend(type: string, resolve: Resolve): void - any(): Schema - never(): Schema - const(value: T): Schema - string(): Schema - number(): Schema - natural(): Schema - percent(): Schema - boolean(): Schema + extend(type: keyof Roles, resolve: Resolve): void + any(): Schema + never(): Schema + const(value: T): Schema + string(): Schema + number(): Schema + natural(): Schema + percent(): Schema + boolean(): Schema date(): Schema regExp(flag?: string): Schema arrayBuffer(): Schema arrayBuffer(encoding: 'hex' | 'base64'): Schema - bitset(bits: Partial>): Schema - function(): Schema any> - is(constructor: string): Schema - is(constructor: Constructor): Schema - array(inner: X): Schema[], TypeT[]> - dict = Schema>(inner: X, sKey?: Y): Schema, TypeS>, Dict, TypeT>> - tuple(list: X): Schema, TupleT> - object(dict: X): Schema, ObjectT> - union(list: readonly X[]): Schema, TypeT> - intersect(list: readonly X[]): Schema, IntersectT> - transform(inner: X, callback: (value: TypeS, options: Schemastery.Options) => T, preserve?: boolean): Schema, T> + bitset(bits: Partial>): Schema + function(): Schema any, 'function'> + is(constructor: string): Schema + is(constructor: Constructor): Schema + array(inner: X): Schema[], TypeT[], 'array'> + dict = Schema>(inner: X, sKey?: Y): Schema, TypeS>, Dict, TypeT>, 'dict'> + tuple(list: X): Schema, TupleT, 'tuple'> + object(dict: X): Schema, ObjectT, 'object'> + union(list: readonly X[]): Schema, TypeT, 'union'> + intersect(list: readonly X[]): Schema, IntersectT, 'intersect'> + transform(inner: X, callback: (value: TypeS, options: Schemastery.Options) => T, preserve?: boolean): Schema, T, 'transform'> lazy(callback: () => X): X ValidationError: typeof ValidationError } @@ -72,7 +72,27 @@ declare global { path?: (keyof any)[] } - export interface Meta { + export interface Roles { + lazy: never + any: never + never: never + const: never + string: 'datetime' | 'regexp' | 'textarea' + number: 'slider' + boolean: never + bitset: 'select' + function: never + is: never + array: 'select' | 'checkbox' | 'table' + dict: 'table' + tuple: never + object: never + union: 'radio' + intersect: never + transform: never + } + + export interface Meta { default?: T extends {} ? Partial : T required?: boolean disabled?: boolean @@ -80,7 +100,7 @@ declare global { badges?: { text: string; type: string }[] hidden?: boolean loose?: boolean - role?: string + role?: Type extends keyof Roles ? Roles[Type] : never extra?: any link?: string description?: string | Dict @@ -92,13 +112,13 @@ declare global { } } - interface Schemastery { + interface Schemastery { (data?: S | null, options?: Schemastery.Options): T - new (data?: S | null, options?: Schemastery.Options): T + new(data?: S | null, options?: Schemastery.Options): T [kSchema]: true uid: number - meta: Schemastery.Meta - type: string + meta: Schemastery.Meta + type: Type sKey?: Schema inner?: Schema list?: Schema[] @@ -112,28 +132,28 @@ declare global { preserve?: boolean '~standard': StandardSchemaV1.Props // toString(inline?: boolean): string - toJSON(): Schema - required(value?: boolean): Schema - hidden(value?: boolean): Schema - loose(value?: boolean): Schema - role(text: string, extra?: any): Schema - link(link: string): Schema - default(value: T): Schema - comment(text: string): Schema - description(text: string): Schema - disabled(value?: boolean): Schema - collapse(value?: boolean): Schema - deprecated(): Schema - experimental(): Schema - pattern(regexp: RegExp): Schema - max(value: number): Schema - min(value: number): Schema - step(value: number): Schema - set(key: string, value: Schema): Schema - push(value: Schema): Schema + toJSON(): Schema + required(value?: boolean): Schema + hidden(value?: boolean): Schema + loose(value?: boolean): Schema + role(role: this['meta']['role'], extra?: any): Schema + link(link: string): Schema + default(value: T): Schema + comment(text: string): Schema + description(text: string): Schema + disabled(value?: boolean): Schema + collapse(value?: boolean): Schema + deprecated(): Schema + experimental(): Schema + pattern(regexp: RegExp): Schema + max(value: number): Schema + min(value: number): Schema + step(value: number): Schema + set(key: string, value: Schema): Schema + push(value: Schema): Schema simplify(value?: any): any - i18n(messages: Dict): Schema - extra(key: K, value: Schemastery.Meta[K]): Schema + i18n(messages: Dict): Schema + extra(key: K, value: Schemastery.Meta[K]): Schema } } @@ -173,7 +193,7 @@ Object.defineProperty(ValidationError.prototype, kValidationError, { value: true, }) -type Schema = Schemastery +type Schema = Schemastery const Schema = function (options: Schema) { const schema = function (data: any, options: Schemastery.Options = {}) { @@ -384,7 +404,7 @@ Schema.prototype.toString = function toString(inline?: boolean) { return formatters[this.type]?.(this, inline) ?? `Schema<${this.type}>` } -Schema.prototype.role = function role(role, extra) { +Schema.prototype.role = function role(this: This, role: This['meta']['role'], extra?: any) { const schema = Schema(this) schema.meta = { ...schema.meta, role, extra } return schema @@ -775,13 +795,13 @@ function defineMethod(name: string, keys: (keyof Schema)[], format: Formatter) { } case 'callback': { const callback = schema.callback = args[index] - ;callback['toJSON'] ||= () => callback.toString() + callback.toJSON ||= () => callback.toString() break } case 'constructor': { const constructor = schema.constructor = args[index] if (typeof constructor === 'function') { - ;constructor['toJSON'] ||= () => constructor['name'] + constructor.toJSON ||= () => constructor.name } break }