@webergency-utils/typechecker 0.1.0
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 +263 -0
- package/dist/engine/generators.d.ts +23 -0
- package/dist/engine/generators.js +283 -0
- package/dist/engine/hoister.d.ts +2 -0
- package/dist/engine/hoister.js +106 -0
- package/dist/engine/resolver.d.ts +7 -0
- package/dist/engine/resolver.js +882 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +2 -0
- package/dist/runtime/tags/constraint.d.ts +61 -0
- package/dist/runtime/tags/constraint.js +1 -0
- package/dist/runtime/tags/format.d.ts +15 -0
- package/dist/runtime/tags/format.js +1 -0
- package/dist/runtime/tags/tag.d.ts +3 -0
- package/dist/runtime/tags/tag.js +1 -0
- package/dist/runtime/tags/transform.d.ts +24 -0
- package/dist/runtime/tags/transform.js +1 -0
- package/dist/runtime/tags.d.ts +44 -0
- package/dist/runtime/tags.js +12 -0
- package/dist/runtime/validators.d.ts +89 -0
- package/dist/runtime/validators.js +768 -0
- package/dist/transformer.d.ts +3 -0
- package/dist/transformer.js +134 -0
- package/package.json +62 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface IValidation<T> {
|
|
2
|
+
success: boolean;
|
|
3
|
+
data?: T;
|
|
4
|
+
errors?: any[];
|
|
5
|
+
}
|
|
6
|
+
export type ValidationMode = 'strict' | 'relaxed' | 'strip';
|
|
7
|
+
export interface ValidationOptions {
|
|
8
|
+
mode?: ValidationMode;
|
|
9
|
+
tryConvert?: boolean;
|
|
10
|
+
wrapArrays?: boolean;
|
|
11
|
+
schema?: any;
|
|
12
|
+
errorFactory?: (errors: any[]) => Error;
|
|
13
|
+
}
|
|
14
|
+
export declare function is<T>(input: unknown, options?: ValidationMode | ValidationOptions): input is T;
|
|
15
|
+
export declare function assert<T>(input: unknown, options?: ValidationMode | ValidationOptions): T;
|
|
16
|
+
export declare function assertGuard<T>(input: unknown, options?: ValidationMode | ValidationOptions): asserts input is T;
|
|
17
|
+
export declare function validate<T>(input: unknown, options?: ValidationMode | ValidationOptions): IValidation<T>;
|
|
18
|
+
export declare function jsonSchema<T>(): any;
|
|
19
|
+
export * from './runtime/validators.js';
|
|
20
|
+
export * from './runtime/tags.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type MinLength<N extends number, Msg extends string = string> = {
|
|
2
|
+
readonly __minLength: N;
|
|
3
|
+
readonly __minLength_message?: Msg;
|
|
4
|
+
};
|
|
5
|
+
export type MaxLength<N extends number, Msg extends string = string> = {
|
|
6
|
+
readonly __maxLength: N;
|
|
7
|
+
readonly __maxLength_message?: Msg;
|
|
8
|
+
};
|
|
9
|
+
export type Pattern<S extends string, Msg extends string = string> = {
|
|
10
|
+
readonly __pattern: S;
|
|
11
|
+
readonly __pattern_message?: Msg;
|
|
12
|
+
};
|
|
13
|
+
export type Format<S extends 'email' | 'uuid' | 'url' | 'ipv4' | 'ipv6' | 'date' | 'date-time' | 'byte' | 'password' | 'regex' | 'hostname' | 'idn-email' | 'idn-hostname' | 'iri' | 'iri-reference' | 'uri' | 'uri-reference' | 'uri-template' | 'time' | 'duration' | 'objectId', Msg extends string = string> = {
|
|
14
|
+
readonly __format: S;
|
|
15
|
+
readonly __format_message?: Msg;
|
|
16
|
+
};
|
|
17
|
+
export type Length<Min extends number, Max extends number> = MinLength<Min> & MaxLength<Max>;
|
|
18
|
+
export type Minimum<N extends number | bigint, Msg extends string = string> = {
|
|
19
|
+
readonly __minimum: N;
|
|
20
|
+
readonly __minimum_message?: Msg;
|
|
21
|
+
};
|
|
22
|
+
export type Maximum<N extends number | bigint, Msg extends string = string> = {
|
|
23
|
+
readonly __maximum: N;
|
|
24
|
+
readonly __maximum_message?: Msg;
|
|
25
|
+
};
|
|
26
|
+
export type ExclusiveMinimum<N extends number | bigint, Msg extends string = string> = {
|
|
27
|
+
readonly __exclusiveMinimum: N;
|
|
28
|
+
readonly __exclusiveMinimum_message?: Msg;
|
|
29
|
+
};
|
|
30
|
+
export type ExclusiveMaximum<N extends number | bigint, Msg extends string = string> = {
|
|
31
|
+
readonly __exclusiveMaximum: N;
|
|
32
|
+
readonly __exclusiveMaximum_message?: Msg;
|
|
33
|
+
};
|
|
34
|
+
export type MultipleOf<N extends number | bigint, Msg extends string = string> = {
|
|
35
|
+
readonly __multipleOf: N;
|
|
36
|
+
readonly __multipleOf_message?: Msg;
|
|
37
|
+
};
|
|
38
|
+
export type Range<Min extends number | bigint, Max extends number | bigint> = Minimum<Min> & Maximum<Max>;
|
|
39
|
+
export type MinItems<N extends number, Msg extends string = string> = {
|
|
40
|
+
readonly __minItems: N;
|
|
41
|
+
readonly __minItems_message?: Msg;
|
|
42
|
+
};
|
|
43
|
+
export type MaxItems<N extends number, Msg extends string = string> = {
|
|
44
|
+
readonly __maxItems: N;
|
|
45
|
+
readonly __maxItems_message?: Msg;
|
|
46
|
+
};
|
|
47
|
+
export type UniqueItems<Msg extends string = string> = {
|
|
48
|
+
readonly __uniqueItems: true;
|
|
49
|
+
readonly __uniqueItems_message?: Msg;
|
|
50
|
+
};
|
|
51
|
+
export type Custom<Fn extends (...args: any[]) => boolean, Msg extends string = string> = {
|
|
52
|
+
readonly __custom: Fn;
|
|
53
|
+
readonly __custom_message?: Msg;
|
|
54
|
+
};
|
|
55
|
+
export type Requires<Paths extends string | readonly string[], Msg extends string = string> = {
|
|
56
|
+
readonly __requires: Paths;
|
|
57
|
+
readonly __requires_message?: Msg;
|
|
58
|
+
};
|
|
59
|
+
export type Message<Msg extends string> = {
|
|
60
|
+
readonly __message: Msg;
|
|
61
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Format } from './constraint.js';
|
|
2
|
+
export type Email = Format<'email'>;
|
|
3
|
+
export type UUID = Format<'uuid'>;
|
|
4
|
+
export type URL = Format<'url'>;
|
|
5
|
+
export type IPv4 = Format<'ipv4'>;
|
|
6
|
+
export type IPv6 = Format<'ipv6'>;
|
|
7
|
+
export type Date = Format<'date'>;
|
|
8
|
+
export type DateTime = Format<'date-time'>;
|
|
9
|
+
export type Byte = Format<'byte'>;
|
|
10
|
+
export type Password = Format<'password'>;
|
|
11
|
+
export type Regex = Format<'regex'>;
|
|
12
|
+
export type Hostname = Format<'hostname'>;
|
|
13
|
+
export type Time = Format<'time'>;
|
|
14
|
+
export type Duration = Format<'duration'>;
|
|
15
|
+
export type ObjectId = Format<'objectId'>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type LowerCase = {
|
|
2
|
+
readonly __transform_lowercase: true;
|
|
3
|
+
};
|
|
4
|
+
export type UpperCase = {
|
|
5
|
+
readonly __transform_uppercase: true;
|
|
6
|
+
};
|
|
7
|
+
export type Trim = {
|
|
8
|
+
readonly __transform_trim: true;
|
|
9
|
+
};
|
|
10
|
+
export type Capitalize = {
|
|
11
|
+
readonly __transform_capitalize: true;
|
|
12
|
+
};
|
|
13
|
+
export type ToNumber = {
|
|
14
|
+
readonly __transform_tonumber: true;
|
|
15
|
+
};
|
|
16
|
+
export type ToBoolean = {
|
|
17
|
+
readonly __transform_toboolean: true;
|
|
18
|
+
};
|
|
19
|
+
export type ToDate = {
|
|
20
|
+
readonly __transform_todate: true;
|
|
21
|
+
};
|
|
22
|
+
export type Custom<Fn extends (val: any) => any> = {
|
|
23
|
+
readonly __transform_custom: Fn;
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Tags for AOT Typechecker
|
|
3
|
+
* These are used in intersection types to add constraints.
|
|
4
|
+
* Example: type Password = string & MinLength<8>;
|
|
5
|
+
*/
|
|
6
|
+
export * from './tags/constraint.js';
|
|
7
|
+
export * as constraint from './tags/constraint.js';
|
|
8
|
+
export * as tag from './tags/tag.js';
|
|
9
|
+
export * as format from './tags/format.js';
|
|
10
|
+
export * as transform from './tags/transform.js';
|
|
11
|
+
export interface ITypeConstraints {
|
|
12
|
+
minLength?: number;
|
|
13
|
+
maxLength?: number;
|
|
14
|
+
pattern?: string;
|
|
15
|
+
format?: string;
|
|
16
|
+
minimum?: number | bigint;
|
|
17
|
+
maximum?: number | bigint;
|
|
18
|
+
exclusiveMinimum?: number | bigint;
|
|
19
|
+
exclusiveMaximum?: number | bigint;
|
|
20
|
+
multipleOf?: number | bigint;
|
|
21
|
+
minItems?: number;
|
|
22
|
+
maxItems?: number;
|
|
23
|
+
uniqueItems?: boolean;
|
|
24
|
+
custom?: string;
|
|
25
|
+
default?: string | number | boolean | null;
|
|
26
|
+
}
|
|
27
|
+
type Split<S extends string, D extends string> = S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
|
|
28
|
+
type ModifierPathTuple<T, Path extends string[], Modifiers> = Path extends [infer Key, ...infer Rest] ? Key extends keyof T ? {
|
|
29
|
+
[P in keyof T]: P extends Key ? Rest extends string[] ? Rest['length'] extends 0 ? T[P] & Modifiers : ModifierPathTuple<Exclude<T[P], undefined>, Rest, Modifiers> | (undefined extends T[P] ? undefined : never) : T[P] : T[P];
|
|
30
|
+
} : T : T;
|
|
31
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
32
|
+
type LastOf<U> = UnionToIntersection<U extends any ? (f: U) => void : never> extends ((a: infer A) => void) ? A : never;
|
|
33
|
+
type Push<T extends any[], V> = [...T, V];
|
|
34
|
+
type TuplifyUnion<U, L = LastOf<U>> = [
|
|
35
|
+
U
|
|
36
|
+
] extends [never] ? [] : Push<TuplifyUnion<Exclude<U, L>>, L>;
|
|
37
|
+
type Entries<M> = {
|
|
38
|
+
[K in keyof M]: [K, M[K]];
|
|
39
|
+
}[keyof M];
|
|
40
|
+
type ApplyModifiers<T, EntriesList extends any[]> = EntriesList extends [[infer Path, infer Modifiers], ...infer Rest] ? Path extends string ? ApplyModifiers<ModifierPathTuple<T, Split<Path, '.'>, Modifiers>, Rest> : T : T;
|
|
41
|
+
/**
|
|
42
|
+
* Utility type to decorate nested properties of a type with validation/transform modifiers without retyping the structure.
|
|
43
|
+
*/
|
|
44
|
+
export type WithModifiers<T, M> = ApplyModifiers<T, TuplifyUnion<Entries<M>>>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Tags for AOT Typechecker
|
|
3
|
+
* These are used in intersection types to add constraints.
|
|
4
|
+
* Example: type Password = string & MinLength<8>;
|
|
5
|
+
*/
|
|
6
|
+
// Export root-level tags for backward compatibility
|
|
7
|
+
export * from './tags/constraint.js';
|
|
8
|
+
// Export namespace-like modules
|
|
9
|
+
export * as constraint from './tags/constraint.js';
|
|
10
|
+
export * as tag from './tags/tag.js';
|
|
11
|
+
export * as format from './tags/format.js';
|
|
12
|
+
export * as transform from './tags/transform.js';
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export type ValidationMode = 'strict' | 'relaxed' | 'strip';
|
|
2
|
+
export interface IValidationError {
|
|
3
|
+
path: string;
|
|
4
|
+
value: any;
|
|
5
|
+
error: string;
|
|
6
|
+
}
|
|
7
|
+
export interface ValidationContext {
|
|
8
|
+
success: boolean;
|
|
9
|
+
errors: IValidationError[];
|
|
10
|
+
mode: ValidationMode;
|
|
11
|
+
tryConvert?: boolean;
|
|
12
|
+
wrapArrays?: boolean;
|
|
13
|
+
root?: any;
|
|
14
|
+
}
|
|
15
|
+
export interface ValidationOptions {
|
|
16
|
+
mode?: ValidationMode;
|
|
17
|
+
tryConvert?: boolean;
|
|
18
|
+
wrapArrays?: boolean;
|
|
19
|
+
schema?: any;
|
|
20
|
+
errorFactory?: (errors: IValidationError[]) => Error;
|
|
21
|
+
}
|
|
22
|
+
export declare const validators: {
|
|
23
|
+
string: (v: any, path: string, ctx: ValidationContext) => any;
|
|
24
|
+
number: (v: any, path: string, ctx: ValidationContext) => any;
|
|
25
|
+
bigint: (v: any, path: string, ctx: ValidationContext) => any;
|
|
26
|
+
boolean: (v: any, path: string, ctx: ValidationContext) => any;
|
|
27
|
+
date: (v: any, path: string, ctx: ValidationContext) => any;
|
|
28
|
+
regexp: (v: any, path: string, ctx: ValidationContext) => any;
|
|
29
|
+
null: (v: any, path: string, ctx: ValidationContext) => null;
|
|
30
|
+
undefined: (v: any, path: string, ctx: ValidationContext) => undefined;
|
|
31
|
+
literal: (v: any, path: string, ctx: ValidationContext, expected: any) => any;
|
|
32
|
+
array: (v: any, path: string, ctx: ValidationContext, childValidator: Function) => any;
|
|
33
|
+
props: (v: any, data: any, path: string, ctx: ValidationContext, props: [string, boolean, Function][]) => void;
|
|
34
|
+
object: (v: any, path: string, ctx: ValidationContext, allowedKeys?: string[], expected?: string) => boolean;
|
|
35
|
+
templateLiteral: (v: any, path: string, ctx: ValidationContext, regex: RegExp, expected: string) => any;
|
|
36
|
+
minLength: (v: string, path: string, ctx: ValidationContext, min: number, message?: string) => string;
|
|
37
|
+
maxLength: (v: string, path: string, ctx: ValidationContext, max: number, message?: string) => string;
|
|
38
|
+
minimum: (v: number | bigint, path: string, ctx: ValidationContext, min: number | bigint, message?: string) => number | bigint;
|
|
39
|
+
maximum: (v: number | bigint, path: string, ctx: ValidationContext, max: number | bigint, message?: string) => number | bigint;
|
|
40
|
+
exclusiveMinimum: (v: number | bigint, path: string, ctx: ValidationContext, min: number | bigint, message?: string) => number | bigint;
|
|
41
|
+
exclusiveMaximum: (v: number | bigint, path: string, ctx: ValidationContext, max: number | bigint, message?: string) => number | bigint;
|
|
42
|
+
multipleOf: (v: number | bigint, path: string, ctx: ValidationContext, n: number | bigint, message?: string) => number | bigint;
|
|
43
|
+
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) => string;
|
|
45
|
+
minItems: (v: any[], path: string, ctx: ValidationContext, min: number, message?: string) => any[];
|
|
46
|
+
maxItems: (v: any[], path: string, ctx: ValidationContext, max: number, message?: string) => any[];
|
|
47
|
+
uniqueItems: (v: any[], path: string, ctx: ValidationContext, message?: string) => any[];
|
|
48
|
+
custom: (v: any, path: string, ctx: ValidationContext, fn: Function, message?: string) => any;
|
|
49
|
+
union: (v: any, path: string, ctx: ValidationContext, checks: Function[], expected?: string) => any;
|
|
50
|
+
tuple: (v: any, path: string, ctx: ValidationContext, checks: Function[]) => any;
|
|
51
|
+
any: (v: any) => any;
|
|
52
|
+
requires: (v: any, path: string, ctx: ValidationContext, reqs: string[], message?: string) => any;
|
|
53
|
+
record: (v: any, path: string, ctx: ValidationContext, childValidator: Function) => any;
|
|
54
|
+
set: (v: any, path: string, ctx: ValidationContext, childValidator: Function, message?: string) => any;
|
|
55
|
+
map: (v: any, path: string, ctx: ValidationContext, keyValidator: Function, valueValidator: Function, message?: string) => any;
|
|
56
|
+
};
|
|
57
|
+
export declare class MetadataStoreClass {
|
|
58
|
+
private validators;
|
|
59
|
+
private schemas;
|
|
60
|
+
private compiledSchemas;
|
|
61
|
+
registerValidator(hash: string, validator: Function): void;
|
|
62
|
+
getValidator(hash: string): Function;
|
|
63
|
+
registerSchema(hash: string, schema: any): void;
|
|
64
|
+
getSchema(hash: string): any;
|
|
65
|
+
getOrCompileSchema(schema: any): Function;
|
|
66
|
+
is(validator: Function, value: any, options?: ValidationMode | ValidationOptions): boolean;
|
|
67
|
+
assert(validator: Function, value: any, options?: ValidationMode | ValidationOptions): any;
|
|
68
|
+
validate(validator: Function, value: any, options?: ValidationMode | ValidationOptions): {
|
|
69
|
+
success: boolean;
|
|
70
|
+
errors: IValidationError[];
|
|
71
|
+
data: any;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export declare function groupErrorsByPath(errors: IValidationError[]): Record<string, {
|
|
75
|
+
value: any;
|
|
76
|
+
errors: string[];
|
|
77
|
+
}>;
|
|
78
|
+
export declare const MetadataStore: MetadataStoreClass;
|
|
79
|
+
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
|
+
}[];
|
|
86
|
+
export declare class ZodLikeError extends Error {
|
|
87
|
+
issues: any[];
|
|
88
|
+
constructor(errors: IValidationError[]);
|
|
89
|
+
}
|