forked from DZakh/rescript-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS.d.ts
355 lines (325 loc) · 9.69 KB
/
S.d.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { S_t, S_Error_class } from "../RescriptSchema.gen";
export class Error extends S_Error_class {
constructor(
code: unknown,
operation: "Parsing" | "Serializing",
path: string
);
code: unknown;
operation: "Parsing" | "Serializing";
path: string;
message: string;
reason: string;
}
export type Result<Value> =
| {
success: true;
value: Value;
}
| { success: false; error: Error };
export type EffectCtx<Output, Input> = {
schema: Schema<Output, Input>;
fail: (message: string) => void;
};
export type Schema<Output, Input = Output> = S_t<Output, Input>;
export type Output<T> = T extends Schema<infer Output, unknown>
? Output
: never;
export type Input<T> = T extends Schema<unknown, infer Input> ? Input : never;
export type Json =
| string
| boolean
| number
| null
| { [key: string]: Json }
| Json[];
type NoUndefined<T> = T extends undefined ? never : T;
type UnknownSchema = Schema<unknown, unknown>;
type SchemaTupleOutput<
Tuple extends UnknownSchema[],
Length extends number = Tuple["length"]
> = Length extends Length
? number extends Length
? Tuple
: _TupleOutput<Tuple, Length, []>
: never;
type _TupleOutput<
Tuple extends UnknownSchema[],
Length extends number,
Accumulated extends unknown[],
Index extends number = Accumulated["length"]
> = Index extends Length
? Accumulated
: _TupleOutput<Tuple, Length, [...Accumulated, Output<Tuple[Index]>]>;
type SchemaTupleInput<
Tuple extends UnknownSchema[],
Length extends number = Tuple["length"]
> = Length extends Length
? number extends Length
? Tuple
: _TupleInput<Tuple, Length, []>
: never;
type _TupleInput<
Tuple extends UnknownSchema[],
Length extends number,
Accumulated extends unknown[],
Index extends number = Accumulated["length"]
> = Index extends Length
? Accumulated
: _TupleInput<Tuple, Length, [...Accumulated, Input<Tuple[Index]>]>;
export const string: Schema<string>;
export const boolean: Schema<boolean>;
export const integer: Schema<number>;
export const number: Schema<number>;
export const never: Schema<never>;
export const unknown: Schema<unknown>;
export const json: Schema<Json>;
export const undefined: Schema<undefined>;
export function literal<Literal extends string>(
value: Literal
): Schema<Literal>;
export function literal<Literal extends number>(
value: Literal
): Schema<Literal>;
export function literal<Literal extends boolean>(
value: Literal
): Schema<Literal>;
export function literal<Literal extends symbol>(
value: Literal
): Schema<Literal>;
export function literal<Literal extends BigInt>(
value: Literal
): Schema<Literal>;
export function literal(value: undefined): Schema<undefined>;
export function literal(value: null): Schema<null>;
export function literal<T>(value: T): Schema<T>;
export function tuple(schemas: []): Schema<[]>;
export function tuple<Output, Input>(
schemas: [Schema<Output, Input>]
): Schema<[Output], [Input]>;
export function tuple<A extends UnknownSchema, B extends UnknownSchema[]>(
schemas: [A, ...B]
): Schema<
[Output<A>, ...SchemaTupleOutput<B>],
[Input<A>, ...SchemaTupleInput<B>]
>;
export function tuple<Output>(
definer: (ctx: {
item: <InputIndex extends number, ItemOutput>(
inputIndex: InputIndex,
schema: Schema<ItemOutput, unknown>
) => ItemOutput;
tag: (inputIndex: number, value: unknown) => void;
}) => Output
): Schema<Output, unknown>;
export function optional<Output, Input>(
schema: Schema<Output, Input>
): Schema<Output | undefined, Input | undefined>;
export function optional<Output, Input>(
schema: Schema<Output, Input>,
or: () => Output
): Schema<Output, Input | undefined>;
export function optional<Output, Input>(
schema: Schema<Output, Input>,
or: Output
): Schema<Output, Input | undefined>;
export const nullable: <Output, Input>(
schema: Schema<Output, Input>
) => Schema<Output | undefined, Input | null>;
export const nullish: <Output, Input>(
schema: Schema<Output, Input>
) => Schema<Output | undefined, Input | undefined | null>;
export const array: <Output, Input>(
schema: Schema<Output, Input>
) => Schema<Output[], Input[]>;
export const record: <Output, Input>(
schema: Schema<Output, Input>
) => Schema<Record<string, Output>, Record<string, Input>>;
export const jsonString: <Output>(
schema: Schema<Output, unknown>,
space?: number
) => Schema<Output, string>;
export const union: <A extends UnknownSchema, B extends UnknownSchema[]>(
schemas: [A, ...B]
) => Schema<
Output<A> | SchemaTupleOutput<B>[number],
Input<A> | SchemaTupleInput<B>[number]
>;
export function schema<Value>(
definer: (ctx: {
matches: <Output>(schema: Schema<Output, unknown>) => Output;
}) => Value
): Schema<Value, unknown>;
export function object<Output>(
definer: (ctx: {
field: <InputFieldName extends string, FieldOutput>(
inputFieldName: InputFieldName,
schema: Schema<FieldOutput, unknown>
) => FieldOutput;
fieldOr: <InputFieldName extends string, FieldOutput>(
name: InputFieldName,
schema: Schema<FieldOutput, unknown>,
or: FieldOutput
) => FieldOutput;
tag: (name: string, value: unknown) => void;
}) => Output
): Schema<Output, unknown>;
export function object<
Shape extends {
[k in keyof Shape]: Schema<unknown, unknown>;
}
>(
shape: Shape
): Schema<
{
[k in keyof Shape]: Output<Shape[k]>;
},
{
[k in keyof Shape]: Input<Shape[k]>;
}
>;
export const Object: {
strip: <Output, Input>(
schema: Schema<Output, Input>
) => Schema<Output, Input>;
strict: <Output, Input>(
schema: Schema<Output, Input>
) => Schema<Output, Input>;
};
export function merge<O1, O2>(
schema1: Schema<O1, Record<string, unknown>>,
schema2: Schema<O2, Record<string, unknown>>
): Schema<O1 & O2, Record<string, unknown>>;
export const String: {
min: <Input>(
schema: Schema<string, Input>,
length: number,
message?: string
) => Schema<string, Input>;
max: <Input>(
schema: Schema<string, Input>,
length: number,
message?: string
) => Schema<string, Input>;
length: <Input>(
schema: Schema<string, Input>,
length: number,
message?: string
) => Schema<string, Input>;
email: <Input>(
schema: Schema<string, Input>,
message?: string
) => Schema<string, Input>;
uuid: <Input>(
schema: Schema<string, Input>,
message?: string
) => Schema<string, Input>;
cuid: <Input>(
schema: Schema<string, Input>,
message?: string
) => Schema<string, Input>;
url: <Input>(
schema: Schema<string, Input>,
message?: string
) => Schema<string, Input>;
pattern: <Input>(
schema: Schema<string, Input>,
re: RegExp,
message?: string
) => Schema<string, Input>;
datetime: <Input>(
schema: Schema<string, Input>,
message?: string
) => Schema<Date, Input>;
trim: <Input>(schema: Schema<string, Input>) => Schema<string, Input>;
};
export const Number: {
min: <Input>(
schema: Schema<number, Input>,
value: number,
message?: string
) => Schema<number, Input>;
max: <Input>(
schema: Schema<number, Input>,
value: number,
message?: string
) => Schema<number, Input>;
};
export const Array: {
min: <Input, ItemSchema>(
schema: Schema<ItemSchema[], Input>,
length: number,
message?: string
) => Schema<ItemSchema[], Input>;
max: <Input, ItemSchema>(
schema: Schema<ItemSchema[], Input>,
length: number,
message?: string
) => Schema<ItemSchema[], Input>;
length: <Input, ItemSchema>(
schema: Schema<ItemSchema[], Input>,
length: number,
message?: string
) => Schema<ItemSchema[], Input>;
};
export function custom<Output, Input = unknown>(
name: string,
parser: (data: unknown, s: EffectCtx<unknown, unknown>) => Output
): Schema<Output, Input>;
export function custom<Output, Input = unknown>(
name: string,
parser: (data: unknown, s: EffectCtx<unknown, unknown>) => Output | undefined,
serializer: (value: Output, s: EffectCtx<unknown, unknown>) => Input
): Schema<Output, Input>;
export function name(schema: Schema<unknown, unknown>): string;
export function setName<Output, Input>(
schema: Schema<Output, Input>,
name: string
): Schema<Output, Input>;
export function asyncParserRefine<Output, Input>(
schema: Schema<Output, Input>,
refiner: (value: Output, s: EffectCtx<Output, Input>) => Promise<void>
): Schema<Output, Input>;
export function refine<Output, Input>(
schema: Schema<Output, Input>,
refiner: (value: Output, s: EffectCtx<Output, Input>) => void
): Schema<Output, Input>;
export function transform<Output, Input, Transformed>(
schema: Schema<Output, Input>,
parser: (value: Output, s: EffectCtx<unknown, unknown>) => Transformed
): Schema<Transformed, Input>;
export function transform<Output, Input, Transformed>(
schema: Schema<Output, Input>,
parser: (
value: Output,
s: EffectCtx<unknown, unknown>
) => Transformed | undefined,
serializer: (value: Transformed, s: EffectCtx<unknown, unknown>) => Input
): Schema<Transformed, Input>;
export function describe<Output, Input>(
schema: Schema<Output, Input>,
description: string
): Schema<Output, Input>;
export function description<Output, Input>(
schema: Schema<Output, Input>
): string | undefined;
export function parse<Output, Input>(
schema: Schema<Output, Input>,
data: unknown
): Result<Output>;
export function parseOrThrow<Output, Input>(
schema: Schema<Output, Input>,
data: unknown
): Output;
export function parseAsync<Output, Input>(
schema: Schema<Output, Input>,
data: unknown
): Promise<Result<Output>>;
export function serialize<Output, Input>(
schema: Schema<Output, Input>,
data: Output
): Result<Input>;
export function serializeOrThrow<Output, Input>(
schema: Schema<Output, Input>,
data: Output
): Input;