-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshort.ts
37 lines (33 loc) · 1.12 KB
/
short.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
import { Ast } from "./ast/ast.ts";
import { any, array, decorate, optional, union } from "./ast/utils.ts";
import { EstimateType } from "./ast/estimate_type.ts";
import { createValidate } from "./validator/create_validate.ts";
import { createSanitize } from "./validator/create_sanitize.ts";
import { Decorator } from "./decorator/decorator.ts";
import { d, PredefinedDecorators } from "./decorators.ts";
export type DecoratorFactory<T extends Ast> = (
d: PredefinedDecorators,
) => Decorator<EstimateType<T>> | Decorator<EstimateType<T>>[];
const helpers = {
any() {
return any();
},
union<T extends Ast>(types: T[]) {
return union(types);
},
array<T extends Ast>(of: T) {
return array(of);
},
decorate<T extends Ast>(of: T, by: DecoratorFactory<T>) {
return decorate(of, by(d) as Decorator<EstimateType<T>>);
},
optional<T extends Ast>(of: T) {
return optional(of);
},
};
export const v = Object.assign(function <T extends Ast>(ast: T) {
return createValidate<T>(ast);
}, helpers);
export const s = Object.assign(function <T extends Ast>(ast: T) {
return createSanitize<T>(ast);
}, helpers);