@atscript/typescript 0.0.1

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.
@@ -0,0 +1,98 @@
1
+ import { TAtscriptPlugin } from '@atscript/core';
2
+
3
+ declare const tsPlugin: () => TAtscriptPlugin;
4
+
5
+ interface TError {
6
+ path: string;
7
+ message: string;
8
+ details?: TError[];
9
+ }
10
+ interface TValidatorOptions {
11
+ partial: boolean | 'deep';
12
+ unknwonProps: 'strip' | 'ignore' | 'error';
13
+ errorLimit: number;
14
+ }
15
+ declare class Validator {
16
+ protected readonly def: TAtscriptAnnotatedType;
17
+ protected opts: TValidatorOptions;
18
+ constructor(def: TAtscriptAnnotatedType, opts?: Partial<TValidatorOptions>);
19
+ errors: TError[];
20
+ protected stackErrors: TError[][];
21
+ protected stackPath: string[];
22
+ protected isLimitExceeded(): boolean;
23
+ protected push(name: string): void;
24
+ protected pop(saveErrors: boolean): TError[] | undefined;
25
+ protected clear(): void;
26
+ protected error(message: string, path?: string, details?: TError[]): void;
27
+ protected throw(): void;
28
+ validate(value: any, safe?: boolean): boolean;
29
+ protected _validate(def: TAtscriptAnnotatedType, value: any): boolean;
30
+ protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
31
+ protected validateIntersection(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
32
+ protected validateTuple(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
33
+ protected validateArray(def: TAtscriptAnnotatedType<TAtscriptTypeArray>, value: any): boolean;
34
+ protected validateObject(def: TAtscriptAnnotatedType<TAtscriptTypeObject>, value: any): boolean;
35
+ protected validatePrimitive(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: any): boolean;
36
+ protected validateString(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: string): boolean;
37
+ protected validateNumber(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: number): boolean;
38
+ }
39
+ declare class ValidatorError extends Error {
40
+ readonly errors: TError[];
41
+ name: string;
42
+ constructor(errors: TError[]);
43
+ }
44
+
45
+ interface TAtscriptTypeComplex {
46
+ kind: 'union' | 'intersection' | 'tuple';
47
+ items: TAtscriptAnnotatedType[];
48
+ tags: Set<AtscriptPrimitiveTags>;
49
+ }
50
+ interface TAtscriptTypeArray {
51
+ kind: 'array';
52
+ of: TAtscriptAnnotatedType;
53
+ tags: Set<AtscriptPrimitiveTags>;
54
+ }
55
+ interface TAtscriptTypeObject<K extends string = string> {
56
+ kind: 'object';
57
+ props: Map<K, TAtscriptAnnotatedType>;
58
+ tags: Set<AtscriptPrimitiveTags>;
59
+ }
60
+ interface TAtscriptTypeFinal {
61
+ kind: '';
62
+ designType: 'string' | 'number' | 'boolean' | 'undefined' | 'null' | 'object' | 'any' | 'never';
63
+ value?: string | number | boolean;
64
+ tags: Set<AtscriptPrimitiveTags>;
65
+ }
66
+ type TAtscriptTypeDef = TAtscriptTypeComplex | TAtscriptTypeFinal | TAtscriptTypeArray | TAtscriptTypeObject<string>;
67
+ interface TAtscriptAnnotatedType<T = TAtscriptTypeDef> {
68
+ __is_anscript_annotated_type: true;
69
+ type: T;
70
+ validator: (opts?: TValidatorOptions) => Validator;
71
+ metadata: TMetadataMap<AtscriptMetadata>;
72
+ optional?: boolean;
73
+ }
74
+ declare function isAnnotatedType(type: any): type is TAtscriptAnnotatedType;
75
+ type TKind = '' | 'array' | 'object' | 'union' | 'intersection' | 'tuple';
76
+ declare function defineAnnotatedType(_kind?: TKind, base?: any): {
77
+ $type: any;
78
+ $def: {
79
+ kind: TKind;
80
+ } & Omit<TAtscriptTypeComplex, "kind"> & Omit<TAtscriptTypeFinal, "kind"> & Omit<TAtscriptTypeArray, "kind"> & Omit<TAtscriptTypeObject<string>, "kind">;
81
+ $metadata: Map<string, unknown>;
82
+ _existingObject: TAtscriptAnnotatedType | undefined;
83
+ tags(...tags: string[]): any;
84
+ designType(value: TAtscriptTypeFinal["designType"]): any;
85
+ value(value: string | number | boolean): any;
86
+ of(value: TAtscriptAnnotatedType): any;
87
+ item(value: TAtscriptAnnotatedType): any;
88
+ prop(name: string, value: TAtscriptAnnotatedType): any;
89
+ optional(): any;
90
+ refTo(type: any, chain?: string[]): any;
91
+ annotate(key: string, value: any, asArray?: boolean): any;
92
+ };
93
+ interface TMetadataMap<O extends object> extends Map<keyof O, O[keyof O]> {
94
+ get<K extends keyof O>(key: K): O[K] | undefined;
95
+ set<K extends keyof O>(key: K, value: O[K]): this;
96
+ }
97
+
98
+ export { type TAtscriptAnnotatedType, type TAtscriptTypeArray, type TAtscriptTypeComplex, type TAtscriptTypeDef, type TAtscriptTypeFinal, type TAtscriptTypeObject, type TMetadataMap, type TValidatorOptions, Validator, ValidatorError, tsPlugin as default, defineAnnotatedType, isAnnotatedType };