@atscript/typescript 0.2.3 → 0.2.5
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/dist/cli.cjs +233 -32
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +29 -1
- package/dist/index.mjs +2 -2
- package/dist/{plugin-DhesoKce.mjs → plugin-Bx7j-vWf.mjs} +77 -23
- package/dist/{plugin-oUSsb3Ri.cjs → plugin-JJ9MJ6AF.cjs} +80 -26
- package/dist/realm-runtime.js +1629 -385
- package/dist/test-utils.cjs +3 -3
- package/dist/test-utils.d.ts +355 -152
- package/dist/test-utils.mjs +2 -2
- package/dist/{unique-name-hg1CyPIs.mjs → user-text-BOf56N4d.mjs} +765 -192
- package/dist/{unique-name-xWd9uO-k.cjs → user-text-Dl8FNKLL.cjs} +848 -191
- package/dist/utils.cjs +858 -228
- package/dist/utils.d.ts +436 -32
- package/dist/utils.mjs +755 -137
- package/package.json +5 -5
package/dist/test-utils.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
3
|
-
const
|
|
2
|
+
const require_plugin = require('./plugin-JJ9MJ6AF.cjs');
|
|
3
|
+
const require_user_text = require('./user-text-Dl8FNKLL.cjs');
|
|
4
4
|
let path = require("path");
|
|
5
5
|
path = require_plugin.__toESM(path);
|
|
6
6
|
let _atscript_core = require("@atscript/core");
|
|
@@ -38,7 +38,7 @@ let fs = require("fs");
|
|
|
38
38
|
* Reference validator for parity tests: a subclass never takes the allocation-free pre-check
|
|
39
39
|
* (interpreted or compiled) nor the literal-union shortcut, so it always runs the full
|
|
40
40
|
* error-collecting walk.
|
|
41
|
-
*/ var WalkValidator = class extends
|
|
41
|
+
*/ var WalkValidator = class extends require_user_text.Validator {};
|
|
42
42
|
/** A seeded random number generator (mulberry32) returning values in `[0, 1)`. */ function rng(seed) {
|
|
43
43
|
return () => {
|
|
44
44
|
seed = seed + 1831565813 | 0;
|
package/dist/test-utils.d.ts
CHANGED
|
@@ -1,166 +1,138 @@
|
|
|
1
1
|
import { TAtscriptPlugin } from '@atscript/core';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Validator error codes, their default English templates and the translation helpers.
|
|
5
|
+
*
|
|
6
|
+
* The validator is locale-agnostic: every error it reports carries a stable `code` and the
|
|
7
|
+
* `params` its message needs, and its `message` is the default English text (the code's
|
|
8
|
+
* template filled with the params) or the app's own text from an annotation (`custom: true`).
|
|
9
|
+
* Translate after the fact with {@link localizeErrors}, or per validator with the `translate`
|
|
10
|
+
* option.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Default English templates of the built-in error codes. `{name}` is replaced with
|
|
14
|
+
* `params.name` (see {@link formatValidatorMessage}).
|
|
15
|
+
*/
|
|
16
|
+
declare const VALIDATOR_MESSAGES: Readonly<{
|
|
17
|
+
/** A required string is empty (or whitespace only), or a required optional field is `null`. */
|
|
18
|
+
readonly REQUIRED: "Must not be empty";
|
|
19
|
+
/** A required boolean (`@meta.required` on a boolean) is not `true`. */
|
|
20
|
+
readonly REQUIRED_CHECKED: "Must be checked";
|
|
21
|
+
/** Wrong primitive type. `{ expected, actual }`: type names. */
|
|
22
|
+
readonly TYPE: "Expected {expected}, got {actual}";
|
|
23
|
+
/** Not an array. `{ actual }`: the value's type name. */
|
|
24
|
+
readonly TYPE_ARRAY: "Expected array";
|
|
25
|
+
/** Not an object. `{ actual }`: the value's type name. */
|
|
26
|
+
readonly TYPE_OBJECT: "Expected object";
|
|
27
|
+
/** A decimal that is not a string. `{ actual }`: the value's type name. */
|
|
28
|
+
readonly TYPE_DECIMAL: "Expected string (decimal), got {actual}";
|
|
29
|
+
/** `NaN` or an infinity. `{ actual }`: the value as text. */
|
|
30
|
+
readonly NOT_FINITE: "Expected finite number, got {actual}";
|
|
31
|
+
/** A decimal string in the wrong format. `{ value }`: the string. */
|
|
32
|
+
readonly DECIMAL_FORMAT: "Invalid decimal format: \"{value}\"";
|
|
33
|
+
/** A `never` type. */
|
|
34
|
+
readonly NEVER: "This type is impossible, must be an internal problem";
|
|
35
|
+
/** Not the literal value. `{ expected, actual }`: the literal, the value as text. */
|
|
36
|
+
readonly LITERAL: "Expected {expected}, got {actual}";
|
|
37
|
+
/** No union branch matches. `{ expected }`: the branch kinds (`[string(0)], [number(1)]`). */
|
|
38
|
+
readonly UNION: "Value does not match any of the allowed types: {expected}";
|
|
14
39
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* - `true` — Import `createDataFromAnnotatedType`, create example data on each call.
|
|
40
|
+
* The discriminator of an object union holds no branch's value. Reported at the discriminator
|
|
41
|
+
* property. `{ property, allowed, actual }`: its name, the allowed values, the value as text.
|
|
18
42
|
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
43
|
+
readonly UNION_DISCRIMINATOR: "Expected one of {allowed}, got {actual}";
|
|
44
|
+
/** A property the type does not declare (`unknownProps: 'error'`). `{ property }`. */
|
|
45
|
+
readonly UNEXPECTED_PROPERTY: "Unexpected property";
|
|
46
|
+
/** A tuple of the wrong length. `{ length }`: the expected length. */
|
|
47
|
+
readonly TUPLE_LENGTH: "Expected array of length {length}";
|
|
48
|
+
/** `@expect.minLength` on a string. `{ min, actual }`: lengths. */
|
|
49
|
+
readonly MIN_LENGTH: "Expected minimum length of {min} characters, got {actual} characters";
|
|
50
|
+
/** `@expect.maxLength` on a string. `{ max, actual }`: lengths. */
|
|
51
|
+
readonly MAX_LENGTH: "Expected maximum length of {max} characters, got {actual} characters";
|
|
52
|
+
/** `@expect.minLength` on an array. `{ min, actual }`: item counts. */
|
|
53
|
+
readonly MIN_ITEMS: "Expected minimum length of {min} items, got {actual} items";
|
|
54
|
+
/** `@expect.maxLength` on an array. `{ max, actual }`: item counts. */
|
|
55
|
+
readonly MAX_ITEMS: "Expected maximum length of {max} items, got {actual} items";
|
|
56
|
+
/** `@expect.array.uniqueItems`: a duplicate item (reported at its index). */
|
|
57
|
+
readonly UNIQUE_ITEMS: "Duplicate items are not allowed";
|
|
58
|
+
/** `@expect.minKeys`. `{ min, actual }`: key counts. */
|
|
59
|
+
readonly MIN_KEYS: "Expected at least {min} properties, got {actual}";
|
|
60
|
+
/** `@expect.maxKeys`. `{ max, actual }`: key counts. */
|
|
61
|
+
readonly MAX_KEYS: "Expected at most {max} properties, got {actual}";
|
|
62
|
+
/** `@expect.pattern` without a message. `{ pattern, flags }`. */
|
|
63
|
+
readonly PATTERN: "Value is expected to match pattern \"{pattern}\"";
|
|
64
|
+
/** `@expect.int`. `{ actual }`: the value. */
|
|
65
|
+
readonly INT: "Expected integer, got {actual}";
|
|
66
|
+
/** `@expect.min`. `{ min, actual }`. */
|
|
67
|
+
readonly MIN: "Expected minimum {min}, got {actual}";
|
|
68
|
+
/** `@expect.max`. `{ max, actual }`. */
|
|
69
|
+
readonly MAX: "Expected maximum {max}, got {actual}";
|
|
70
|
+
/** `@expect.exclusiveMin`. `{ min, actual }`. */
|
|
71
|
+
readonly EXCLUSIVE_MIN: "Expected greater than {min}, got {actual}";
|
|
72
|
+
/** `@expect.exclusiveMax`. `{ max, actual }`. */
|
|
73
|
+
readonly EXCLUSIVE_MAX: "Expected less than {max}, got {actual}";
|
|
74
|
+
/** `string.email`. */
|
|
75
|
+
readonly EMAIL: "Invalid email format.";
|
|
76
|
+
/** `string.phone`. */
|
|
77
|
+
readonly PHONE: "Invalid phone number format.";
|
|
78
|
+
/** `string.date`. */
|
|
79
|
+
readonly DATE: "Invalid date format.";
|
|
80
|
+
/** `string.isoDate`. */
|
|
81
|
+
readonly ISO_DATE: "Invalid ISO date format.";
|
|
82
|
+
/** `string.uuid`. */
|
|
83
|
+
readonly UUID: "Invalid UUID format.";
|
|
84
|
+
/** `string.url`. */
|
|
85
|
+
readonly URL: "Invalid URL format.";
|
|
86
|
+
/** `string.ipv4`. */
|
|
87
|
+
readonly IPV4: "Invalid IPv4 address.";
|
|
88
|
+
/** `string.ipv6`. */
|
|
89
|
+
readonly IPV6: "Invalid IPv6 address.";
|
|
90
|
+
/** `string.ip`. */
|
|
91
|
+
readonly IP: "Invalid IP address.";
|
|
92
|
+
}>;
|
|
93
|
+
/** A built-in validator error code (a key of {@link VALIDATOR_MESSAGES}), or `CUSTOM`. */
|
|
94
|
+
type TValidatorErrorCode = keyof typeof VALIDATOR_MESSAGES | 'CUSTOM';
|
|
95
|
+
/** A validation error. */
|
|
96
|
+
interface TError {
|
|
97
|
+
/** Dot path of the value (`''`: the root). */
|
|
98
|
+
path: string;
|
|
30
99
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* `validate()` then runs the type's compiled check instead of the interpreted pre-check
|
|
34
|
-
* (results and errors are identical; only speed differs). The check binds to the live
|
|
35
|
-
* type on first use and re-binds after mutations made through `annotate()`, the
|
|
36
|
-
* metadata / props `Map` methods and the `defineAnnotatedType()` handle.
|
|
37
|
-
* Raw writes need `invalidateCompiledValidators()`: property writes (`node.optional = …`,
|
|
38
|
-
* `type.items.push(…)`, …) and in-place edits of annotation values (mutating a constraint
|
|
39
|
-
* object, pushing onto an `@expect.pattern` array directly) — the check keeps the values
|
|
40
|
-
* it bound until then. Any runtime mutation of a type a check has bound to makes the
|
|
41
|
-
* checks re-bind once on next use: cheap at startup, but avoid it per request. Types it
|
|
42
|
-
* cannot bind to, and types not built from generated modules (deserialised,
|
|
43
|
-
* JSON-schema-built, flattened, hand-built), are validated by the interpreted path as
|
|
44
|
-
* before.
|
|
45
|
-
*
|
|
46
|
-
* Adds code to every `.as.js`: meant for backend builds — leave it off for bundles
|
|
47
|
-
* shipped to browsers (the code is CSP-safe: no `eval` / `new Function`).
|
|
48
|
-
*
|
|
49
|
-
* Off (the default), only types annotated with `@emit.compiledValidator` get a compiled
|
|
50
|
-
* check. The `'js:compiled'` render format compiles every type; `'js:interpreted'`
|
|
51
|
-
* compiles none, ignoring `@emit.compiledValidator` (see {@link JS_INTERPRETED_FORMAT}).
|
|
52
|
-
* *(default: `false`)*
|
|
100
|
+
* The message: the code's template filled with `params` (English by default), or the app's
|
|
101
|
+
* own text from an annotation (`custom: true`), translated when a translator is used.
|
|
53
102
|
*/
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Type definition for union, intersection, or tuple types. */
|
|
58
|
-
interface TAtscriptTypeComplex<DataType = unknown> {
|
|
59
|
-
kind: 'union' | 'intersection' | 'tuple';
|
|
60
|
-
items: TAtscriptAnnotatedType[];
|
|
61
|
-
tags: Set<AtscriptPrimitiveTags>;
|
|
62
|
-
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
63
|
-
__dataType?: DataType;
|
|
64
|
-
}
|
|
65
|
-
/** Type definition for array types. */
|
|
66
|
-
interface TAtscriptTypeArray<DataType = unknown[]> {
|
|
67
|
-
kind: 'array';
|
|
68
|
-
of: TAtscriptAnnotatedType;
|
|
69
|
-
tags: Set<AtscriptPrimitiveTags>;
|
|
70
|
-
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
71
|
-
__dataType?: DataType;
|
|
72
|
-
}
|
|
73
|
-
/** Type definition for object types with named and pattern-matched properties. */
|
|
74
|
-
interface TAtscriptTypeObject<K extends string = string, DataType = Record<K, unknown>> {
|
|
75
|
-
kind: 'object';
|
|
76
|
-
props: Map<K, TAtscriptAnnotatedType>;
|
|
77
|
-
propsPatterns: Array<{
|
|
78
|
-
pattern: RegExp;
|
|
79
|
-
def: TAtscriptAnnotatedType;
|
|
80
|
-
}>;
|
|
81
|
-
tags: Set<AtscriptPrimitiveTags>;
|
|
82
|
-
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
83
|
-
__dataType?: DataType;
|
|
84
|
-
}
|
|
85
|
-
/** Type definition for primitive/literal types (string, number, boolean, null, etc.). */
|
|
86
|
-
interface TAtscriptTypeFinal<DataType = unknown> {
|
|
87
|
-
kind: '';
|
|
103
|
+
message: string;
|
|
88
104
|
/**
|
|
89
|
-
*
|
|
105
|
+
* Stable code (UPPER_SNAKE): a {@link TValidatorErrorCode} for the validator's own errors,
|
|
106
|
+
* a plugin's own code, or `CUSTOM`. Optional only for errors made by hand.
|
|
90
107
|
*/
|
|
91
|
-
|
|
108
|
+
code?: TValidatorErrorCode | (string & Record<never, never>);
|
|
109
|
+
/** The values the code's message needs (limits, expected type, pattern, …); JSON-safe. */
|
|
110
|
+
params?: Record<string, unknown>;
|
|
92
111
|
/**
|
|
93
|
-
*
|
|
112
|
+
* `true` when `message` is the app's own text (an annotation's message argument,
|
|
113
|
+
* `@expect.message`, a plugin's message) rather than the code's template: a translator
|
|
114
|
+
* gets that text as the message id instead of the code.
|
|
94
115
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
__dataType?: DataType;
|
|
116
|
+
custom?: boolean;
|
|
117
|
+
/** The errors of each union branch (a union error), in branch order. */
|
|
118
|
+
details?: TError[];
|
|
99
119
|
}
|
|
100
120
|
/**
|
|
101
|
-
*
|
|
121
|
+
* Translates one error: `id` is the error's `code` for a built-in message, or its message text
|
|
122
|
+
* for app text (`custom`, or no code). Return `undefined` to keep the current message.
|
|
102
123
|
*/
|
|
103
|
-
type
|
|
104
|
-
|
|
105
|
-
} ? D : unknown;
|
|
106
|
-
/**
|
|
107
|
-
* Extract the DataType from a {@link TAtscriptAnnotatedType}.
|
|
108
|
-
*
|
|
109
|
-
* Resolves the phantom `__dataType` carried by the type definition.
|
|
110
|
-
* When `__dataType` is `unknown` (unset), falls back to the constructor
|
|
111
|
-
* instance type if `T` is also a class (i.e. a generated interface).
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```ts
|
|
115
|
-
* import type { TAtscriptDataType } from '@atscript/typescript/utils'
|
|
116
|
-
* import MyInterface from './my-interface.as'
|
|
117
|
-
*
|
|
118
|
-
* type Data = TAtscriptDataType<typeof MyInterface>
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
121
|
-
type TAtscriptDataType<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType> = T extends {
|
|
122
|
-
type: {
|
|
123
|
-
__dataType?: infer D;
|
|
124
|
-
};
|
|
125
|
-
} ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown;
|
|
126
|
-
/** Union of all possible type definition shapes. */
|
|
127
|
-
type TAtscriptTypeDef<DataType = unknown> = TAtscriptTypeComplex<DataType> | TAtscriptTypeFinal<DataType> | TAtscriptTypeArray<DataType> | TAtscriptTypeObject<string, DataType>;
|
|
128
|
-
/**
|
|
129
|
-
* Core annotated type — wraps a type definition with metadata and a validator factory.
|
|
130
|
-
*
|
|
131
|
-
* Generated `.as` files produce classes/namespaces that conform to this interface.
|
|
132
|
-
* The `DataType` phantom generic carries the TypeScript data shape for type-safe validation.
|
|
133
|
-
*
|
|
134
|
-
* @typeParam T - The underlying type definition (e.g. {@link TAtscriptTypeObject}).
|
|
135
|
-
* @typeParam DataType - The TypeScript type the validated data narrows to (auto-inferred from `T`).
|
|
136
|
-
*/
|
|
137
|
-
interface TAtscriptAnnotatedType<T extends TAtscriptTypeDef = TAtscriptTypeDef, DataType = InferDataType<T>> {
|
|
138
|
-
__is_atscript_annotated_type: true;
|
|
139
|
-
type: T;
|
|
140
|
-
validator(opts?: Partial<TValidatorOptions>): Validator<this, DataType>;
|
|
141
|
-
metadata: TMetadataMap<AtscriptMetadata>;
|
|
142
|
-
optional?: boolean;
|
|
143
|
-
id?: string;
|
|
144
|
-
ref?: {
|
|
145
|
-
type: () => TAtscriptAnnotatedType;
|
|
146
|
-
field: string;
|
|
147
|
-
};
|
|
148
|
-
}
|
|
124
|
+
type TValidatorTranslate = (id: string, params: Record<string, unknown>, error: TError) => string | undefined;
|
|
125
|
+
|
|
149
126
|
/**
|
|
150
|
-
*
|
|
127
|
+
* What an error reports besides its message (see {@link Validator.error}): its code (`CUSTOM`
|
|
128
|
+
* when omitted), params and `custom` flag.
|
|
151
129
|
*/
|
|
152
|
-
interface
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
interface TError {
|
|
161
|
-
path: string;
|
|
162
|
-
message: string;
|
|
163
|
-
details?: TError[];
|
|
130
|
+
interface TErrorInfo extends Pick<TError, 'code' | 'params'> {
|
|
131
|
+
/**
|
|
132
|
+
* The message is app text (a translator gets it as the id) rather than the code's own
|
|
133
|
+
* message. Default: `true` without a `code`, `false` with one.
|
|
134
|
+
*/
|
|
135
|
+
custom?: boolean;
|
|
164
136
|
}
|
|
165
137
|
/**
|
|
166
138
|
* A plugin function that can intercept validation.
|
|
@@ -177,6 +149,11 @@ interface TValidatorOptions {
|
|
|
177
149
|
unknownProps: 'strip' | 'ignore' | 'error';
|
|
178
150
|
errorLimit: number;
|
|
179
151
|
skipList?: Set<string>;
|
|
152
|
+
/**
|
|
153
|
+
* Translates the errors of a failed call (see {@link localizeErrors}) before they are
|
|
154
|
+
* reported (`errors`, the thrown {@link ValidatorError}).
|
|
155
|
+
*/
|
|
156
|
+
translate?: TValidatorTranslate;
|
|
180
157
|
}
|
|
181
158
|
/** Context exposed to {@link TValidatorPlugin} functions. */
|
|
182
159
|
interface TValidatorPluginContext {
|
|
@@ -260,8 +237,26 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
260
237
|
protected push(name: string): void;
|
|
261
238
|
protected pop(saveErrors: boolean): TError[] | null | undefined;
|
|
262
239
|
protected clear(): void;
|
|
263
|
-
|
|
264
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Reports an error at `path` (default: the current path). `info` gives its code and params;
|
|
242
|
+
* without it the error is app text with code `CUSTOM` (a plugin's own message).
|
|
243
|
+
*/
|
|
244
|
+
protected error(message: string, path?: string, details?: TError[], info?: TErrorInfo): TError;
|
|
245
|
+
/**
|
|
246
|
+
* Reports built-in error `code`: `custom` (the app's message from an annotation) when given,
|
|
247
|
+
* else the code's template filled with `params`.
|
|
248
|
+
*/
|
|
249
|
+
private fail;
|
|
250
|
+
/** Reports built-in error `code` at `path` (see {@link fail}), with `details` (a union's). */
|
|
251
|
+
private builtin;
|
|
252
|
+
/** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */
|
|
253
|
+
private mismatch;
|
|
254
|
+
/** The `@meta.required` error of `def` (custom message or the type's default). */
|
|
255
|
+
private required;
|
|
256
|
+
/**
|
|
257
|
+
* Adds `error`, already made, to the errors of the current level (as {@link error} does);
|
|
258
|
+
* `true` once the level holds as many errors as the limit.
|
|
259
|
+
*/
|
|
265
260
|
private report;
|
|
266
261
|
protected throw(): void;
|
|
267
262
|
/**
|
|
@@ -304,6 +299,19 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
304
299
|
protected get root(): T;
|
|
305
300
|
protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
|
|
306
301
|
protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
|
|
302
|
+
/** Reports the error of union `def` no branch matched (`details`: the branches' errors). */
|
|
303
|
+
private unionError;
|
|
304
|
+
/**
|
|
305
|
+
* The errors of an object union over object `value` with a shared literal discriminator (see
|
|
306
|
+
* {@link unionDiscriminator}), reported in place of the union error: the errors of the branch
|
|
307
|
+
* the value's discriminator picks, or one `UNION_DISCRIMINATOR` error at the discriminator
|
|
308
|
+
* when it picks none. `undefined` (nothing reported) for other unions and values.
|
|
309
|
+
*/
|
|
310
|
+
private discriminatedErrors;
|
|
311
|
+
/** Reports `errors`, made already, until the error limit is reached. */
|
|
312
|
+
private reportAll;
|
|
313
|
+
/** Whether missing props are allowed at the current depth (the `partial` option). */
|
|
314
|
+
private partialHere;
|
|
307
315
|
/** A union branch failed after branches of nested unions over objects did (see `unionMemo`). */
|
|
308
316
|
private wasteBranch;
|
|
309
317
|
/** The outcome this call recorded for union `def` over `value` at `path` (see `unionMemo`). */
|
|
@@ -340,11 +348,206 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
340
348
|
protected validateNumber(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: number): boolean;
|
|
341
349
|
/** `@expect.int` / `min` / `max` / `exclusiveMin` / `exclusiveMax` checks for a number value. */
|
|
342
350
|
protected validateNumberConstraints(meta: TMetadataMap<AtscriptMetadata>, value: number): boolean;
|
|
343
|
-
/** Reports a failed number bound: its custom message, else `Expected <label> <bound>, got <value>`. */
|
|
344
|
-
private numberBoundFailed;
|
|
345
351
|
protected validateBoolean(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: boolean): boolean;
|
|
346
352
|
/** `@meta.required` check for a boolean value (must be `true`). */
|
|
347
353
|
protected validateBooleanConstraints(meta: TMetadataMap<AtscriptMetadata>, value: boolean): boolean;
|
|
354
|
+
/**
|
|
355
|
+
* `@expect.minKeys` / `maxKeys` checks for an object value, matched as `shape`: the keys the
|
|
356
|
+
* result keeps are counted (see {@link keptKeyCount}; `skipList`: the skip list keys here).
|
|
357
|
+
*/
|
|
358
|
+
protected validateObjectConstraints(meta: TMetadataMap<AtscriptMetadata>, value: object, shape: TAtscriptAnnotatedType, skipList: Set<string> | undefined): boolean;
|
|
359
|
+
/**
|
|
360
|
+
* The keys of object `value` (matched as `shape`) the result keeps: every own key with
|
|
361
|
+
* `unknownProps: 'ignore'`; else (and with 'strip', and 'error', where the others are errors
|
|
362
|
+
* of their own) the keys `shape` declares, its pattern props match or the skip list names.
|
|
363
|
+
* Matched as anything other than an object type, every own key.
|
|
364
|
+
*/
|
|
365
|
+
private keptKeyCount;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Type definition for union, intersection, or tuple types. */
|
|
369
|
+
interface TAtscriptTypeComplex<DataType = unknown> {
|
|
370
|
+
kind: 'union' | 'intersection' | 'tuple';
|
|
371
|
+
items: TAtscriptAnnotatedType[];
|
|
372
|
+
tags: Set<AtscriptPrimitiveTags>;
|
|
373
|
+
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
374
|
+
__dataType?: DataType;
|
|
375
|
+
}
|
|
376
|
+
/** Type definition for array types. */
|
|
377
|
+
interface TAtscriptTypeArray<DataType = unknown[]> {
|
|
378
|
+
kind: 'array';
|
|
379
|
+
of: TAtscriptAnnotatedType;
|
|
380
|
+
tags: Set<AtscriptPrimitiveTags>;
|
|
381
|
+
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
382
|
+
__dataType?: DataType;
|
|
383
|
+
}
|
|
384
|
+
/** Type definition for object types with named and pattern-matched properties. */
|
|
385
|
+
interface TAtscriptTypeObject<K extends string = string, DataType = Record<K, unknown>> {
|
|
386
|
+
kind: 'object';
|
|
387
|
+
props: Map<K, TAtscriptAnnotatedType>;
|
|
388
|
+
propsPatterns: Array<{
|
|
389
|
+
pattern: RegExp;
|
|
390
|
+
def: TAtscriptAnnotatedType;
|
|
391
|
+
}>;
|
|
392
|
+
tags: Set<AtscriptPrimitiveTags>;
|
|
393
|
+
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
394
|
+
__dataType?: DataType;
|
|
395
|
+
}
|
|
396
|
+
/** Type definition for primitive/literal types (string, number, boolean, null, etc.). */
|
|
397
|
+
interface TAtscriptTypeFinal<DataType = unknown> {
|
|
398
|
+
kind: '';
|
|
399
|
+
/**
|
|
400
|
+
* design type
|
|
401
|
+
*/
|
|
402
|
+
designType: 'string' | 'number' | 'boolean' | 'undefined' | 'null' | 'object' | 'any' | 'never' | 'phantom' | 'decimal';
|
|
403
|
+
/**
|
|
404
|
+
* value for literals
|
|
405
|
+
*/
|
|
406
|
+
value?: string | number | boolean;
|
|
407
|
+
tags: Set<AtscriptPrimitiveTags>;
|
|
408
|
+
/** @internal phantom — carries the DataType at the type level, never set at runtime */
|
|
409
|
+
__dataType?: DataType;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Extract DataType from a type def's phantom generic
|
|
413
|
+
*/
|
|
414
|
+
type InferDataType<T> = T extends {
|
|
415
|
+
__dataType?: infer D;
|
|
416
|
+
} ? D : unknown;
|
|
417
|
+
/**
|
|
418
|
+
* Extract the DataType from a {@link TAtscriptAnnotatedType}.
|
|
419
|
+
*
|
|
420
|
+
* Resolves the phantom `__dataType` carried by the type definition.
|
|
421
|
+
* When `__dataType` is `unknown` (unset), falls back to the constructor
|
|
422
|
+
* instance type if `T` is also a class (i.e. a generated interface).
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```ts
|
|
426
|
+
* import type { TAtscriptDataType } from '@atscript/typescript/utils'
|
|
427
|
+
* import MyInterface from './my-interface.as'
|
|
428
|
+
*
|
|
429
|
+
* type Data = TAtscriptDataType<typeof MyInterface>
|
|
430
|
+
* ```
|
|
431
|
+
*/
|
|
432
|
+
type TAtscriptDataType<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType> = T extends {
|
|
433
|
+
type: {
|
|
434
|
+
__dataType?: infer D;
|
|
435
|
+
};
|
|
436
|
+
} ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown;
|
|
437
|
+
/** Union of all possible type definition shapes. */
|
|
438
|
+
type TAtscriptTypeDef<DataType = unknown> = TAtscriptTypeComplex<DataType> | TAtscriptTypeFinal<DataType> | TAtscriptTypeArray<DataType> | TAtscriptTypeObject<string, DataType>;
|
|
439
|
+
/**
|
|
440
|
+
* Core annotated type — wraps a type definition with metadata and a validator factory.
|
|
441
|
+
*
|
|
442
|
+
* Generated `.as` files produce classes/namespaces that conform to this interface.
|
|
443
|
+
* The `DataType` phantom generic carries the TypeScript data shape for type-safe validation.
|
|
444
|
+
*
|
|
445
|
+
* @typeParam T - The underlying type definition (e.g. {@link TAtscriptTypeObject}).
|
|
446
|
+
* @typeParam DataType - The TypeScript type the validated data narrows to (auto-inferred from `T`).
|
|
447
|
+
*/
|
|
448
|
+
interface TAtscriptAnnotatedType<T extends TAtscriptTypeDef = TAtscriptTypeDef, DataType = InferDataType<T>> {
|
|
449
|
+
__is_atscript_annotated_type: true;
|
|
450
|
+
type: T;
|
|
451
|
+
validator(opts?: Partial<TValidatorOptions>): Validator<this, DataType>;
|
|
452
|
+
metadata: TMetadataMap<AtscriptMetadata>;
|
|
453
|
+
optional?: boolean;
|
|
454
|
+
id?: string;
|
|
455
|
+
ref?: {
|
|
456
|
+
type: () => TAtscriptAnnotatedType;
|
|
457
|
+
field: string;
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Atscript Metadata Map with typed setters/getters
|
|
462
|
+
*/
|
|
463
|
+
interface TMetadataMap<O extends object> extends Map<keyof O, O[keyof O]> {
|
|
464
|
+
get<K extends keyof O>(key: K): O[K] | undefined;
|
|
465
|
+
get(key: string): unknown;
|
|
466
|
+
set<K extends keyof O>(key: K, value: O[K]): this;
|
|
467
|
+
has<K extends keyof O>(key: K): boolean;
|
|
468
|
+
has(key: string): boolean;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** Options of {@link buildJsonSchema} (and of the `jsonSchemaOptions` plugin option). */
|
|
472
|
+
interface TJsonSchemaOptions {
|
|
473
|
+
/**
|
|
474
|
+
* `additionalProperties: false` on every object schema: the schema rejects keys the type does
|
|
475
|
+
* not declare, as the validator does with its default `unknownProps: 'error'`.
|
|
476
|
+
* *(default: `false`)*
|
|
477
|
+
*/
|
|
478
|
+
closed?: boolean;
|
|
479
|
+
/**
|
|
480
|
+
* `@meta.description` → `description` and `@meta.label` → `title`, on the schema of the
|
|
481
|
+
* type or field that carries them. *(default: `true`)*
|
|
482
|
+
*/
|
|
483
|
+
annotations?: boolean;
|
|
484
|
+
/**
|
|
485
|
+
* A union of literals (`'a' | 'b'`, also with `null`) → `{ type, enum }` instead of an
|
|
486
|
+
* `anyOf` of `const` schemas. *(default: `true`)*
|
|
487
|
+
*/
|
|
488
|
+
enums?: boolean;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
interface TTsPluginOptions {
|
|
492
|
+
/**
|
|
493
|
+
* JSON Schema support mode:
|
|
494
|
+
* - `false` — No support. `toJsonSchema()` throws at runtime. No `buildJsonSchema` import. *(default)*
|
|
495
|
+
* - `'lazy'` — Import `buildJsonSchema`, compute on demand, cache in type object.
|
|
496
|
+
* - `'bundle'` — Pre-compute at build time, embed static JSON in output. No import.
|
|
497
|
+
*
|
|
498
|
+
* Individual interfaces can override this with `@emit.jsonSchema` annotation
|
|
499
|
+
* to force build-time embedding regardless of this setting.
|
|
500
|
+
*/
|
|
501
|
+
jsonSchema?: false | 'lazy' | 'bundle';
|
|
502
|
+
/**
|
|
503
|
+
* Options of the generated `toJsonSchema()` (`'lazy'`, `'bundle'` and `@emit.jsonSchema`
|
|
504
|
+
* alike): the same schema `buildJsonSchema(type, jsonSchemaOptions)` builds at runtime.
|
|
505
|
+
* `closed` (`additionalProperties: false`, default `false`), `annotations` (`description` /
|
|
506
|
+
* `title` from `@meta.description` / `@meta.label`, default `true`), `enums` (a union of
|
|
507
|
+
* literals as `enum`, default `true`).
|
|
508
|
+
*/
|
|
509
|
+
jsonSchemaOptions?: TJsonSchemaOptions;
|
|
510
|
+
/**
|
|
511
|
+
* Example data support:
|
|
512
|
+
* - `false` — No support. `toExampleData` is not rendered. *(default)*
|
|
513
|
+
* - `true` — Import `createDataFromAnnotatedType`, create example data on each call.
|
|
514
|
+
*/
|
|
515
|
+
exampleData?: boolean;
|
|
516
|
+
/**
|
|
517
|
+
* Extension the emitted JS uses for **relative** `.as` imports
|
|
518
|
+
* (`./user` → `./user.as` by default). Bare specifiers (packages, e.g.
|
|
519
|
+
* `some-pkg/models/user.as`) always keep `.as` because they resolve through
|
|
520
|
+
* the package's `exports` map.
|
|
521
|
+
*
|
|
522
|
+
* `.as.mjs` is what `asc db sync` uses to load the compiled models straight
|
|
523
|
+
* from Node. *(default: `'.as'`)*
|
|
524
|
+
*/
|
|
525
|
+
moduleExtension?: '.as' | '.as.js' | '.as.mjs';
|
|
526
|
+
/**
|
|
527
|
+
* Emit a build-time compiled validator check per type into the `.as.js` output.
|
|
528
|
+
*
|
|
529
|
+
* `validate()` then runs the type's compiled check instead of the interpreted pre-check
|
|
530
|
+
* (results and errors are identical; only speed differs). The check binds to the live
|
|
531
|
+
* type on first use and re-binds after mutations made through `annotate()`, the
|
|
532
|
+
* metadata / props `Map` methods and the `defineAnnotatedType()` handle.
|
|
533
|
+
* Raw writes need `invalidateCompiledValidators()`: property writes (`node.optional = …`,
|
|
534
|
+
* `type.items.push(…)`, …) and in-place edits of annotation values (mutating a constraint
|
|
535
|
+
* object, pushing onto an `@expect.pattern` array directly) — the check keeps the values
|
|
536
|
+
* it bound until then. Any runtime mutation of a type a check has bound to makes the
|
|
537
|
+
* checks re-bind once on next use: cheap at startup, but avoid it per request. Types it
|
|
538
|
+
* cannot bind to, and types not built from generated modules (deserialised,
|
|
539
|
+
* JSON-schema-built, flattened, hand-built), are validated by the interpreted path as
|
|
540
|
+
* before.
|
|
541
|
+
*
|
|
542
|
+
* Adds code to every `.as.js`: meant for backend builds — leave it off for bundles
|
|
543
|
+
* shipped to browsers (the code is CSP-safe: no `eval` / `new Function`).
|
|
544
|
+
*
|
|
545
|
+
* Off (the default), only types annotated with `@emit.compiledValidator` get a compiled
|
|
546
|
+
* check. The `'js:compiled'` render format compiles every type; `'js:interpreted'`
|
|
547
|
+
* compiles none, ignoring `@emit.compiledValidator` (see {@link JS_INTERPRETED_FORMAT}).
|
|
548
|
+
* *(default: `false`)*
|
|
549
|
+
*/
|
|
550
|
+
compileValidators?: boolean;
|
|
348
551
|
}
|
|
349
552
|
|
|
350
553
|
interface PrepareFixturesOptions {
|
package/dist/test-utils.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { r as tsPlugin } from "./plugin-
|
|
2
|
-
import {
|
|
1
|
+
import { r as tsPlugin } from "./plugin-Bx7j-vWf.mjs";
|
|
2
|
+
import { j as Validator } from "./user-text-BOf56N4d.mjs";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { build } from "@atscript/core";
|
|
5
5
|
import { readFileSync, writeFileSync } from "fs";
|