@webergency-utils/typechecker 0.1.10 → 0.2.2
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 +169 -47
- package/dist/engine/generators.d.ts +2 -1
- package/dist/engine/generators.js +37 -11
- package/dist/engine/resolver.js +279 -68
- package/dist/engine/staticAsserts.js +2 -1
- package/dist/index.d.ts +17 -13
- package/dist/index.js +35 -0
- package/dist/runtime/tags/format.d.ts +7 -0
- package/dist/runtime/tags/tag.d.ts +1 -1
- package/dist/runtime/tags.d.ts +1 -1
- package/dist/runtime/validators.d.ts +70 -16
- package/dist/runtime/validators.js +935 -219
- package/dist/transformer.js +37 -58
- package/package.json +12 -3
|
@@ -1,29 +1,80 @@
|
|
|
1
|
+
/** Controls unknown object keys only — not coercion. Use `from` for conversion. */
|
|
1
2
|
export type ValidationMode = 'strict' | 'relaxed' | 'strip';
|
|
2
3
|
export interface IValidationError {
|
|
3
4
|
path: string;
|
|
4
5
|
value: any;
|
|
5
6
|
error: string;
|
|
7
|
+
/** Nested failures (e.g. per-arm errors for a failed union). */
|
|
8
|
+
issues?: IValidationError[];
|
|
6
9
|
}
|
|
10
|
+
/** Expected runtime kind for custom `from` — a dispatch tag, not `typeof` / a TS type. */
|
|
11
|
+
export type CoercionKind = 'string' | 'number' | 'boolean' | 'bigint' | 'function' | 'symbol' | 'never' | 'Date' | 'RegExp' | 'Set' | 'Map' | 'Array' | 'Object' | 'instance' | 'null' | 'undefined' | 'tuple' | 'literal';
|
|
12
|
+
/** Shared context for `constraint.Custom` and custom `from` callbacks. */
|
|
13
|
+
export interface PathContext {
|
|
14
|
+
/** Nearest named property; for `[n]` leaves, the closest named segment above. */
|
|
15
|
+
key: string;
|
|
16
|
+
path: string;
|
|
17
|
+
parent: any;
|
|
18
|
+
root: any;
|
|
19
|
+
/** Set when the leaf path segment is an array index. */
|
|
20
|
+
index?: number;
|
|
21
|
+
}
|
|
22
|
+
export type FromCoercionContext = PathContext & {
|
|
23
|
+
kind: CoercionKind;
|
|
24
|
+
};
|
|
25
|
+
type FromOption = 'json' | 'query' | ((val: any, ctx: FromCoercionContext) => any);
|
|
7
26
|
export interface ValidationContext {
|
|
8
27
|
success: boolean;
|
|
9
28
|
errors: IValidationError[];
|
|
10
29
|
mode: ValidationMode;
|
|
11
|
-
|
|
12
|
-
|
|
30
|
+
from?: FromOption;
|
|
31
|
+
mutate?: boolean;
|
|
13
32
|
root?: any;
|
|
14
33
|
}
|
|
15
|
-
|
|
34
|
+
/** Options for `is` / `isSchema`. Always mutate; no `mutate` / `errorFactory`. */
|
|
35
|
+
export interface GuardOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Unknown-key policy for closed objects (default `'strict'`).
|
|
38
|
+
* - `'strict'` — reject properties not in the type/schema
|
|
39
|
+
* - `'relaxed'` — allow and keep unknown properties (does **not** coerce values)
|
|
40
|
+
* - `'strip'` — drop unknown properties from the result (in place when mutating)
|
|
41
|
+
*
|
|
42
|
+
* Coercion / revival is controlled only by `from`, never by `mode`.
|
|
43
|
+
*/
|
|
16
44
|
mode?: ValidationMode;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
45
|
+
from?: FromOption;
|
|
46
|
+
}
|
|
47
|
+
/** Options for `assertGuard` / `assertGuardSchema`. */
|
|
48
|
+
export interface AssertGuardOptions extends GuardOptions {
|
|
49
|
+
errorFactory?: (errors: IValidationError[]) => Error;
|
|
50
|
+
}
|
|
51
|
+
/** Options for `validate` / `validateSchema`. */
|
|
52
|
+
export interface ValidationOptions extends GuardOptions {
|
|
53
|
+
/** `true`: always write onto the input. `false` (default): always allocate new containers. */
|
|
54
|
+
mutate?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/** Options for `assert` / `assertSchema`. */
|
|
57
|
+
export interface AssertOptions extends ValidationOptions {
|
|
20
58
|
errorFactory?: (errors: IValidationError[]) => Error;
|
|
21
59
|
}
|
|
60
|
+
/** Query-style number coercion — shared by `from: 'query'` and `transform.ToNumber`. */
|
|
61
|
+
export declare function coerceQueryNumber(v: any): any;
|
|
62
|
+
/** Query-style boolean coercion — shared by `from: 'query'` and `transform.ToBoolean`. */
|
|
63
|
+
export declare function coerceQueryBoolean(v: any): any;
|
|
64
|
+
/** JSON-wire Date revival (ISO / date-parseable strings only). */
|
|
65
|
+
export declare function coerceJsonDate(v: any): any;
|
|
66
|
+
/** Query-style Date coercion — shared by `from: 'query'` and `transform.ToDate`. */
|
|
67
|
+
export declare function coerceQueryDate(v: any): any;
|
|
22
68
|
export declare const validators: {
|
|
69
|
+
coerceQueryNumber: typeof coerceQueryNumber;
|
|
70
|
+
coerceQueryBoolean: typeof coerceQueryBoolean;
|
|
71
|
+
coerceQueryDate: typeof coerceQueryDate;
|
|
72
|
+
coerceJsonDate: typeof coerceJsonDate;
|
|
23
73
|
string: (v: any, path: string, ctx: ValidationContext) => any;
|
|
24
74
|
number: (v: any, path: string, ctx: ValidationContext) => any;
|
|
25
75
|
bigint: (v: any, path: string, ctx: ValidationContext) => any;
|
|
26
76
|
boolean: (v: any, path: string, ctx: ValidationContext) => any;
|
|
77
|
+
function: (v: any, path: string, ctx: ValidationContext) => any;
|
|
27
78
|
date: (v: any, path: string, ctx: ValidationContext) => any;
|
|
28
79
|
regexp: (v: any, path: string, ctx: ValidationContext) => any;
|
|
29
80
|
null: (v: any, path: string, ctx: ValidationContext) => null;
|
|
@@ -31,7 +82,10 @@ export declare const validators: {
|
|
|
31
82
|
literal: (v: any, path: string, ctx: ValidationContext, expected: any) => any;
|
|
32
83
|
array: (v: any, path: string, ctx: ValidationContext, childValidator: Function) => any;
|
|
33
84
|
props: (v: any, data: any, path: string, ctx: ValidationContext, props: [string, boolean, Function][]) => void;
|
|
34
|
-
|
|
85
|
+
objectShell: (v: any, ctx: ValidationContext) => any;
|
|
86
|
+
stripExtras: (data: any, ctx: ValidationContext, allowedKeys?: string[]) => any;
|
|
87
|
+
additionalProps: (v: any, data: any, path: string, ctx: ValidationContext, knownKeys: string[], childValidator: Function) => void;
|
|
88
|
+
object: (v: any, path: string, ctx: ValidationContext, allowedKeys?: string[], expected?: string) => any;
|
|
35
89
|
templateLiteral: (v: any, path: string, ctx: ValidationContext, regex: RegExp, expected: string) => any;
|
|
36
90
|
minLength: (v: string, path: string, ctx: ValidationContext, min: number, message?: string) => string;
|
|
37
91
|
maxLength: (v: string, path: string, ctx: ValidationContext, max: number, message?: string) => string;
|
|
@@ -41,7 +95,7 @@ export declare const validators: {
|
|
|
41
95
|
exclusiveMaximum: (v: number | bigint, path: string, ctx: ValidationContext, max: number | bigint, message?: string) => number | bigint;
|
|
42
96
|
multipleOf: (v: number | bigint, path: string, ctx: ValidationContext, n: number | bigint, message?: string) => number | bigint;
|
|
43
97
|
pattern: (v: string, path: string, ctx: ValidationContext, regex: RegExp, expected: string, message?: string) => string;
|
|
44
|
-
format: (v: string, path: string, ctx: ValidationContext, format: string, message?: string) =>
|
|
98
|
+
format: (v: string, path: string, ctx: ValidationContext, format: string, message?: string) => any;
|
|
45
99
|
minItems: (v: any[], path: string, ctx: ValidationContext, min: number, message?: string) => any[];
|
|
46
100
|
maxItems: (v: any[], path: string, ctx: ValidationContext, max: number, message?: string) => any[];
|
|
47
101
|
uniqueItems: (v: any[], path: string, ctx: ValidationContext, message?: string) => any[];
|
|
@@ -49,6 +103,9 @@ export declare const validators: {
|
|
|
49
103
|
union: (v: any, path: string, ctx: ValidationContext, checks: Function[], expected?: string) => any;
|
|
50
104
|
tuple: (v: any, path: string, ctx: ValidationContext, checks: Function[]) => any;
|
|
51
105
|
any: (v: any) => any;
|
|
106
|
+
never: (v: any, path: string, ctx: ValidationContext) => any;
|
|
107
|
+
symbol: (v: any, path: string, ctx: ValidationContext) => any;
|
|
108
|
+
instanceOf: (v: any, path: string, ctx: ValidationContext, typeName: string) => any;
|
|
52
109
|
requires: (v: any, path: string, ctx: ValidationContext, reqs: string[], message?: string) => any;
|
|
53
110
|
record: (v: any, path: string, ctx: ValidationContext, childValidator: Function) => any;
|
|
54
111
|
set: (v: any, path: string, ctx: ValidationContext, childValidator: Function, message?: string) => any;
|
|
@@ -63,8 +120,9 @@ export declare class MetadataStoreClass {
|
|
|
63
120
|
registerSchema(hash: string, schema: any): void;
|
|
64
121
|
getSchema(hash: string): any;
|
|
65
122
|
getOrCompileSchema(schema: any): Function;
|
|
66
|
-
is(validator: Function, value: any, options?: ValidationMode |
|
|
67
|
-
assert(validator: Function, value: any, options?: ValidationMode |
|
|
123
|
+
is(validator: Function, value: any, options?: ValidationMode | GuardOptions): boolean;
|
|
124
|
+
assert(validator: Function, value: any, options?: ValidationMode | AssertOptions): any;
|
|
125
|
+
assertGuard(validator: Function, value: any, options?: ValidationMode | AssertGuardOptions): void;
|
|
68
126
|
validate(validator: Function, value: any, options?: ValidationMode | ValidationOptions): {
|
|
69
127
|
success: boolean;
|
|
70
128
|
errors: IValidationError[];
|
|
@@ -77,13 +135,9 @@ export declare function groupErrorsByPath(errors: IValidationError[]): Record<st
|
|
|
77
135
|
}>;
|
|
78
136
|
export declare const MetadataStore: MetadataStoreClass;
|
|
79
137
|
export declare function compileSchema(schema: any): (v: any, path: string, ctx: any) => any;
|
|
80
|
-
export declare function toZodIssues(errors: IValidationError[]):
|
|
81
|
-
code: string;
|
|
82
|
-
path: (string | number)[];
|
|
83
|
-
message: string;
|
|
84
|
-
received: any;
|
|
85
|
-
}[];
|
|
138
|
+
export declare function toZodIssues(errors: IValidationError[]): any[];
|
|
86
139
|
export declare class ZodLikeError extends Error {
|
|
87
140
|
issues: any[];
|
|
88
141
|
constructor(errors: IValidationError[]);
|
|
89
142
|
}
|
|
143
|
+
export {};
|