@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
115 lines (114 loc) • 3.43 kB
TypeScript
import { AliasFieldDef } from './AliasField';
import { ArrayFieldDef } from './ArrayField';
import { ObjectLike } from './IObjectLike';
import { MetaFieldDef } from './MetaFieldField';
import { PhoneFieldDef } from './PhoneField';
import { RecordFieldDef } from './RecordField';
import { UnknownFieldDef } from './UnknownField';
import { ObjectFieldInput } from './_parseFields';
export type CursorType = {
PK: string;
SK?: string | undefined;
after?: string | undefined;
fields?: string[] | undefined;
limit?: number | undefined;
prefix?: string | undefined;
sep?: string | undefined;
version?: string | undefined;
};
export type ListDefinitionObject = {
length?: number;
max?: number;
min?: number;
};
export type ListDefinition = ListDefinitionObject | boolean;
export type ListDefinitionTruthy = ListDefinitionObject | true;
export type FieldExampleFunction = () => string | Promise<string>;
export type FieldExample = FieldExampleFunction | string;
type _get<T, K> = K extends keyof T ? T[K] extends unknown ? T[K] : never : never;
export type ComputeFieldDefinition<T> = T extends unknown ? {
type: _get<T, 'type'>;
__infer: _get<T, '__infer'>;
def: _get<T, 'def'>;
defaultValue: _get<T, 'defaultValue'>;
description: _get<T, 'description'>;
example: _get<T, 'example'>;
hidden: _get<T, 'hidden'>;
list: _get<T, 'list'>;
name: _get<T, 'name'>;
optional: _get<T, 'optional'>;
} : never;
export type CommonDefSafe = {
description?: string;
example?: FieldExample;
hidden?: boolean;
name?: string;
};
export type CommonFieldOptions = CommonDefSafe & {
def?: any;
defaultValue?: any;
list?: ListDefinition;
optional?: boolean;
};
export type CommonFieldDefinition<T> = {
type: T;
} & CommonFieldOptions;
export declare const SpecialObjectKeyEnum: {
readonly $string: "$string";
readonly $number: "$number";
} & {
list: ("$string" | "$number")[];
} & {
enum: "$string" | "$number";
};
export type SpecialObjectKeys = typeof SpecialObjectKeyEnum.enum;
export type FieldDefinitions = {
ID: {
autoCreate?: boolean;
} | undefined;
alias: AliasFieldDef;
any: undefined;
array: ArrayFieldDef;
boolean: undefined;
cursor: undefined;
date: {
autoCreate?: boolean;
max?: Date;
min?: Date;
} | undefined;
email: {
regex?: [string] | [string, string] | Readonly<[string] | [string, string]>;
} | undefined;
enum: Array<string> | Readonly<Array<string>>;
float: {
max?: number;
min?: number;
} | undefined;
int: {
max?: number;
min?: number;
} | undefined;
literal: Readonly<unknown>;
meta: MetaFieldDef;
null: undefined;
object: {
[K: string]: ObjectFieldInput;
} | Readonly<{
[K: string]: ObjectFieldInput;
}> | ObjectLike;
phone: PhoneFieldDef;
record: RecordFieldDef | undefined;
string: {
max?: number;
min?: number;
regex?: [string] | [string, string] | Readonly<[string] | [string, string]>;
} | undefined;
ulid: {
autoCreate?: boolean;
} | undefined;
undefined: undefined;
union: ObjectFieldInput[] | Readonly<ObjectFieldInput[]>;
unknown: UnknownFieldDef | undefined;
};
export type FieldTypeName = Extract<keyof FieldDefinitions, string>;
export {};