-
Notifications
You must be signed in to change notification settings - Fork 7
/
schemable.ts
324 lines (301 loc) · 8.38 KB
/
schemable.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Schemable presents a unified algebra for parsing, decoding
* guarding, or otherwise building typed js structures in
* TypeScript.
*
* @module Schemable
* @since 2.0.0
*/
import type { $, Hold, Kind, Spread } from "./kind.ts";
import type { NonEmptyArray } from "./array.ts";
import { memoize } from "./fn.ts";
/**
* These are the super-types that a Literal schema must extent.
* They are used to constrain the inputs for LiteralSchemable.
*
* @since 2.0.0
*/
export type Literal = string | number | boolean | null | undefined;
/**
* Wraps an unknown type in Schemable. This is the best escape
* hatch when a Schema isn't known ahead of time.
*
* @since 2.0.0
*/
export interface UnknownSchemable<U extends Kind> extends Hold<U> {
readonly unknown: <B, C, D, E>() => $<U, [unknown, B, C], [D], [E]>;
}
/**
* Wraps a string type in Schemable.
*
* @since 2.0.0
*/
export interface StringSchemable<U extends Kind> extends Hold<U> {
readonly string: <B, C, D, E>() => $<U, [string, B, C], [D], [E]>;
}
/**
* Wraps a number type in Schemable.
*
* @since 2.0.0
*/
export interface NumberSchemable<U extends Kind> extends Hold<U> {
readonly number: <B, C, D, E>() => $<U, [number, B, C], [D], [E]>;
}
/**
* Wraps a boolean type in Schemable.
*
* @since 2.0.0
*/
export interface BooleanSchemable<U extends Kind> extends Hold<U> {
readonly boolean: <B, C, D, E>() => $<U, [boolean, B, C], [D], [E]>;
}
/**
* Wraps a union of literals in Schemable.
*
* @since 2.0.0
*/
export interface LiteralSchemable<U extends Kind> extends Hold<U> {
readonly literal: <A extends NonEmptyArray<Literal>, B, C, D, E>(
...s: A
) => $<U, [A[number], B, C], [D], [E]>;
}
/**
* Takes a Schemable<A> and returns a Schemable<A | null>;
*
* @since 2.0.0
*/
export interface NullableSchemable<U extends Kind> extends Hold<U> {
readonly nullable: <A, B, C, D, E>(
or: $<U, [A, B, C], [D], [E]>,
) => $<U, [A | null, B, C], [D], [E]>;
}
/**
* Takes a Schemable<A> and returns a Schemable<A | undefined>;
*
* @since 2.0.0
*/
export interface UndefinableSchemable<U extends Kind> extends Hold<U> {
readonly undefinable: <A, B, C, D, E>(
or: $<U, [A, B, C], [D], [E]>,
) => $<U, [A | undefined, B, C], [D], [E]>;
}
/**
* Takes a Schemable<A> and returns a Schemable<ReadonlyRecord<A>>
*
* @since 2.0.0
*/
export interface RecordSchemable<U extends Kind> extends Hold<U> {
readonly record: <A, B, C, D, E>(
codomain: $<U, [A, B, C], [D], [E]>,
) => $<U, [Record<string, A>, B, C], [D], [E]>;
}
/**
* Takes a Schemable<A> and returns a Schemable<ReadonlyArray<A>>
*
* @since 2.0.0
*/
export interface ArraySchemable<U extends Kind> extends Hold<U> {
readonly array: <A, B, C, D, E>(
item: $<U, [A, B, C], [D], [E]>,
) => $<U, [ReadonlyArray<A>, B, C], [D], [E]>;
}
/**
* Takes a tuple of Schemables and returns a Schemable
* for a tuple that matches, index for index, the input
* schemables.
*
* ie. [StringSchemable, NumberSchemable] becomes
* Schemable<[string, number]>
*
* @since 2.0.0
*/
export interface TupleSchemable<U extends Kind> extends Hold<U> {
// deno-lint-ignore no-explicit-any
readonly tuple: <A extends any[], B, C, D, E>(
...items: { readonly [K in keyof A]: $<U, [A[K], B, C], [D], [E]> }
) => $<U, [{ [K in keyof A]: A[K] }, B, C], [D], [E]>;
}
/**
* Takes a struct of Schemables and returns a Schemable
* for a struct that matches, key for key, the input
* schemables.
*
* ie. { str: StringSchemable, num: NumberSchemable } becomes
* Schemable<{ str: string, num: number }>
*
* @since 2.0.0
*/
export interface StructSchemable<U extends Kind> extends Hold<U> {
readonly struct: <A, B, C, D, E>(
items: { readonly [K in keyof A]: $<U, [A[K], B, C], [D], [E]> },
) => $<U, [{ readonly [K in keyof A]: A[K] }, B, C], [D], [E]>;
}
/**
* Takes a struct of Schemables and returns a Schemable
* for a struct that matches, key for key, the input
* schemables but the values can also be partial.
*
* ie. { str: StringSchemable, num: NumberSchemable } becomes
* Schemable<{ str?: string, num?: number }>
*
* @since 2.0.0
*/
export interface PartialSchemable<U extends Kind> extends Hold<U> {
readonly partial: <A, B, C, D, E>(
items: { readonly [K in keyof A]: $<U, [A[K], B, C], [D], [E]> },
) => $<U, [{ readonly [K in keyof A]?: A[K] }, B, C], [D], [E]>;
}
/**
* Takes two schemables, left and right, and returns
* the intersection of them. This means that any value
* for must match both schemables.
*
* @since 2.0.0
*/
export interface IntersectSchemable<U extends Kind> extends Hold<U> {
readonly intersect: <I, B, C, D, E>(
right: $<U, [I, B, C], [D], [E]>,
) => <A>(
left: $<U, [A, B, C], [D], [E]>,
) => $<U, [Spread<A & I>, B, C], [D], [E]>;
}
/**
* Takes two schemables, left and right, and returns
* the union of them. This means that any value
* for must match either schemable.
*
* @since 2.0.0
*/
export interface UnionSchemable<U extends Kind> extends Hold<U> {
readonly union: <I, B, C, D, E>(
right: $<U, [I, B, C], [D], [E]>,
) => <A>(left: $<U, [A, B, C], [D], [E]>) => $<U, [A | I, B, C], [D], [E]>;
}
/**
* Takes an id and a thunk returning a schemable and
* returns a schemable that matches the return value
* of the thunk. This schemable is necessary for
* handling recursive or corecursive schemables.
*
* @since 2.0.0
*/
export interface LazySchemable<U extends Kind> extends Hold<U> {
readonly lazy: <A, B, C, D, E>(
id: string,
f: () => $<U, [A, B, C], [D], [E]>,
) => $<U, [A, B, C], [D], [E]>;
}
/**
* A Schemable is the union of all schemable methods.
* This allows one to build an arbitrary Schema using
* the Schemable interface, then pass a concrete
* Schemable implementation to the Schema. Thus, one
* can build a single model and produce decoders,
* guards, or jsonschema from that model.
*
* @since 2.0.0
*/
export interface Schemable<U extends Kind>
extends
UnknownSchemable<U>,
StringSchemable<U>,
NumberSchemable<U>,
BooleanSchemable<U>,
LiteralSchemable<U>,
NullableSchemable<U>,
UndefinableSchemable<U>,
RecordSchemable<U>,
ArraySchemable<U>,
TupleSchemable<U>,
StructSchemable<U>,
PartialSchemable<U>,
IntersectSchemable<U>,
UnionSchemable<U>,
LazySchemable<U>,
Hold<U> {}
/**
* A Schema is the a function that takes a generic schemable and builds
* a specific model from it.
*
* @since 2.0.0
*/
export type Schema<A, B = unknown, C = unknown, D = unknown, E = unknown> = <
U extends Kind,
>(S: Schemable<U>) => $<U, [A, B, C], [D], [E]>;
/**
* Extracts the inner type of a Schema
*
* @since 2.0.0
*/
export type TypeOf<T> = T extends Schema<infer A> ? A : unknown;
// Helps inference in the schema function
type InferSchema<U extends Kind, A, B, C, D, E> = (
S: Schemable<U>,
) => $<U, [A, B, C], [D], [E]>;
/**
* A helper function to build a generic Schema that can be used
* with any Schemable.
*
* @example
* ```ts
* import { schema, TypeOf } from "./schemable.ts";
* import { SchemableDecoder } from "./decoder.ts";
* import { SchemableRefinement } from "./refinement.ts";
* import { SchemableJsonBuilder, print } from "./json_schema.ts";
* import { pipe } from "./fn.ts";
*
* const mySchema = schema(s => pipe(
* s.struct({
* name: s.string(),
* age: s.number(),
* }),
* s.intersect(s.partial({
* interests: s.array(s.string()),
* }))
* ));
*
* // Derive the type from the schema
* type MySchema = TypeOf<typeof mySchema>;
*
* const decode = mySchema(SchemableDecoder);
* const refine = mySchema(SchemableRefinement);
* const jsonSchema = mySchema(SchemableJsonBuilder);
*
* const unknown1 = {
* name: "Batman",
* age: 45,
* interests: ["crime fighting", "cake", "bats"],
* };
* const unknown2 = {
* name: "Cthulhu",
* interests: ["madness"],
* };
*
* const decoded1 = decode(unknown1); // Success!
* const decoded2 = decode(unknown2); // Failure with info
*
* const refine1 = refine(unknown1); // true
* const refine2 = refine(unknown2); // false
*
* const jsonSchemaString = pipe(
* jsonSchema,
* print,
* json => JSON.stringify(json, null, 2),
* ); // Turns the jsonSchema into a prettified string
*
* ```
*
* @since 2.0.0
*/
export function schema<
A,
B = unknown,
C = unknown,
D = unknown,
E = unknown,
U extends Kind = Kind,
>(
s: InferSchema<U, A, B, C, D, E>,
): Schema<A, B, C, D, E> {
return memoize(s) as Schema<A>;
}