1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.makeStructuredValidator = exports.makeExactValidator = exports.makeValidator = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var internalMakeValidator = function (parseFn) {
|
6 | return function (spec) { return (tslib_1.__assign(tslib_1.__assign({}, spec), { _parse: parseFn })); };
|
7 | };
|
8 | /**
|
9 | * Creates a validator which can output subtypes of `BaseT`. E.g.:
|
10 | *
|
11 | * ```ts
|
12 | * const int = makeValidator<number>((input: string) => {
|
13 | * // Implementation details
|
14 | * })
|
15 | * const MAX_RETRIES = int({ choices: [1, 2, 3, 4] })
|
16 | * // Narrows down output type to 1 | 2 | 3 | 4
|
17 | * ```
|
18 | *
|
19 | * @param parseFn - A function to parse and validate input.
|
20 | * @returns A validator which output type is narrowed-down to a subtype of `BaseT`
|
21 | */
|
22 | var makeValidator = function (parseFn) {
|
23 | return internalMakeValidator(parseFn);
|
24 | };
|
25 | exports.makeValidator = makeValidator;
|
26 | /**
|
27 | * Creates a validator which output type is exactly T:
|
28 | *
|
29 | * ```ts
|
30 | * const int = makeExactValidator<number>((input: string) => {
|
31 | * // Implementation details
|
32 | * })
|
33 | * const MAX_RETRIES = int({ choices: [1, 2, 3, 4] })
|
34 | * // Output type 'number'
|
35 | * ```
|
36 | *
|
37 | * @param parseFn - A function to parse and validate input.
|
38 | * @returns A validator which output type is exactly `T`
|
39 | */
|
40 | var makeExactValidator = function (parseFn) {
|
41 | return internalMakeValidator(parseFn);
|
42 | };
|
43 | exports.makeExactValidator = makeExactValidator;
|
44 | /**
|
45 | * This validator is meant for inputs which can produce arbitrary output types (e.g. json).
|
46 | * The typing logic behaves differently from other makers:
|
47 | *
|
48 | * - makeStructuredValidator has no type parameter.
|
49 | * - When no types can be inferred from context, output type defaults to any.
|
50 | * - Otherwise, infers type from `default` or `devDefault`.
|
51 | * - Also generated validators have an output type parameter.
|
52 | * - Finally, the generated validators disallow `choices` parameter.
|
53 | *
|
54 | * Below is an example of a validator for query parameters (e.g. `option1=foo&option2=bar`):
|
55 | *
|
56 | * ```ts
|
57 | * const queryParams = makeStructuredValidator((input: string) => {
|
58 | * const params = new URLSearchParams(input)
|
59 | * return Object.fromEntries(params.entries())
|
60 | * })
|
61 | * const OPTIONS1 = queryParams()
|
62 | * // Output type 'any'
|
63 | * const OPTIONS2 = queryParams({ default: { option1: 'foo', option2: 'bar' } })
|
64 | * // Output type '{ option1: string, option2: string }'
|
65 | * const OPTIONS3 = queryParams<{ option1?: string; option2?: string }>({
|
66 | * default: { option1: 'foo', option2: 'bar' },
|
67 | * })
|
68 | * // Output type '{ option1?: string, option2?: string }'
|
69 | * ```
|
70 | *
|
71 | * @param parseFn - A function to parse and validate input.
|
72 | * @returns A validator which output type is exactly `T`
|
73 | */
|
74 | var makeStructuredValidator = function (parseFn) {
|
75 | return internalMakeValidator(parseFn);
|
76 | };
|
77 | exports.makeStructuredValidator = makeStructuredValidator;
|
78 | //# sourceMappingURL=makers.js.map |
\ | No newline at end of file |