better-call 0.0.0-experimental.01ff4b77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +265 -0
- package/dist/capability.cjs +417 -0
- package/dist/capability.cjs.map +1 -0
- package/dist/capability.d.cts +189 -0
- package/dist/capability.d.mts +189 -0
- package/dist/capability.mjs +401 -0
- package/dist/capability.mjs.map +1 -0
- package/dist/error.cjs +76 -0
- package/dist/error.cjs.map +1 -0
- package/dist/error.d.cts +51 -0
- package/dist/error.d.mts +51 -0
- package/dist/error.mjs +72 -0
- package/dist/error.mjs.map +1 -0
- package/dist/fn.cjs +350 -0
- package/dist/fn.cjs.map +1 -0
- package/dist/fn.d.cts +378 -0
- package/dist/fn.d.mts +378 -0
- package/dist/fn.mjs +350 -0
- package/dist/fn.mjs.map +1 -0
- package/dist/index.cjs +43 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +50 -0
- package/dist/index.d.mts +50 -0
- package/dist/index.mjs +24 -0
- package/dist/index.mjs.map +1 -0
- package/dist/module.cjs +176 -0
- package/dist/module.cjs.map +1 -0
- package/dist/module.d.cts +283 -0
- package/dist/module.d.mts +283 -0
- package/dist/module.mjs +166 -0
- package/dist/module.mjs.map +1 -0
- package/dist/plugins/db.cjs +24 -0
- package/dist/plugins/db.cjs.map +1 -0
- package/dist/plugins/db.d.cts +18 -0
- package/dist/plugins/db.d.mts +18 -0
- package/dist/plugins/db.mjs +19 -0
- package/dist/plugins/db.mjs.map +1 -0
- package/dist/plugins/http/attrs.cjs +91 -0
- package/dist/plugins/http/attrs.cjs.map +1 -0
- package/dist/plugins/http/attrs.d.cts +26 -0
- package/dist/plugins/http/attrs.d.mts +26 -0
- package/dist/plugins/http/attrs.mjs +87 -0
- package/dist/plugins/http/attrs.mjs.map +1 -0
- package/dist/plugins/http/cookie.cjs +117 -0
- package/dist/plugins/http/cookie.cjs.map +1 -0
- package/dist/plugins/http/cookie.d.cts +637 -0
- package/dist/plugins/http/cookie.d.mts +637 -0
- package/dist/plugins/http/cookie.mjs +113 -0
- package/dist/plugins/http/cookie.mjs.map +1 -0
- package/dist/plugins/http/error.cjs +39 -0
- package/dist/plugins/http/error.cjs.map +1 -0
- package/dist/plugins/http/error.d.cts +38 -0
- package/dist/plugins/http/error.d.mts +38 -0
- package/dist/plugins/http/error.mjs +35 -0
- package/dist/plugins/http/error.mjs.map +1 -0
- package/dist/plugins/http/handle.cjs +73 -0
- package/dist/plugins/http/handle.cjs.map +1 -0
- package/dist/plugins/http/handle.d.cts +955 -0
- package/dist/plugins/http/handle.d.mts +955 -0
- package/dist/plugins/http/handle.mjs +72 -0
- package/dist/plugins/http/handle.mjs.map +1 -0
- package/dist/plugins/http/redirect.cjs +82 -0
- package/dist/plugins/http/redirect.cjs.map +1 -0
- package/dist/plugins/http/redirect.d.cts +67 -0
- package/dist/plugins/http/redirect.d.mts +67 -0
- package/dist/plugins/http/redirect.mjs +79 -0
- package/dist/plugins/http/redirect.mjs.map +1 -0
- package/dist/plugins/http/request.cjs +63 -0
- package/dist/plugins/http/request.cjs.map +1 -0
- package/dist/plugins/http/request.d.cts +142 -0
- package/dist/plugins/http/request.d.mts +142 -0
- package/dist/plugins/http/request.mjs +61 -0
- package/dist/plugins/http/request.mjs.map +1 -0
- package/dist/plugins/http/response.cjs +13 -0
- package/dist/plugins/http/response.cjs.map +1 -0
- package/dist/plugins/http/response.d.cts +18 -0
- package/dist/plugins/http/response.d.mts +18 -0
- package/dist/plugins/http/response.mjs +13 -0
- package/dist/plugins/http/response.mjs.map +1 -0
- package/dist/plugins/http.cjs +64 -0
- package/dist/plugins/http.cjs.map +1 -0
- package/dist/plugins/http.d.cts +945 -0
- package/dist/plugins/http.d.mts +945 -0
- package/dist/plugins/http.mjs +37 -0
- package/dist/plugins/http.mjs.map +1 -0
- package/dist/plugins/read-only.cjs +19 -0
- package/dist/plugins/read-only.cjs.map +1 -0
- package/dist/plugins/read-only.d.cts +17 -0
- package/dist/plugins/read-only.d.mts +17 -0
- package/dist/plugins/read-only.mjs +19 -0
- package/dist/plugins/read-only.mjs.map +1 -0
- package/dist/schema.cjs +245 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.cts +411 -0
- package/dist/schema.d.mts +411 -0
- package/dist/schema.mjs +235 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/scope.d.cts +31 -0
- package/dist/scope.d.mts +31 -0
- package/dist/storage.cjs +308 -0
- package/dist/storage.cjs.map +1 -0
- package/dist/storage.d.cts +220 -0
- package/dist/storage.d.mts +220 -0
- package/dist/storage.mjs +302 -0
- package/dist/storage.mjs.map +1 -0
- package/dist/types.d.cts +8 -0
- package/dist/types.d.mts +8 -0
- package/dist/var.cjs +186 -0
- package/dist/var.cjs.map +1 -0
- package/dist/var.d.cts +46 -0
- package/dist/var.d.mts +46 -0
- package/dist/var.mjs +177 -0
- package/dist/var.mjs.map +1 -0
- package/package.json +114 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { LiteralString, Prettify } from "./types.cjs";
|
|
2
|
+
//#region src/schema.d.ts
|
|
3
|
+
type Rules = {
|
|
4
|
+
/** Strings: length. Numbers: value. */
|
|
5
|
+
min?: number;
|
|
6
|
+
/** Strings: length. Numbers: value. */
|
|
7
|
+
max?: number;
|
|
8
|
+
/** Strings only: exact length. */
|
|
9
|
+
length?: number;
|
|
10
|
+
regex?: RegExp;
|
|
11
|
+
/** Strings only. Trims and lowercases before the format check. */
|
|
12
|
+
email?: boolean;
|
|
13
|
+
url?: boolean;
|
|
14
|
+
startsWith?: string;
|
|
15
|
+
endsWith?: string;
|
|
16
|
+
/** Numbers only. */
|
|
17
|
+
int?: boolean;
|
|
18
|
+
/** Allowed values. */
|
|
19
|
+
enum?: readonly unknown[];
|
|
20
|
+
/** Escape hatch - return true, or a message to fail with. */
|
|
21
|
+
check?: (value: any) => boolean | string;
|
|
22
|
+
};
|
|
23
|
+
/** Plugin-owned field metadata. Outer key is the plugin namespace
|
|
24
|
+
* (`"http"`, `"db"`, …); core never interprets the contents. */
|
|
25
|
+
type AttrBag = Record<string, Record<string, unknown>>;
|
|
26
|
+
interface TypeDefination<T, O, D = never> extends Rules {
|
|
27
|
+
name: LiteralString;
|
|
28
|
+
type?: T;
|
|
29
|
+
output?: O;
|
|
30
|
+
shape?: unknown;
|
|
31
|
+
/** `function` types only: the declared input of the expected fn -
|
|
32
|
+
* plain closures get it validated at their door on every call. */
|
|
33
|
+
fnInput?: unknown;
|
|
34
|
+
/** Used when the incoming value is `undefined`. A zero-arg function
|
|
35
|
+
* is called (fresh value per validate) except on `function` schemas,
|
|
36
|
+
* where the default IS the fn. */
|
|
37
|
+
default?: D | (() => D);
|
|
38
|
+
/** When true, `undefined` and `null` pass straight through unvalidated. */
|
|
39
|
+
optional?: boolean;
|
|
40
|
+
transform?: (value: any) => O;
|
|
41
|
+
/** Opaque plugin attributes - ignored by validate / Infer*. */
|
|
42
|
+
$attrs?: AttrBag;
|
|
43
|
+
}
|
|
44
|
+
type TypeOptions<T, O> = {
|
|
45
|
+
transform?: (value: T) => O;
|
|
46
|
+
/** Accepted on every helper; overloads refine the output when `true`. */
|
|
47
|
+
optional?: boolean;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* `optional` widens the output with `| undefined | null`; `default` keeps
|
|
51
|
+
* it narrow because a value is always produced. Declaring both means
|
|
52
|
+
* optional to send, never absent.
|
|
53
|
+
*
|
|
54
|
+
* Helpers select among these via option-shape overloads rather than `Opt` /
|
|
55
|
+
* `D` type parameters: providing a partial type argument (e.g.
|
|
56
|
+
* `v.string<"a" | "b">`) would lock remaining params to their defaults, so
|
|
57
|
+
* `{ optional: true }` would fail to type-check.
|
|
58
|
+
*/
|
|
59
|
+
type OutOf<O, D, Opt> = [Opt] extends [true] ? [D] extends [never] ? O | undefined | null : O : O;
|
|
60
|
+
/**
|
|
61
|
+
* Recover a type's output `O`. Plain `infer O` from
|
|
62
|
+
* `TypeDefination<any, infer O, …>` drops `| undefined` because `output?`
|
|
63
|
+
* is optional and TypeScript attributes the undefined to the property.
|
|
64
|
+
* When the third type arg is `undefined` (optional, no default), put
|
|
65
|
+
* `| undefined | null` back so handlers see the same absence validate
|
|
66
|
+
* produces at runtime.
|
|
67
|
+
*/
|
|
68
|
+
type OutputOf<F> = F extends TypeDefination<any, infer O, infer D> ? [D] extends [never] ? O : undefined extends D ? O | undefined | null : O : never;
|
|
69
|
+
type StringOptions<E extends string, O> = TypeOptions<E, O> & Pick<Rules, "min" | "max" | "length" | "regex" | "email" | "url" | "startsWith" | "endsWith" | "check"> & {
|
|
70
|
+
enum?: readonly E[];
|
|
71
|
+
};
|
|
72
|
+
type ArrayOptions<E, O> = TypeOptions<FieldOut<E>[], O> & Pick<Rules, "min" | "max" | "length" | "check">;
|
|
73
|
+
type UnionOptions<T extends readonly unknown[], O> = TypeOptions<FieldOut<T[number]>, O> & Pick<Rules, "check">;
|
|
74
|
+
type NumberOptions<O> = TypeOptions<number, O> & Pick<Rules, "min" | "max" | "int" | "check"> & {
|
|
75
|
+
enum?: readonly number[];
|
|
76
|
+
};
|
|
77
|
+
type InferType<T> = T extends TypeDefination<infer T2, any, any> ? T2 : never;
|
|
78
|
+
type InferOutput<T> = T extends TypeDefination<any, any, any> ? OutputOf<T> : never;
|
|
79
|
+
type DefineInput<I> = Prettify<{ [K in keyof I]: FieldIn<I[K]>; }>;
|
|
80
|
+
/** Keys whose field is OPTIONAL with no default: absent from the output
|
|
81
|
+
* too, so they mark `?`. A defaulted field always produces a value and
|
|
82
|
+
* stays required. */
|
|
83
|
+
type OutOptional<O> = { [K in keyof O]: [DefaultOf<O[K]>] extends [never] ? never : [DefaultOf<O[K]>] extends [undefined] ? K : never; }[keyof O];
|
|
84
|
+
type DefineOutput<O> = Prettify<{ [K in keyof O as K extends OutOptional<O> ? never : K]: FieldOut<O[K]>; } & { [K in keyof O as K extends OutOptional<O> ? K : never]?: FieldOut<O[K]>; }>;
|
|
85
|
+
/**
|
|
86
|
+
* A handler-less `v.fn({ input, output })` used as a schema describes
|
|
87
|
+
* "a fn from `input` to `output`" - the VALUE is the fn itself. Two
|
|
88
|
+
* views of the same signature:
|
|
89
|
+
*
|
|
90
|
+
* `SchemaFnIn` is the PROVIDER's side - what a caller must hand over.
|
|
91
|
+
* Like any handler, their fn receives the PARSED input (validation runs
|
|
92
|
+
* at its door) and returns the declared output, sync or async.
|
|
93
|
+
*
|
|
94
|
+
* `SchemaFnOut` is the CONSUMER's side (`c.input.x`) - the handler calls
|
|
95
|
+
* it with RAW args, exactly like calling the fn it stands in for.
|
|
96
|
+
*
|
|
97
|
+
* No declared input means the signature is UNSPECIFIED, not zero-arg -
|
|
98
|
+
* any fn fits (`create: v.fn`), so the args stay open.
|
|
99
|
+
*/
|
|
100
|
+
type SchemaFnIn<FI, FO> = (...args: unknown extends FI ? any[] : [input: InferInput<FI>]) => unknown extends FO ? any : InferInput<OutputSchemaOf<FO>> | Promise<InferInput<OutputSchemaOf<FO>>>;
|
|
101
|
+
type SchemaFnOut<FI, FO> = (...args: unknown extends FI ? any[] : [input: InferArgs<FI>]) => unknown extends FO ? any : InferInput<OutputSchemaOf<FO>> | Promise<InferInput<OutputSchemaOf<FO>>>;
|
|
102
|
+
/**
|
|
103
|
+
* A fn schema whose input IS a var carries that var's name as an optional
|
|
104
|
+
* phantom (`$fnVar`, tuple-wrapped so a plain fn can never false-match).
|
|
105
|
+
* Scope resolution reads it to WIDEN the fn's args with everything the
|
|
106
|
+
* scope mounts on that var - see `WidenSchemaFns`. Optional, so any plain
|
|
107
|
+
* closure still satisfies the type.
|
|
108
|
+
*/
|
|
109
|
+
type FnVarBrand<FI> = FI extends {
|
|
110
|
+
$var: true;
|
|
111
|
+
name: infer N extends string;
|
|
112
|
+
} ? {
|
|
113
|
+
readonly $fnVar?: [N];
|
|
114
|
+
} : unknown;
|
|
115
|
+
/**
|
|
116
|
+
* A declared `output` comes in two forms: a bare schema (the signature
|
|
117
|
+
* AND the exit check), or the wrapper `{ def?, validation? }` splitting
|
|
118
|
+
* what the fn PROMISES from what gets CHECKED - `{ def }` documents
|
|
119
|
+
* without paying runtime validation, `{ def, validation }` checks with a
|
|
120
|
+
* different (usually looser) schema than it documents. The wrapper is
|
|
121
|
+
* recognized by its keys, so an output that IS an object with only
|
|
122
|
+
* `def`/`validation` fields must be written `v.object({...})`.
|
|
123
|
+
*
|
|
124
|
+
* `OutputSchemaOf` is the type-level unwrap - the schema the fn's return
|
|
125
|
+
* type (and its rendered signature) comes from.
|
|
126
|
+
*/
|
|
127
|
+
type OutputSchemaOf<O> = Exclude<keyof O, "def" | "validation"> extends never ? O extends {
|
|
128
|
+
def: infer D;
|
|
129
|
+
} ? D : O extends {
|
|
130
|
+
validation: infer Vl;
|
|
131
|
+
} ? Vl : O : O;
|
|
132
|
+
/** The runtime unwrap: `def` is the documented schema (falls back to
|
|
133
|
+
* `validation`), `validation` is what the exit check runs - undefined
|
|
134
|
+
* means no check. A bare schema is both. */
|
|
135
|
+
declare const outputContract: (output: unknown) => {
|
|
136
|
+
def?: unknown;
|
|
137
|
+
validation?: unknown;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* One input field, in five flavours:
|
|
141
|
+
* - a `v.var()`, whose shape comes from the var's own `schema`
|
|
142
|
+
* - a handler-less `v.fn(...)` builder, which types the field as a FN
|
|
143
|
+
* - a type from `v.string()` / `v.object()` / ...
|
|
144
|
+
* - a bare nested record, which recurses
|
|
145
|
+
* - anything else (e.g. `null`, `"lit"`) kept as itself - so
|
|
146
|
+
* `v.union([user, null])` infers `User | null`, not just `User`
|
|
147
|
+
*
|
|
148
|
+
* The record case has to come after TypeDefination: a TypeDefination is
|
|
149
|
+
* itself a record, and so is a builder.
|
|
150
|
+
*/
|
|
151
|
+
type FieldOut<F> = F extends {
|
|
152
|
+
$var: true;
|
|
153
|
+
schema?: infer S;
|
|
154
|
+
} ? InferInput<NonNullable<S>> : F extends {
|
|
155
|
+
$fnSchema: {
|
|
156
|
+
input?: infer FI;
|
|
157
|
+
output?: infer FO;
|
|
158
|
+
};
|
|
159
|
+
} ? FnSchemaOut<F, SchemaFnOut<FI, FO> & FnVarBrand<FI>> : F extends TypeDefination<any, any, any> ? OutputOf<F> : F extends Record<string, unknown> ? Prettify<{ [K in keyof F]: FieldOut<F[K]>; }> : F;
|
|
160
|
+
type FieldIn<F> = F extends {
|
|
161
|
+
$var: true;
|
|
162
|
+
schema?: infer S;
|
|
163
|
+
} ? InferArgs<NonNullable<S>> : F extends {
|
|
164
|
+
$fnSchema: {
|
|
165
|
+
input?: infer FI;
|
|
166
|
+
output?: infer FO;
|
|
167
|
+
};
|
|
168
|
+
} ? SchemaFnIn<FI, FO> & FnVarBrand<FI> : F extends TypeDefination<infer T, any, infer D> ? undefined extends D ? T | null : T : F extends Record<string, unknown> ? ArgsShape<F> : F;
|
|
169
|
+
/**
|
|
170
|
+
* `optional: true` on `v.fn.type` widens the same way a type's `optional`
|
|
171
|
+
* does: absent without a default means the value may be `undefined` or
|
|
172
|
+
* `null`.
|
|
173
|
+
*/
|
|
174
|
+
type FnSchemaOut<F, Fn> = F extends {
|
|
175
|
+
optional: true;
|
|
176
|
+
} ? F extends {
|
|
177
|
+
default: infer _D;
|
|
178
|
+
} ? Fn : Fn | undefined | null : Fn;
|
|
179
|
+
/**
|
|
180
|
+
* A field's declared default, looked through a var to its schema. Only a
|
|
181
|
+
* TYPE's default counts - a var's own default is its initial value, not a
|
|
182
|
+
* licence to omit the input. The `$fnSchema` guard mirrors `asType`'s
|
|
183
|
+
* ordering: a bare `v.fn` is CALLABLE, and any callable duck-matches
|
|
184
|
+
* TypeDefination (`.name` comes with every function), which would read a
|
|
185
|
+
* phantom default off it and wrongly mark the field optional. `v.fn.type`
|
|
186
|
+
* may still declare `optional` / `default` on the carrier itself.
|
|
187
|
+
*/
|
|
188
|
+
type DefaultOf<F> = F extends {
|
|
189
|
+
$var: true;
|
|
190
|
+
schema?: infer S;
|
|
191
|
+
} ? NonNullable<S> extends TypeDefination<any, any, infer D> ? D : never : F extends {
|
|
192
|
+
$fnSchema: unknown;
|
|
193
|
+
} ? F extends {
|
|
194
|
+
optional: true;
|
|
195
|
+
} ? F extends {
|
|
196
|
+
default: infer D;
|
|
197
|
+
} ? D : undefined : F extends {
|
|
198
|
+
default: infer D;
|
|
199
|
+
} ? D : never : F extends TypeDefination<any, any, infer D> ? D : never;
|
|
200
|
+
type Defaulted<I> = { [K in keyof I]: [DefaultOf<I[K]>] extends [never] ? never : K; }[keyof I];
|
|
201
|
+
/** Defaulted keys are optional to send, but always present in the handler. */
|
|
202
|
+
type ArgsShape<I> = Prettify<{ [K in keyof I as K extends Defaulted<I> ? never : K]: FieldIn<I[K]>; } & { [K in keyof I as K extends Defaulted<I> ? K : never]?: FieldIn<I[K]>; }>;
|
|
203
|
+
/**
|
|
204
|
+
* Post-transform shape - what a handler sees. The `$var` branch must come
|
|
205
|
+
* first: a var's `name` property duck-matches TypeDefination, and falling
|
|
206
|
+
* into that branch reads the var's VALUE type instead of its schema. A
|
|
207
|
+
* TUPLE input maps position by position - the fn takes that many args.
|
|
208
|
+
*/
|
|
209
|
+
type InferInput<I> = I extends {
|
|
210
|
+
$var: true;
|
|
211
|
+
schema?: infer S;
|
|
212
|
+
} ? InferInput<NonNullable<S>> : I extends {
|
|
213
|
+
$fnSchema: {
|
|
214
|
+
input?: infer FI;
|
|
215
|
+
output?: infer FO;
|
|
216
|
+
};
|
|
217
|
+
} ? SchemaFnOut<FI, FO> & FnVarBrand<FI> : I extends readonly unknown[] ? { -readonly [K in keyof I]: InferInput<I[K]>; } : I extends TypeDefination<any, any, any> ? OutputOf<I> : Prettify<{ [K in keyof I]: FieldOut<I[K]>; }>;
|
|
218
|
+
/** Pre-transform shape - what a caller sends. Same branch order. */
|
|
219
|
+
type InferArgs<I> = I extends {
|
|
220
|
+
$var: true;
|
|
221
|
+
schema?: infer S;
|
|
222
|
+
} ? InferArgs<NonNullable<S>> : I extends {
|
|
223
|
+
$fnSchema: {
|
|
224
|
+
input?: infer FI;
|
|
225
|
+
output?: infer FO;
|
|
226
|
+
};
|
|
227
|
+
} ? SchemaFnIn<FI, FO> & FnVarBrand<FI> : I extends readonly unknown[] ? { -readonly [K in keyof I]: InferArgs<I[K]>; } : I extends TypeDefination<infer T, any, infer D> ? undefined extends D ? T | null : T : ArgsShape<I>;
|
|
228
|
+
declare const isType: (value: any) => value is TypeDefination<any, any>;
|
|
229
|
+
/** A handler-less `v.fn(...)` builder doubles as a schema: the value it
|
|
230
|
+
* describes is a FN with the declared signature. The builder FN itself is
|
|
231
|
+
* branded too, so bare `v.fn` reads as "any function". */
|
|
232
|
+
declare const isFnSchema: (value: any) => value is {
|
|
233
|
+
$fnSchema: {
|
|
234
|
+
input?: unknown;
|
|
235
|
+
output?: unknown;
|
|
236
|
+
};
|
|
237
|
+
optional?: boolean;
|
|
238
|
+
default?: unknown;
|
|
239
|
+
};
|
|
240
|
+
declare const asType: (value: any) => TypeDefination<any, any>;
|
|
241
|
+
/**
|
|
242
|
+
* Attach plugin attributes under `namespace`, deep-merging with any
|
|
243
|
+
* already on the schema. Returns a new type def (or var) with the same
|
|
244
|
+
* value type. Core never reads these - only plugin edges do.
|
|
245
|
+
*
|
|
246
|
+
* Passed a var, attributes land on the var itself (`$attrs`) and the
|
|
247
|
+
* `$var` / `name` / `schema` identity is preserved; `customize` is rebound
|
|
248
|
+
* so attrs survive further customization.
|
|
249
|
+
*/
|
|
250
|
+
declare const withAttrs: <S>(schema: S, namespace: string, attrs: Record<string, unknown>) => S;
|
|
251
|
+
/** Read the whole attr bag, or one plugin namespace. */
|
|
252
|
+
declare function attrsOf(schema: unknown): AttrBag | undefined;
|
|
253
|
+
declare function attrsOf(schema: unknown, namespace: string): Record<string, unknown> | undefined;
|
|
254
|
+
declare const typeOf: (value: unknown) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array" | "NaN";
|
|
255
|
+
declare const isVar: (value: any) => boolean;
|
|
256
|
+
declare const validate: (def: TypeDefination<any, any, any>, value: unknown, path: string) => any;
|
|
257
|
+
/** A default may be the value itself or a factory that mints it fresh on
|
|
258
|
+
* each validate - `() => new Date()`, `() => []`, …. */
|
|
259
|
+
type DefaultInput<T> = T | (() => T);
|
|
260
|
+
/**
|
|
261
|
+
* Call signatures for the type helpers. Option-shape overloads replace
|
|
262
|
+
* `Opt` / `D` type parameters so a partial type argument (literal narrowing)
|
|
263
|
+
* does not lock `optional` / `default` to their defaults.
|
|
264
|
+
*/
|
|
265
|
+
type StringFn = {
|
|
266
|
+
<const E extends string, O = E>(options: StringOptions<E, O> & {
|
|
267
|
+
optional: true;
|
|
268
|
+
default: DefaultInput<string>;
|
|
269
|
+
}): TypeDefination<E, O, string>;
|
|
270
|
+
<const E extends string, O = E>(options: StringOptions<E, O> & {
|
|
271
|
+
optional: true;
|
|
272
|
+
}): TypeDefination<E, OutOf<O, never, true>, undefined>;
|
|
273
|
+
<const E extends string, O = E>(options: StringOptions<E, O> & {
|
|
274
|
+
default: DefaultInput<string>;
|
|
275
|
+
}): TypeDefination<E, O, string>;
|
|
276
|
+
<const E extends string, O = E>(options?: StringOptions<E, O>): TypeDefination<E, O, never>;
|
|
277
|
+
};
|
|
278
|
+
type NumberFn = {
|
|
279
|
+
<O = number>(options: NumberOptions<O> & {
|
|
280
|
+
optional: true;
|
|
281
|
+
default: DefaultInput<number>;
|
|
282
|
+
}): TypeDefination<number, O, number>;
|
|
283
|
+
<O = number>(options: NumberOptions<O> & {
|
|
284
|
+
optional: true;
|
|
285
|
+
}): TypeDefination<number, OutOf<O, never, true>, undefined>;
|
|
286
|
+
<O = number>(options: NumberOptions<O> & {
|
|
287
|
+
default: DefaultInput<number>;
|
|
288
|
+
}): TypeDefination<number, O, number>;
|
|
289
|
+
<O = number>(options?: NumberOptions<O>): TypeDefination<number, O, never>;
|
|
290
|
+
};
|
|
291
|
+
type BooleanFn = {
|
|
292
|
+
<O = boolean>(options: TypeOptions<boolean, O> & {
|
|
293
|
+
optional: true;
|
|
294
|
+
default: DefaultInput<boolean>;
|
|
295
|
+
}): TypeDefination<boolean, O, boolean>;
|
|
296
|
+
<O = boolean>(options: TypeOptions<boolean, O> & {
|
|
297
|
+
optional: true;
|
|
298
|
+
}): TypeDefination<boolean, OutOf<O, never, true>, undefined>;
|
|
299
|
+
<O = boolean>(options: TypeOptions<boolean, O> & {
|
|
300
|
+
default: DefaultInput<boolean>;
|
|
301
|
+
}): TypeDefination<boolean, O, boolean>;
|
|
302
|
+
<O = boolean>(options?: TypeOptions<boolean, O>): TypeDefination<boolean, O, never>;
|
|
303
|
+
};
|
|
304
|
+
type DateFn = {
|
|
305
|
+
<O = Date>(options: TypeOptions<Date, O> & {
|
|
306
|
+
optional: true;
|
|
307
|
+
default: DefaultInput<Date>;
|
|
308
|
+
}): TypeDefination<Date, O, Date>;
|
|
309
|
+
<O = Date>(options: TypeOptions<Date, O> & {
|
|
310
|
+
optional: true;
|
|
311
|
+
}): TypeDefination<Date, OutOf<O, never, true>, undefined>;
|
|
312
|
+
<O = Date>(options: TypeOptions<Date, O> & {
|
|
313
|
+
default: DefaultInput<Date>;
|
|
314
|
+
}): TypeDefination<Date, O, Date>;
|
|
315
|
+
<O = Date>(options?: TypeOptions<Date, O>): TypeDefination<Date, O, never>;
|
|
316
|
+
};
|
|
317
|
+
type AnyFn = {
|
|
318
|
+
<T = unknown>(options: TypeOptions<T, T> & {
|
|
319
|
+
optional: true;
|
|
320
|
+
default: DefaultInput<T>;
|
|
321
|
+
}): TypeDefination<T, T, T>;
|
|
322
|
+
<T = unknown>(options: TypeOptions<T, T> & {
|
|
323
|
+
optional: true;
|
|
324
|
+
}): TypeDefination<T, OutOf<T, never, true>, undefined>;
|
|
325
|
+
<T = unknown>(options: TypeOptions<T, T> & {
|
|
326
|
+
default: DefaultInput<T>;
|
|
327
|
+
}): TypeDefination<T, T, T>;
|
|
328
|
+
<T = unknown>(options?: TypeOptions<T, T>): TypeDefination<T, T, never>;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Object defaults may be partial: nested fields still run through
|
|
332
|
+
* validate (so their own defaults apply). An empty `{}` is always
|
|
333
|
+
* fine at the type level - parent `optional` / `default` must not
|
|
334
|
+
* demand that every required child be listed in the default.
|
|
335
|
+
*/
|
|
336
|
+
type ObjectDefault<S> = DefaultInput<Partial<ArgsShape<S>>>;
|
|
337
|
+
type ObjectFn = {
|
|
338
|
+
<S, O = DefineOutput<S>>(shape: S, options: TypeOptions<DefineOutput<S>, O> & {
|
|
339
|
+
optional: true;
|
|
340
|
+
default: ObjectDefault<S>;
|
|
341
|
+
}): TypeDefination<ArgsShape<S>, O, ArgsShape<S>>;
|
|
342
|
+
<S, O = DefineOutput<S>>(shape: S, options: TypeOptions<DefineOutput<S>, O> & {
|
|
343
|
+
optional: true;
|
|
344
|
+
}): TypeDefination<ArgsShape<S>, OutOf<O, never, true>, undefined>;
|
|
345
|
+
<S, O = DefineOutput<S>>(shape: S, options: TypeOptions<DefineOutput<S>, O> & {
|
|
346
|
+
default: ObjectDefault<S>;
|
|
347
|
+
}): TypeDefination<ArgsShape<S>, O, ArgsShape<S>>;
|
|
348
|
+
<S, O = DefineOutput<S>>(shape: S, options?: TypeOptions<DefineOutput<S>, O>): TypeDefination<ArgsShape<S>, O, never>;
|
|
349
|
+
(): TypeDefination<Record<string, any>, Record<string, any>>;
|
|
350
|
+
};
|
|
351
|
+
type ArrayFn = {
|
|
352
|
+
<E, O = FieldOut<E>[]>(element: E, options: ArrayOptions<E, O> & {
|
|
353
|
+
optional: true;
|
|
354
|
+
default: DefaultInput<FieldIn<E>[]>;
|
|
355
|
+
}): TypeDefination<FieldIn<E>[], O, FieldIn<E>[]>;
|
|
356
|
+
<E, O = FieldOut<E>[]>(element: E, options: ArrayOptions<E, O> & {
|
|
357
|
+
optional: true;
|
|
358
|
+
}): TypeDefination<FieldIn<E>[], OutOf<O, never, true>, undefined>;
|
|
359
|
+
<E, O = FieldOut<E>[]>(element: E, options: ArrayOptions<E, O> & {
|
|
360
|
+
default: DefaultInput<FieldIn<E>[]>;
|
|
361
|
+
}): TypeDefination<FieldIn<E>[], O, FieldIn<E>[]>;
|
|
362
|
+
<E, O = FieldOut<E>[]>(element: E, options?: ArrayOptions<E, O>): TypeDefination<FieldIn<E>[], O, never>;
|
|
363
|
+
(element?: undefined, options?: ArrayOptions<undefined, any[]> & {
|
|
364
|
+
optional: true;
|
|
365
|
+
default: DefaultInput<any[]>;
|
|
366
|
+
}): TypeDefination<any[], any[], any[]>;
|
|
367
|
+
(element?: undefined, options?: ArrayOptions<undefined, any[]> & {
|
|
368
|
+
optional: true;
|
|
369
|
+
}): TypeDefination<any[], any[] | undefined | null, undefined>;
|
|
370
|
+
(element?: undefined, options?: ArrayOptions<undefined, any[]> & {
|
|
371
|
+
default: DefaultInput<any[]>;
|
|
372
|
+
}): TypeDefination<any[], any[], any[]>;
|
|
373
|
+
(element?: undefined, options?: ArrayOptions<undefined, any[]>): TypeDefination<any[], any[], never>;
|
|
374
|
+
};
|
|
375
|
+
type UnionFn = {
|
|
376
|
+
<const T extends readonly [unknown, ...unknown[]], O = FieldOut<T[number]>>(members: T, options: UnionOptions<T, O> & {
|
|
377
|
+
optional: true;
|
|
378
|
+
default: DefaultInput<FieldIn<T[number]>>;
|
|
379
|
+
}): TypeDefination<FieldIn<T[number]>, O, FieldIn<T[number]>>;
|
|
380
|
+
<const T extends readonly [unknown, ...unknown[]], O = FieldOut<T[number]>>(members: T, options: UnionOptions<T, O> & {
|
|
381
|
+
optional: true;
|
|
382
|
+
}): TypeDefination<FieldIn<T[number]>, OutOf<O, never, true>, undefined>;
|
|
383
|
+
<const T extends readonly [unknown, ...unknown[]], O = FieldOut<T[number]>>(members: T, options: UnionOptions<T, O> & {
|
|
384
|
+
default: DefaultInput<FieldIn<T[number]>>;
|
|
385
|
+
}): TypeDefination<FieldIn<T[number]>, O, FieldIn<T[number]>>;
|
|
386
|
+
<const T extends readonly [unknown, ...unknown[]], O = FieldOut<T[number]>>(members: T, options?: UnionOptions<T, O>): TypeDefination<FieldIn<T[number]>, O, never>;
|
|
387
|
+
};
|
|
388
|
+
declare const vTypes: {
|
|
389
|
+
/** An `enum` narrows both sides to the literal union: `v.string({
|
|
390
|
+
* enum: ["a", "b"] })` types as `"a" | "b"`, not `string`. */
|
|
391
|
+
string: StringFn;
|
|
392
|
+
number: NumberFn;
|
|
393
|
+
boolean: BooleanFn;
|
|
394
|
+
/** A Date INSTANCE - checked with `instanceof`, never parsed. */
|
|
395
|
+
date: DateFn;
|
|
396
|
+
/** Passthrough - validated as-is, never coerced or stripped. */
|
|
397
|
+
any: AnyFn;
|
|
398
|
+
/** With a SHAPE every field validates; with NO shape (`v.object()`)
|
|
399
|
+
* any object passes, as-is. */
|
|
400
|
+
object: ObjectFn;
|
|
401
|
+
/** With an ELEMENT every item validates - all failures report
|
|
402
|
+
* together, like object fields; with NO element (`v.array()`) any
|
|
403
|
+
* array passes, as-is. `min`/`max`/`length` count items. */
|
|
404
|
+
array: ArrayFn;
|
|
405
|
+
/** Try each option in order - first successful parse wins. All
|
|
406
|
+
* failing branches report together when none match. */
|
|
407
|
+
union: UnionFn;
|
|
408
|
+
};
|
|
409
|
+
//#endregion
|
|
410
|
+
export { AttrBag, DefineInput, DefineOutput, FnVarBrand, InferArgs, InferInput, InferOutput, InferType, OutputSchemaOf, Rules, TypeDefination, TypeOptions, asType, attrsOf, isFnSchema, isType, isVar, outputContract, typeOf, vTypes, validate, withAttrs };
|
|
411
|
+
//# sourceMappingURL=schema.d.cts.map
|