Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 40x 67x 51x 13x 1x 12x 1x 2131x 2x 12x 6539x 1x 1x 300x 4058x 2815x 75x 1008x 503x 17x 17x 17x 17x 4419x 4419x 2215x 2204x 58x 2146x 4984x 443x 62x 6840x 115x 178x 28x 15x | import type {Type} from '../type';
import type * as _ from './schema';
export class SchemaBuilder {
get str() {
return this.String();
}
get num() {
return this.Number();
}
get bool() {
return this.Boolean();
}
get undef() {
return this.Const<undefined>(undefined);
}
get nil() {
return this.Const<null>(null);
}
get arr() {
return this.Array(this.any);
}
get obj() {
return this.Object();
}
get map() {
return this.Map(this.any);
}
get bin() {
return this.Binary(this.any);
}
get any() {
return this.Any();
}
get fn() {
return this.Function(this.any, this.any);
}
get fn$() {
return this.Function$(this.any, this.any);
}
public Boolean(options?: _.NoT<_.BoolSchema>): _.BoolSchema {
return {...options, kind: 'bool'};
}
public Number(options?: _.NoT<_.NumSchema>): _.NumSchema {
return {...options, kind: 'num'};
}
public String(options?: _.NoT<_.StrSchema>): _.StrSchema {
return {...options, kind: 'str'};
}
public Binary<T extends _.Schema>(type: T, options: _.Optional<Omit<_.BinSchema, 'type'>> = {}): _.BinSchema<T> {
return {
...options,
kind: 'bin',
type,
};
}
public Array<T extends _.Schema>(type: T, options?: Omit<_.NoT<_.ArrSchema<T>>, 'type'>): _.ArrSchema<T, [], []> {
return {
...options,
kind: 'arr',
type,
};
}
/**
* Use TypeScript const when defining a constant value.
*
* @example
*
* ```ts
* s.Const('foo' as const);
* ```
*/
public Const<V>(
value: _.Narrow<V>,
options?: _.Optional<_.ConSchema<V>>,
): _.ConSchema<
string extends V ? never : number extends V ? never : boolean extends V ? never : any[] extends V ? never : V
> {
return {...options, kind: 'con', value: value as any};
}
public Tuple<const Head extends _.Schema[], T extends _.Schema = _.Schema, const Tail extends [] | _.Schema[] = []>(
head: Head,
type?: T,
tail?: Tail,
): _.ArrSchema<Type extends T ? _.Schema : T, Head, Tail> {
const schema: _.ArrSchema<T, Head, Tail> = {kind: 'arr', head};
if (type) schema.type = type;
if (tail) schema.tail = tail;
return schema;
}
public Object<F extends _.KeySchema<string, _.Schema>[] | readonly _.KeySchema<string, _.Schema>[]>(
options: _.NoT<_.ObjSchema<F>>,
): _.ObjSchema<F>;
public Object<F extends _.KeySchema<string, _.Schema>[] | readonly _.KeySchema<string, _.Schema>[]>(
keys: _.ObjSchema<F>['keys'],
options?: _.Optional<_.ObjSchema<F>>,
): _.ObjSchema<F>;
public Object<F extends _.KeySchema<string, _.Schema>[] | readonly _.KeySchema<string, _.Schema>[]>(
...keys: _.ObjSchema<F>['keys']
): _.ObjSchema<F>;
public Object<F extends _.KeySchema<string, _.Schema>[] | readonly _.KeySchema<string, _.Schema>[]>(
...args: unknown[]
): _.ObjSchema<F> {
const first = args[0];
if (
args.length === 1 &&
first &&
typeof first === 'object' &&
(first as _.NoT<_.ObjSchema<F>>).keys instanceof Array
)
return {kind: 'obj', ...(first as _.NoT<_.ObjSchema<F>>)};
if (args.length >= 1 && args[0] instanceof Array)
return this.Object({
keys: args[0] as F,
...(args[1] as _.Optional<_.ObjSchema<F>>),
});
return this.Object({keys: args as F});
}
/** Declares an object property. */
public Key<K extends string, V extends _.Schema>(
key: K,
value: V,
options: Omit<_.NoT<_.KeySchema<K, V>>, 'key' | 'value' | 'optional'> = {},
): _.KeySchema<K, V> {
return {
...options,
kind: 'key',
key,
value,
};
}
/** Declares an optional object property. */
public KeyOpt<K extends string, V extends _.Schema>(
key: K,
value: V,
options: Omit<_.NoT<_.KeySchema<K, V>>, 'key' | 'value' | 'optional'> = {},
): _.OptKeySchema<K, V> {
return {
...options,
kind: 'key',
key,
value,
optional: true,
};
}
public Map<V extends _.Schema, K extends _.Schema = _.StrSchema>(
value: V,
key?: K,
options?: Omit<_.NoT<_.MapSchema<V, K>>, 'value' | 'key'>,
): _.MapSchema<V, K> {
return {...(key && {key}), ...options, kind: 'map', value};
}
public Any(options: _.NoT<_.AnySchema> = {}): _.AnySchema {
return {
...options,
kind: 'any',
};
}
public Ref<T extends _.SchemaBase = any>(ref: string, options: Omit<_.NoT<_.RefSchema>, 'ref'> = {}): _.RefSchema<T> {
return {
...options,
kind: 'ref',
ref: ref as string & T,
};
}
public Or<T extends _.Schema[]>(...types: T): _.OrSchema<T> {
return {
kind: 'or',
types,
discriminator: ['num', -1],
};
}
public Function<Req extends _.Schema, Res extends _.Schema>(
req: Req,
res: Res,
options: Omit<_.NoT<_.FnSchema>, 'req' | 'res'> = {},
): _.FnSchema<Req, Res> {
return {
...options,
kind: 'fn',
req,
res,
};
}
public Function$<Req extends _.Schema, Res extends _.Schema>(
req: Req,
res: Res,
options: Omit<_.NoT<_.FnRxSchema>, 'req' | 'res'> = {},
): _.FnRxSchema<Req, Res> {
return {
...options,
kind: 'fn$',
req,
res,
};
}
}
|