@oh-my-pi/omptype 17.2.7 → 17.2.8
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/CHANGELOG.md +23 -0
- package/dist/js/compile.js +414 -219
- package/dist/js/errors.js +236 -41
- package/dist/js/from-json-schema.js +234 -0
- package/dist/js/index.js +1 -0
- package/dist/js/interp.js +514 -132
- package/dist/js/ir.js +972 -202
- package/dist/js/json-schema.js +73 -34
- package/dist/js/keywords.js +108 -1
- package/dist/js/type.js +2156 -172
- package/dist/js/typebox.js +41 -32
- package/dist/js/zod.js +2 -2
- package/dist/types/compile.d.ts +1 -1
- package/dist/types/errors.d.ts +19 -18
- package/dist/types/from-json-schema.d.ts +9 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/infer.d.ts +45 -6
- package/dist/types/interp.d.ts +9 -2
- package/dist/types/ir.d.ts +57 -6
- package/dist/types/json-schema.d.ts +10 -1
- package/dist/types/type.d.ts +324 -48
- package/dist/types/typebox.d.ts +78 -50
- package/package.json +5 -1
- package/src/compile.ts +598 -243
- package/src/errors.ts +247 -49
- package/src/from-json-schema.ts +231 -0
- package/src/index.ts +1 -0
- package/src/infer.ts +107 -32
- package/src/interp.ts +457 -118
- package/src/ir.ts +981 -212
- package/src/json-schema.ts +82 -34
- package/src/keywords.ts +111 -1
- package/src/type.ts +2760 -271
- package/src/typebox.ts +141 -98
- package/src/zod.ts +1 -1
package/dist/types/type.d.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { type ErrorConfig, OmpErrors } from "./errors.js";
|
|
2
|
-
import type { InferDef, InferDefIn, InferObjectLiteral, InferString } from "./infer.js";
|
|
2
|
+
import type { InferDef, InferDefIn, InferObjectLiteral, InferObjectLiteralIn, InferString } from "./infer.js";
|
|
3
3
|
import { type Constructor, type Def, hasMorph, type IR, IR_BRAND } from "./ir.js";
|
|
4
4
|
import { type JsonSchemaOptions } from "./json-schema.js";
|
|
5
|
+
export interface NarrowErrorInput {
|
|
6
|
+
readonly expected: string;
|
|
7
|
+
readonly actual?: unknown;
|
|
8
|
+
readonly path?: readonly PropertyKey[];
|
|
9
|
+
readonly relativePath?: readonly PropertyKey[];
|
|
10
|
+
}
|
|
5
11
|
/** Context passed to `.narrow()` / `.pipe()` callbacks. */
|
|
6
12
|
export interface NarrowContext {
|
|
7
|
-
|
|
13
|
+
readonly path: readonly PropertyKey[];
|
|
14
|
+
error(error: string | NarrowErrorInput): OmpErrors;
|
|
8
15
|
mustBe(expectation: string): false;
|
|
9
|
-
|
|
10
|
-
reject(problem: string): false;
|
|
16
|
+
reject(problem: string | NarrowErrorInput): OmpErrors | false;
|
|
11
17
|
}
|
|
12
18
|
/** Schema metadata and validation-message overrides accepted by `.configure()`. */
|
|
13
19
|
export interface SchemaConfig extends ErrorConfig {
|
|
@@ -40,6 +46,49 @@ export interface SelectedNode {
|
|
|
40
46
|
readonly node: IR;
|
|
41
47
|
readonly unit?: unknown;
|
|
42
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Standard Schema V1 (https://standardschema.dev) — the cross-library
|
|
51
|
+
* validation interface consumed by tools like @t3-oss/env, tRPC, and
|
|
52
|
+
* Hono validators. Inlined per the spec's recommendation; no dependency.
|
|
53
|
+
*/
|
|
54
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
55
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
56
|
+
}
|
|
57
|
+
export declare namespace StandardSchemaV1 {
|
|
58
|
+
interface Props<Input = unknown, Output = Input> {
|
|
59
|
+
readonly version: 1;
|
|
60
|
+
readonly vendor: string;
|
|
61
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
62
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
63
|
+
readonly jsonSchema: {
|
|
64
|
+
readonly input: (options: StandardJsonSchemaOptions) => Record<string, unknown>;
|
|
65
|
+
readonly output: (options: StandardJsonSchemaOptions) => Record<string, unknown>;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
69
|
+
interface SuccessResult<Output> {
|
|
70
|
+
readonly value: Output;
|
|
71
|
+
readonly issues?: undefined;
|
|
72
|
+
}
|
|
73
|
+
interface FailureResult {
|
|
74
|
+
readonly issues: readonly Issue[];
|
|
75
|
+
}
|
|
76
|
+
interface Issue {
|
|
77
|
+
readonly message: string;
|
|
78
|
+
readonly path?: readonly PropertyKey[] | undefined;
|
|
79
|
+
}
|
|
80
|
+
interface Types<Input = unknown, Output = Input> {
|
|
81
|
+
readonly input: Input;
|
|
82
|
+
readonly output: Output;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export interface StandardJsonSchemaOptions {
|
|
86
|
+
readonly target: "draft-2020-12" | "draft-07" | string;
|
|
87
|
+
readonly libraryOptions?: {
|
|
88
|
+
readonly dialect?: string | null;
|
|
89
|
+
readonly fallback?: JsonSchemaOptions["fallback"];
|
|
90
|
+
};
|
|
91
|
+
}
|
|
43
92
|
/** A compiled schema: callable validator plus composition methods. */
|
|
44
93
|
export interface Type<out t = unknown, i = t> {
|
|
45
94
|
(data: unknown): t | OmpErrors;
|
|
@@ -51,10 +100,26 @@ export interface Type<out t = unknown, i = t> {
|
|
|
51
100
|
readonly hasDefault: boolean;
|
|
52
101
|
readonly defaultValue?: unknown;
|
|
53
102
|
readonly description?: string;
|
|
103
|
+
/** Canonical ArkType-compatible expression for diagnostics. */
|
|
104
|
+
readonly expression: string;
|
|
105
|
+
/** Canonical structural node representation. */
|
|
106
|
+
readonly json: unknown;
|
|
54
107
|
/** Full validate+morph pipeline; identical to calling the schema. */
|
|
55
108
|
readonly run: (data: unknown) => unknown;
|
|
109
|
+
/** ArkType-compatible inference alias (type-only; undefined at runtime). */
|
|
110
|
+
readonly t: t;
|
|
111
|
+
/** Scope that parsed this schema (or the ambient Ark-compatible scope). */
|
|
112
|
+
readonly $: TypeScope | {
|
|
113
|
+
readonly internal: {
|
|
114
|
+
readonly name: "ark";
|
|
115
|
+
};
|
|
116
|
+
};
|
|
56
117
|
/** Inference-only output type (no runtime value). */
|
|
57
118
|
readonly infer: t;
|
|
119
|
+
/** Standalone validator for the schema's accepted input. */
|
|
120
|
+
readonly in: FluentType<i>;
|
|
121
|
+
/** Standalone validator for its known output, or `unknown` after an opaque morph. */
|
|
122
|
+
readonly out: FluentType<t>;
|
|
58
123
|
/** Inference-only input type (no runtime value). */
|
|
59
124
|
readonly inferIn: i;
|
|
60
125
|
/** Structural + narrow check without running pipes. */
|
|
@@ -65,25 +130,49 @@ export interface Type<out t = unknown, i = t> {
|
|
|
65
130
|
from(data: i): t;
|
|
66
131
|
/** JSON Schema for this schema's structural base. */
|
|
67
132
|
toJsonSchema(options?: ToJsonSchemaOptions): Record<string, unknown>;
|
|
133
|
+
/** Standard Schema V1 interop (synchronous validation). */
|
|
134
|
+
readonly "~standard": StandardSchemaV1.Props<i, t>;
|
|
68
135
|
}
|
|
69
136
|
type MergeTypes<left, right> = left extends object ? right extends object ? Omit<left, keyof right> & right : right : right;
|
|
137
|
+
type SimplifyNary<t> = t extends object ? {
|
|
138
|
+
[key in keyof t]: t[key];
|
|
139
|
+
} : t;
|
|
140
|
+
type UnionToIntersection<union> = (union extends unknown ? (value: union) => void : never) extends (value: infer intersection) => void ? intersection : never;
|
|
141
|
+
type NaryOrOutput<definitions extends readonly unknown[]> = InferDef<definitions[number]>;
|
|
142
|
+
type NaryOrInput<definitions extends readonly unknown[]> = InferDefIn<definitions[number]>;
|
|
143
|
+
type NaryAndOutput<definitions extends readonly unknown[]> = definitions extends readonly [] ? unknown : SimplifyNary<UnionToIntersection<InferDef<definitions[number]>>>;
|
|
144
|
+
type NaryAndInput<definitions extends readonly unknown[]> = definitions extends readonly [] ? unknown : SimplifyNary<UnionToIntersection<InferDefIn<definitions[number]>>>;
|
|
145
|
+
type ReduceNaryMergeOutput<definitions extends readonly unknown[], result = {}> = definitions extends readonly [
|
|
146
|
+
infer head,
|
|
147
|
+
...infer tail
|
|
148
|
+
] ? ReduceNaryMergeOutput<tail, SimplifyNary<MergeTypes<result, InferDef<head>>>> : definitions extends readonly [] ? result : {};
|
|
149
|
+
type ReduceNaryMergeInput<definitions extends readonly unknown[], result = {}> = definitions extends readonly [
|
|
150
|
+
infer head,
|
|
151
|
+
...infer tail
|
|
152
|
+
] ? ReduceNaryMergeInput<tail, SimplifyNary<MergeTypes<result, InferDefIn<head>>>> : definitions extends readonly [] ? result : {};
|
|
153
|
+
type NaryMergeOutput<definitions extends readonly unknown[]> = definitions extends readonly [] ? object : ReduceNaryMergeOutput<definitions>;
|
|
154
|
+
type NaryMergeInput<definitions extends readonly unknown[]> = definitions extends readonly [] ? object : ReduceNaryMergeInput<definitions>;
|
|
155
|
+
type PipeItemOutput<item> = item extends SchemaInference<infer output, unknown> ? output : item extends (data: never, ...arguments_: never[]) => infer output ? Exclude<output, OmpErrors> : InferDef<item>;
|
|
156
|
+
type NaryPipeOutput<items extends readonly unknown[]> = items extends readonly [...(readonly unknown[]), infer last] ? PipeItemOutput<last> : unknown;
|
|
157
|
+
type NaryPipeInput<items extends readonly unknown[]> = items extends readonly [infer first, ...(readonly unknown[])] ? first extends SchemaInference<unknown, infer input> ? input : first extends (data: infer input, ...arguments_: never[]) => unknown ? input : InferDefIn<first> : unknown;
|
|
70
158
|
interface FluentMethods<t, i> {
|
|
71
159
|
describe(description: string): FluentType<t, i>;
|
|
72
|
-
configure(config: SchemaConfig): FluentType<t, i>;
|
|
73
|
-
default(value:
|
|
160
|
+
configure(config: SchemaConfig, selector?: "self" | ConfigureSelector): FluentType<t, i>;
|
|
161
|
+
default(value: i | (() => i)): FluentType<t, i | undefined>;
|
|
74
162
|
optional(): readonly [SchemaInference<t, i>, "?"];
|
|
75
163
|
or<r, ri>(def: SchemaInference<r, ri>): FluentType<t | r, i | ri>;
|
|
76
164
|
or<const def extends string>(def: def): FluentType<t | InferString<def>, i | InferString<def>>;
|
|
77
|
-
or<const def extends Record<string, unknown>>(def: def): FluentType<t | InferObjectLiteral<def>, i |
|
|
165
|
+
or<const def extends Record<string, unknown>>(def: def): FluentType<t | InferObjectLiteral<def>, i | InferObjectLiteralIn<def>>;
|
|
78
166
|
or(def: Def): FluentType<unknown>;
|
|
79
167
|
and<r, ri>(def: SchemaInference<r, ri>): FluentType<t & r, i & ri>;
|
|
80
|
-
and<const def extends Record<string, unknown>>(def: def): FluentType<t & InferObjectLiteral<def>, i &
|
|
168
|
+
and<const def extends Record<string, unknown>>(def: def): FluentType<t & InferObjectLiteral<def>, i & InferObjectLiteralIn<def>>;
|
|
81
169
|
and(def: Def): FluentType<unknown>;
|
|
82
170
|
equals(def: Def): boolean;
|
|
83
171
|
ifEquals(def: Def): FluentType<t, i> | undefined;
|
|
172
|
+
ifExtends(def: Def): FluentType<t, i> | undefined;
|
|
84
173
|
extends(def: Def): boolean;
|
|
85
174
|
overlaps(def: Def): boolean;
|
|
86
|
-
distribute<r>(mapper: (branch: FluentType<unknown>) => SchemaInference<r>): FluentType<r>;
|
|
175
|
+
distribute<r>(mapper: (branch: FluentType<unknown>) => SchemaInference<r>, reducer?: (branches: readonly SchemaInference<r>[]) => SchemaInference<unknown>): FluentType<r>;
|
|
87
176
|
select(kind: string): readonly SelectedNode[];
|
|
88
177
|
array(): FluentType<t[], i[]>;
|
|
89
178
|
atLeastLength(bound: number): FluentType<t, i>;
|
|
@@ -101,18 +190,19 @@ interface FluentMethods<t, i> {
|
|
|
101
190
|
nonNegative(): FluentType<t, i>;
|
|
102
191
|
nonPositive(): FluentType<t, i>;
|
|
103
192
|
matching(pattern: RegExp): FluentType<t, i>;
|
|
104
|
-
atOrAfter(bound: Date): FluentType<t, i>;
|
|
105
|
-
atOrBefore(bound: Date): FluentType<t, i>;
|
|
106
|
-
laterThan(bound: Date): FluentType<t, i>;
|
|
107
|
-
earlierThan(bound: Date): FluentType<t, i>;
|
|
108
|
-
pipe
|
|
193
|
+
atOrAfter(bound: Date | number): FluentType<t, i>;
|
|
194
|
+
atOrBefore(bound: Date | number): FluentType<t, i>;
|
|
195
|
+
laterThan(bound: Date | number): FluentType<t, i>;
|
|
196
|
+
earlierThan(bound: Date | number): FluentType<t, i>;
|
|
197
|
+
readonly pipe: PipeMethod<t, i>;
|
|
109
198
|
to<const def>(def: def): FluentType<InferDef<def>, i>;
|
|
110
199
|
filter<narrowed extends i>(fn: (data: i, ctx: NarrowContext) => data is narrowed): FluentType<t, narrowed>;
|
|
111
|
-
filter(fn: (data: i, ctx: NarrowContext) => boolean): FluentType<t, i>;
|
|
200
|
+
filter(fn: (data: i, ctx: NarrowContext) => boolean | OmpErrors): FluentType<t, i>;
|
|
112
201
|
narrow<narrowed extends t>(fn: (data: t, ctx: NarrowContext) => data is narrowed): FluentType<narrowed, i>;
|
|
113
|
-
narrow(fn: (data: t, ctx: NarrowContext) => boolean): FluentType<t, i>;
|
|
202
|
+
narrow(fn: (data: t, ctx: NarrowContext) => boolean | OmpErrors): FluentType<t, i>;
|
|
114
203
|
brand<const name extends string>(name: name): FluentType<Brand<t, name>, i>;
|
|
115
204
|
as<castTo>(): FluentType<castTo, i>;
|
|
205
|
+
readonly(): FluentType<Readonly<t>, i>;
|
|
116
206
|
extract<r, ri>(def: SchemaInference<r, ri>): FluentType<Extract<t, r>, Extract<i, ri>>;
|
|
117
207
|
extract<const def extends string>(def: def): FluentType<Extract<t, InferString<def>>, Extract<i, InferString<def>>>;
|
|
118
208
|
extract(def: Def): FluentType<unknown>;
|
|
@@ -122,29 +212,82 @@ interface FluentMethods<t, i> {
|
|
|
122
212
|
onUndeclaredKey(behavior: "ignore" | "reject" | "delete"): FluentType<t, i>;
|
|
123
213
|
onDeepUndeclaredKey(behavior: "ignore" | "reject" | "delete"): FluentType<t, i>;
|
|
124
214
|
}
|
|
215
|
+
interface PipeMethod<t, i> {
|
|
216
|
+
<r>(fn: (data: t, ctx: NarrowContext) => r): FluentType<Exclude<r, OmpErrors>, i>;
|
|
217
|
+
<r, ri>(schema: SchemaInference<r, ri>): FluentType<r, i>;
|
|
218
|
+
(...steps: readonly unknown[]): FluentType<unknown, i>;
|
|
219
|
+
readonly try: {
|
|
220
|
+
<r>(fn: (data: t, ctx: NarrowContext) => r): FluentType<Exclude<r, OmpErrors>, i>;
|
|
221
|
+
(...steps: readonly unknown[]): FluentType<unknown, i>;
|
|
222
|
+
};
|
|
223
|
+
}
|
|
125
224
|
type InputObject<i> = i extends object ? i : object;
|
|
126
225
|
interface ObjectMethods<t extends object, i> {
|
|
127
226
|
readonly props: readonly TypeProperty[];
|
|
128
227
|
map(mapper: (property: TypeProperty) => TypeProperty | readonly TypeProperty[]): FluentType<Record<PropertyKey, unknown>>;
|
|
129
228
|
keyof(): FluentType<Extract<keyof t, PropertyKey>, Extract<keyof InputObject<i>, PropertyKey>>;
|
|
130
|
-
get<
|
|
229
|
+
get<const path extends readonly PropertyKey[]>(...path: path): FluentType<unknown>;
|
|
131
230
|
pick<const keys extends readonly (keyof t)[]>(...keys: keys): FluentType<Pick<t, keys[number]>, Pick<InputObject<i>, Extract<keys[number], keyof InputObject<i>>>>;
|
|
132
231
|
omit<const keys extends readonly (keyof t)[]>(...keys: keys): FluentType<Omit<t, keys[number]>, Omit<InputObject<i>, Extract<keys[number], keyof InputObject<i>>>>;
|
|
133
232
|
partial(): FluentType<Partial<t>, Partial<InputObject<i>>>;
|
|
134
233
|
required(): FluentType<Required<t>, Required<InputObject<i>>>;
|
|
135
234
|
merge<r, ri>(def: SchemaInference<r, ri>): FluentType<MergeTypes<t, r>, MergeTypes<i, ri>>;
|
|
136
|
-
merge<const def extends Record<string, unknown>>(def: def): FluentType<MergeTypes<t, InferObjectLiteral<def>>, MergeTypes<i,
|
|
235
|
+
merge<const def extends Record<string, unknown>>(def: def): FluentType<MergeTypes<t, InferObjectLiteral<def>>, MergeTypes<i, InferObjectLiteralIn<def>>>;
|
|
137
236
|
merge(def: Def): FluentType<unknown>;
|
|
138
237
|
}
|
|
139
238
|
type ObjectMethodsFor<t, i> = [t] extends [never] ? unknown : [t] extends [readonly unknown[]] ? unknown : [t] extends [object] ? ObjectMethods<t & object, i> : unknown;
|
|
140
239
|
/** Callable schema with fluent methods specialized to its output and input. */
|
|
141
240
|
export type FluentType<t = unknown, i = t> = Type<t, i> & FluentMethods<t, i> & ObjectMethodsFor<t, i>;
|
|
241
|
+
type FnDefinition = Def | SchemaInference<unknown, unknown>;
|
|
242
|
+
/** Function returned by `type.fn`: arguments and an optional return are validated at every call. */
|
|
243
|
+
export type TypedFunction<parameters extends readonly unknown[] = readonly unknown[], returns = unknown, declaredReturns = returns> = ((...arguments_: parameters) => returns) & {
|
|
244
|
+
readonly params: FluentType<parameters>;
|
|
245
|
+
readonly returns: FluentType<declaredReturns>;
|
|
246
|
+
readonly expression: string;
|
|
247
|
+
readonly raw: (...arguments_: parameters) => returns;
|
|
248
|
+
};
|
|
249
|
+
type InferFnDefinition<definition> = definition extends SchemaInference<infer output, unknown> ? output : InferDef<definition>;
|
|
250
|
+
type InferFnParameters<definitions extends readonly unknown[], accumulator extends readonly unknown[] = []> = definitions extends readonly [infer head, ...infer tail] ? head extends ":" ? accumulator : InferFnParameters<tail, readonly [...accumulator, InferFnDefinition<head>]> : accumulator;
|
|
251
|
+
type InferFnReturn<definitions extends readonly unknown[], inferred> = definitions extends readonly [
|
|
252
|
+
...(readonly unknown[]),
|
|
253
|
+
":",
|
|
254
|
+
infer returns
|
|
255
|
+
] ? InferFnDefinition<returns> : inferred;
|
|
256
|
+
type DeclaredFnReturn<definitions extends readonly unknown[]> = definitions extends readonly [
|
|
257
|
+
...(readonly unknown[]),
|
|
258
|
+
":",
|
|
259
|
+
infer returns
|
|
260
|
+
] ? InferFnDefinition<returns> : unknown;
|
|
261
|
+
type FnFactory<definitions extends readonly FnDefinition[]> = <result>(implementation: (...arguments_: InferFnParameters<definitions>) => InferFnReturn<definitions, result>) => TypedFunction<InferFnParameters<definitions>, InferFnReturn<definitions, result>, DeclaredFnReturn<definitions>>;
|
|
262
|
+
/** Parses function parameter schemas and validates calls and declared returns. */
|
|
263
|
+
export interface FnParser {
|
|
264
|
+
<const definitions extends readonly FnDefinition[]>(...definitions: definitions): FnFactory<definitions>;
|
|
265
|
+
raw<const definitions extends readonly FnDefinition[]>(...definitions: definitions): FnFactory<definitions>;
|
|
266
|
+
}
|
|
142
267
|
/** Runtime constructor-like value used by ArkType-compatible `instanceof Type` checks. */
|
|
143
268
|
export declare const Type: () => void;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
269
|
+
export interface ConfigureSelector {
|
|
270
|
+
readonly kind?: string;
|
|
271
|
+
readonly where?: (node: {
|
|
272
|
+
readonly domain?: string;
|
|
273
|
+
readonly kind: string;
|
|
274
|
+
}) => boolean;
|
|
275
|
+
}
|
|
276
|
+
/** Callable runtime generic returned by `type("<t>", def)` and `type.generic(...)`. */
|
|
277
|
+
export type Generic = (...arguments_: readonly unknown[]) => BaseType;
|
|
278
|
+
/** Schema arguments passed to a callback-bodied runtime generic. */
|
|
279
|
+
export interface GenericArguments {
|
|
280
|
+
readonly [name: string]: BaseType;
|
|
281
|
+
}
|
|
282
|
+
export interface GenericBuilder {
|
|
283
|
+
(definition: (arguments_: GenericArguments) => unknown, hkt?: unknown): Generic;
|
|
284
|
+
(definition: unknown, hkt?: unknown): Generic;
|
|
285
|
+
}
|
|
286
|
+
export declare function type<const definition>(parameters: `<${string}>`, definition: definition): Generic;
|
|
147
287
|
export declare function type<const def>(def: def): FluentType<InferDef<def>, InferDefIn<def>>;
|
|
288
|
+
export declare function type<input, output>(def: SchemaInference<input> | string, operator: "=>", morph: (data: input, ctx: NarrowContext) => output): FluentType<Exclude<output, OmpErrors>, input>;
|
|
289
|
+
export declare function type<const input, const output>(def: input, operator: "|>", out: output): FluentType<InferDef<output>, InferDefIn<input>>;
|
|
290
|
+
export declare function type<const expression extends readonly unknown[]>(...definition: expression): FluentType<InferDef<expression>, InferDefIn<expression>>;
|
|
148
291
|
/** String keyword with a parser that morphs validated text to another output. */
|
|
149
292
|
export interface ParsedStringKeyword<parsed> extends FluentType<string> {
|
|
150
293
|
readonly parse: FluentType<parsed, string>;
|
|
@@ -224,66 +367,199 @@ export interface NumberKeyword extends FluentType<number> {
|
|
|
224
367
|
readonly integer: FluentType<number>;
|
|
225
368
|
}
|
|
226
369
|
type Constructed<ctor> = ctor extends abstract new (...args: never[]) => infer instance ? instance : never;
|
|
370
|
+
type MatchDefault<input = unknown, output = unknown> = "assert" | "never" | "reject" | ((input: input, ...args: readonly unknown[]) => output);
|
|
371
|
+
type MatchCaseOutput<cases> = {
|
|
372
|
+
[key in keyof cases]: cases[key] extends (...args: never[]) => infer output ? output : never;
|
|
373
|
+
}[keyof cases];
|
|
374
|
+
/** A finalized matcher. Like a schema, it returns structured errors unless finalized with `"assert"`. */
|
|
375
|
+
export type Matcher<input = unknown, output = unknown> = FluentType<output, input> & (<const value extends input>(value: value, ...args: readonly unknown[]) => output | OmpErrors);
|
|
376
|
+
/** Fluent first-match parser exposed as `match` and `type.match`. */
|
|
377
|
+
export interface MatchParser<input = unknown, output = never> {
|
|
378
|
+
<const cases extends Record<PropertyKey, unknown>>(cases: cases): MatchParser<input, output | MatchCaseOutput<cases>> | Matcher<input, output | MatchCaseOutput<cases>>;
|
|
379
|
+
case<const definition, result>(definition: definition, resolver: (value: InferDef<definition>, ...args: readonly unknown[]) => result): MatchParser<input, output | result>;
|
|
380
|
+
match<const cases extends Record<PropertyKey, unknown>>(cases: cases): MatchParser<input, output | MatchCaseOutput<cases>> | Matcher<input, output | MatchCaseOutput<cases>>;
|
|
381
|
+
default<const result>(fallback: MatchDefault<input, result>): Matcher<input, output | result>;
|
|
382
|
+
at<const key extends PropertyKey>(key: key): MatchParser<input, output>;
|
|
383
|
+
at<const key extends PropertyKey, cases extends Record<PropertyKey, unknown>>(key: key, cases: cases): MatchParser<input, output | MatchCaseOutput<cases>> | Matcher<input, output | MatchCaseOutput<cases>>;
|
|
384
|
+
strings<const cases extends Record<PropertyKey, unknown>>(cases: cases): MatchParser<input, output | MatchCaseOutput<cases>> | Matcher<input, output | MatchCaseOutput<cases>>;
|
|
385
|
+
in<narrowed>(): MatchParser<narrowed, output>;
|
|
386
|
+
in<const definition>(definition: definition): MatchParser<InferDef<definition>, output>;
|
|
387
|
+
}
|
|
388
|
+
/** Build a fluent first-match dispatcher from schema definitions. */
|
|
389
|
+
declare const matchBuilder: MatchParser;
|
|
390
|
+
export { matchBuilder as match };
|
|
391
|
+
/** Declares a schema output type while preserving its inferred input. */
|
|
392
|
+
export interface DeclaredParser<declared> {
|
|
393
|
+
type<const definition>(definition: definition): FluentType<declared, InferDefIn<definition>>;
|
|
394
|
+
}
|
|
395
|
+
/** Fix a schema's externally declared static type without changing its runtime validation. */
|
|
396
|
+
export declare function declare<declared, _options = {}>(): DeclaredParser<declared>;
|
|
227
397
|
export declare namespace type {
|
|
228
398
|
/** Error aggregate returned by failed validations (`result instanceof type.errors`). */
|
|
229
|
-
const errors: typeof OmpErrors;
|
|
230
|
-
type errors = OmpErrors;
|
|
399
|
+
export const errors: typeof OmpErrors;
|
|
400
|
+
export type errors = OmpErrors;
|
|
401
|
+
/** Build a union from zero or more definitions. */
|
|
402
|
+
export function or<const definitions extends readonly unknown[]>(...definitions: definitions): FluentType<NaryOrOutput<definitions>, NaryOrInput<definitions>>;
|
|
403
|
+
/** Build an intersection from zero or more definitions. */
|
|
404
|
+
export function and<const definitions extends readonly unknown[]>(...definitions: definitions): FluentType<NaryAndOutput<definitions>, NaryAndInput<definitions>>;
|
|
405
|
+
/** Right-biased object merge over zero or more definitions. */
|
|
406
|
+
export function merge<const definitions extends readonly unknown[]>(...definitions: definitions): FluentType<NaryMergeOutput<definitions>, NaryMergeInput<definitions>>;
|
|
407
|
+
/** Compose Types, definitions, and morph callbacks from left to right. */
|
|
408
|
+
export function pipe<const definitions extends readonly unknown[]>(...definitions: definitions): FluentType<NaryPipeOutput<definitions>, NaryPipeInput<definitions>>;
|
|
231
409
|
/** String validator and its refinement/morph keyword module. */
|
|
232
|
-
const string: StringKeyword;
|
|
410
|
+
export const string: StringKeyword;
|
|
233
411
|
/** Runtime parser keyword family. */
|
|
234
|
-
const parse: ParseKeyword;
|
|
412
|
+
export const parse: ParseKeyword;
|
|
235
413
|
/** Number validator with integer refinement. */
|
|
236
|
-
const number: NumberKeyword;
|
|
414
|
+
export const number: NumberKeyword;
|
|
415
|
+
/** Schema-valued key representing any non-negative integer array index. */
|
|
416
|
+
export const arrayIndex: FluentType<string, string>;
|
|
237
417
|
/** Boolean validator. */
|
|
238
|
-
const boolean: FluentType<boolean, boolean>;
|
|
418
|
+
export const boolean: FluentType<boolean, boolean>;
|
|
239
419
|
/** Bigint validator. */
|
|
240
|
-
const bigint: FluentType<bigint, bigint>;
|
|
420
|
+
export const bigint: FluentType<bigint, bigint>;
|
|
241
421
|
/** Symbol validator. */
|
|
242
|
-
const symbol: FluentType<symbol, symbol>;
|
|
422
|
+
export const symbol: FluentType<symbol, symbol>;
|
|
243
423
|
/** Non-null object validator. */
|
|
244
|
-
const object: FluentType<object, object>;
|
|
424
|
+
export const object: FluentType<object, object>;
|
|
245
425
|
/** Unknown validator. */
|
|
246
|
-
const unknown: FluentType<unknown, unknown>;
|
|
426
|
+
export const unknown: FluentType<unknown, unknown>;
|
|
247
427
|
/** Alias of the unknown validator. */
|
|
248
|
-
const any: FluentType<unknown, unknown>;
|
|
428
|
+
export const any: FluentType<unknown, unknown>;
|
|
249
429
|
/** Validator that rejects every value. */
|
|
250
|
-
const never: FluentType<never, never>;
|
|
430
|
+
export const never: FluentType<never, never>;
|
|
431
|
+
/** ArkType's built-in keyword namespace, including invokable utility generics. */
|
|
432
|
+
export const keywords: {
|
|
433
|
+
number: {
|
|
434
|
+
integer: FluentType<number, number>;
|
|
435
|
+
};
|
|
436
|
+
Map: FluentType<Map<unknown, unknown>, Map<unknown, unknown>>;
|
|
437
|
+
Set: FluentType<Set<unknown>, Set<unknown>>;
|
|
438
|
+
RegExp: FluentType<RegExp, RegExp>;
|
|
439
|
+
File: FluentType<File, File>;
|
|
440
|
+
Error: FluentType<Error, Error>;
|
|
441
|
+
Function: FluentType<Function, Function>;
|
|
442
|
+
Array: {
|
|
443
|
+
liftFrom<const definition>(definition: definition): FluentType<InferDef<definition>[], InferDefIn<definition> | InferDefIn<definition>[]>;
|
|
444
|
+
};
|
|
445
|
+
Record<const key, const value>(key: key, value: value): FluentType<Record<Extract<InferDef<key>, PropertyKey>, InferDef<value>>, Record<Extract<InferDefIn<key>, PropertyKey>, InferDefIn<value>>>;
|
|
446
|
+
Partial<const definition>(definition: definition): FluentType<Partial<InferDef<definition>>, Partial<InputObject<InferDefIn<definition>>>>;
|
|
447
|
+
Required<const definition>(definition: definition): FluentType<Required<InferDef<definition>>, Required<InputObject<InferDefIn<definition>>>>;
|
|
448
|
+
Pick<const definition, const keys extends readonly PropertyKey[]>(definition: definition, ...keys: keys): FluentType<Pick<InferDef<definition>, Extract<keys[number], keyof InferDef<definition>>>, Pick<InputObject<InferDefIn<definition>>, Extract<keys[number], keyof InputObject<InferDefIn<definition>>>>>;
|
|
449
|
+
Omit<const definition, const keys extends readonly PropertyKey[]>(definition: definition, ...keys: keys): FluentType<Omit<InferDef<definition>, Extract<keys[number], keyof InferDef<definition>>>, Omit<InputObject<InferDefIn<definition>>, Extract<keys[number], keyof InputObject<InferDefIn<definition>>>>>;
|
|
450
|
+
Merge<const left, const right>(left: left, right: right): FluentType<MergeTypes<InferDef<left>, InferDef<right>>, MergeTypes<InferDefIn<left>, InferDefIn<right>>>;
|
|
451
|
+
object: {
|
|
452
|
+
json: FluentType<unknown, unknown>;
|
|
453
|
+
};
|
|
454
|
+
unknown: {
|
|
455
|
+
any: FluentType<unknown, unknown>;
|
|
456
|
+
};
|
|
457
|
+
};
|
|
251
458
|
/** Date instance validator. */
|
|
252
|
-
const Date: FluentType<Date, Date>;
|
|
459
|
+
export const Date: FluentType<Date, Date>;
|
|
253
460
|
/** Validate instances of `ctor`. */
|
|
254
|
-
function instanceOf<const ctor extends Constructor>(ctor: ctor): FluentType<Constructed<ctor>>;
|
|
461
|
+
export function instanceOf<const ctor extends Constructor>(ctor: ctor): FluentType<Constructed<ctor>>;
|
|
255
462
|
/** Validate one exact unit value. */
|
|
256
|
-
function unit<const value>(value: value): FluentType<value>;
|
|
463
|
+
export function unit<const value>(value: value): FluentType<value>;
|
|
257
464
|
/** Union of literal values from a runtime array. */
|
|
258
|
-
function enumerated<const values extends readonly unknown[]>(...values: values): FluentType<values[number]>;
|
|
259
|
-
/**
|
|
260
|
-
function
|
|
465
|
+
export function enumerated<const values extends readonly unknown[]>(...values: values): FluentType<values[number]>;
|
|
466
|
+
/** Enumerate an enum-like object's forward values, excluding numeric reverse mappings. */
|
|
467
|
+
export function valueOf<const values extends Record<PropertyKey, unknown>>(values: values): FluentType<values[keyof values]>;
|
|
468
|
+
/** Fluent first-match dispatcher, also exported as standalone `match`. */
|
|
469
|
+
export const match: MatchParser;
|
|
261
470
|
/** Preserve a definition's literal type while authoring reusable modules. */
|
|
262
|
-
function define<const definition>(definition: definition): definition;
|
|
471
|
+
export function define<const definition>(definition: definition): definition;
|
|
472
|
+
/** Build a function whose arguments and optional declared return are validated. */
|
|
473
|
+
export const fn: FnParser;
|
|
474
|
+
/** Fix an externally declared static type while retaining runtime validation. */
|
|
475
|
+
export const declare: <declared, _options = {}>() => DeclaredParser<declared>;
|
|
263
476
|
/** Build a lazy named scope from aliases and recursive definitions. */
|
|
264
|
-
function scope(aliases: Record<string, unknown>, options?: ScopeOptions): TypeScope;
|
|
477
|
+
export function scope(aliases: Record<string, unknown>, options?: ScopeOptions): TypeScope;
|
|
265
478
|
/** Compile a named schema module whose definitions may reference each other. */
|
|
266
|
-
function module<const definitions extends Record<string, unknown>>(definitions: definitions): {
|
|
479
|
+
export function module<const definitions extends Record<string, unknown>>(definitions: definitions, options?: ScopeOptions): {
|
|
267
480
|
[name in keyof definitions]: Type<InferDef<definitions[name]>, InferDefIn<definitions[name]>>;
|
|
268
481
|
};
|
|
269
|
-
|
|
270
|
-
|
|
482
|
+
type GenericParameterSpec = string | readonly [name: string, constraint: unknown];
|
|
483
|
+
/** Build a generic directly from an angle-bracket declaration. */
|
|
484
|
+
export function generic<const definition>(parameters: `<${string}>`, definition: definition): Generic;
|
|
485
|
+
/** Build a curried generic from named, optionally constrained parameters. */
|
|
486
|
+
export function generic(...parameters: readonly GenericParameterSpec[]): GenericBuilder;
|
|
271
487
|
/** Untyped builder for runtime-assembled definitions. */
|
|
272
|
-
function raw(def: unknown): BaseType;
|
|
488
|
+
export function raw(def: unknown): BaseType;
|
|
489
|
+
export {};
|
|
273
490
|
}
|
|
274
491
|
export interface ScopeOptions {
|
|
275
492
|
jitless?: boolean;
|
|
493
|
+
clone?: false | ((input: unknown) => unknown);
|
|
494
|
+
divisor?: SchemaConfig;
|
|
276
495
|
}
|
|
277
496
|
/** Callable builder bound to one alias scope. */
|
|
278
|
-
export type ScopedBuilder = <const definition>(definition: definition) => FluentType<InferDef<definition>, InferDefIn<definition
|
|
279
|
-
/** Named schema scope with
|
|
497
|
+
export type ScopedBuilder = (<const definition>(definition: definition) => FluentType<InferDef<definition>, InferDefIn<definition>>) & typeof type;
|
|
498
|
+
/** Named schema scope with scoped parsing, imports, and bound module exports. */
|
|
280
499
|
export interface TypeScope {
|
|
281
500
|
readonly type: ScopedBuilder;
|
|
282
|
-
|
|
501
|
+
readonly match: MatchParser;
|
|
502
|
+
readonly json: Record<string, unknown>;
|
|
503
|
+
define<const definition>(definition: definition): definition;
|
|
504
|
+
resolve(name: string): BaseType;
|
|
505
|
+
import(...names: readonly string[]): Record<string, unknown>;
|
|
506
|
+
export(...names: readonly string[]): Record<string, BaseType>;
|
|
283
507
|
}
|
|
284
508
|
/** Build a scope whose aliases resolve lazily, including recursive cycles. */
|
|
285
509
|
export declare function scope(aliases: Record<string, unknown>, options?: ScopeOptions): TypeScope;
|
|
510
|
+
export declare namespace scope {
|
|
511
|
+
/** Preserve a scope definition's literal shape without constructing it. */
|
|
512
|
+
function define<const aliases>(definitions: aliases): aliases;
|
|
513
|
+
}
|
|
286
514
|
/** A schema whose output type is not statically known (`type.raw` results). */
|
|
287
515
|
export type BaseType = FluentType<unknown, unknown>;
|
|
516
|
+
/**
|
|
517
|
+
* Minimal structural constraint matching any omptype schema.
|
|
518
|
+
*
|
|
519
|
+
* `FluentType`'s recursive fluent surface makes `T extends FluentType<...>`
|
|
520
|
+
* checks descend until TypeScript's depth limiter reports spurious
|
|
521
|
+
* incompatibilities, and its invariant input parameter rejects concrete
|
|
522
|
+
* schemas outright. This interface exposes only the schema marker plus the
|
|
523
|
+
* members generic helpers commonly need — method syntax keeps parameter
|
|
524
|
+
* positions bivariant, and returns recurse shallowly through `AnyType`.
|
|
525
|
+
*/
|
|
526
|
+
export interface AnyType {
|
|
527
|
+
(data: unknown): unknown;
|
|
528
|
+
readonly [IR_BRAND]: true;
|
|
529
|
+
readonly ir: IR;
|
|
530
|
+
readonly infer: unknown;
|
|
531
|
+
readonly inferIn: unknown;
|
|
532
|
+
readonly hasDefault: boolean;
|
|
533
|
+
readonly description?: string;
|
|
534
|
+
run(data: unknown): unknown;
|
|
535
|
+
assert(data: unknown): unknown;
|
|
536
|
+
allows(data: unknown): boolean;
|
|
537
|
+
toJsonSchema(options?: ToJsonSchemaOptions): Record<string, unknown>;
|
|
538
|
+
describe(description: string): AnyType;
|
|
539
|
+
default(value: unknown): AnyType;
|
|
540
|
+
or(def: Def): AnyType;
|
|
541
|
+
and(def: Def): AnyType;
|
|
542
|
+
pipe(fn: (data: never, ctx: NarrowContext) => unknown): AnyType;
|
|
543
|
+
narrow(fn: (data: never, ctx: NarrowContext) => unknown): AnyType;
|
|
544
|
+
array(): AnyType;
|
|
545
|
+
}
|
|
546
|
+
declare const submoduleType: unique symbol;
|
|
547
|
+
type BoundAlias<value> = value extends Submodule<infer aliases> ? Submodule<aliases> : FluentType<value>;
|
|
548
|
+
/** Exported aliases from a scope, each bound to that scope's resolver. */
|
|
549
|
+
export type Module<aliases extends Record<string, unknown>> = {
|
|
550
|
+
readonly [name in keyof aliases]: BoundAlias<aliases[name]>;
|
|
551
|
+
};
|
|
552
|
+
/** A module nested under an alias rather than directly parseable as a schema. */
|
|
553
|
+
export type Submodule<aliases extends Record<string, unknown>> = {
|
|
554
|
+
readonly [submoduleType]?: aliases;
|
|
555
|
+
} & {
|
|
556
|
+
readonly [name in keyof aliases]: BoundAlias<aliases[name]>;
|
|
557
|
+
};
|
|
558
|
+
/** A selected module export whose schemas retain access to the full scope. */
|
|
559
|
+
export type BoundModule<exports extends Record<string, unknown>, _allAliases extends Record<string, unknown> = exports> = Module<exports>;
|
|
560
|
+
/** Type-level view of a named scope. */
|
|
561
|
+
export type Scope<aliases extends Record<string, unknown>> = TypeScope & {
|
|
562
|
+
readonly t: aliases;
|
|
563
|
+
};
|
|
288
564
|
/** `hasMorph` re-export for diagnostics/tooling. */
|
|
289
565
|
export { hasMorph };
|