@atscript/typescript 0.2.2 → 0.2.4

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.
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_plugin = require('./plugin-oUSsb3Ri.cjs');
3
- const require_unique_name = require('./unique-name-xWd9uO-k.cjs');
2
+ const require_plugin = require('./plugin-Cqao46Gt.cjs');
3
+ const require_user_text = require('./user-text-BddKk8EU.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 require_unique_name.Validator {};
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;
@@ -157,11 +157,140 @@ interface TMetadataMap<O extends object> extends Map<keyof O, O[keyof O]> {
157
157
  has(key: string): boolean;
158
158
  }
159
159
 
160
+ /**
161
+ * Validator error codes, their default English templates and the translation helpers.
162
+ *
163
+ * The validator is locale-agnostic: every error it reports carries a stable `code` and the
164
+ * `params` its message needs, and its `message` is the default English text (the code's
165
+ * template filled with the params) or the app's own text from an annotation (`custom: true`).
166
+ * Translate after the fact with {@link localizeErrors}, or per validator with the `translate`
167
+ * option.
168
+ */
169
+ /**
170
+ * Default English templates of the built-in error codes. `{name}` is replaced with
171
+ * `params.name` (see {@link formatValidatorMessage}).
172
+ */
173
+ declare const VALIDATOR_MESSAGES: Readonly<{
174
+ /** A required string is empty (or whitespace only), or a required optional field is `null`. */
175
+ readonly REQUIRED: "Must not be empty";
176
+ /** A required boolean (`@meta.required` on a boolean) is not `true`. */
177
+ readonly REQUIRED_CHECKED: "Must be checked";
178
+ /** Wrong primitive type. `{ expected, actual }`: type names. */
179
+ readonly TYPE: "Expected {expected}, got {actual}";
180
+ /** Not an array. `{ actual }`: the value's type name. */
181
+ readonly TYPE_ARRAY: "Expected array";
182
+ /** Not an object. `{ actual }`: the value's type name. */
183
+ readonly TYPE_OBJECT: "Expected object";
184
+ /** A decimal that is not a string. `{ actual }`: the value's type name. */
185
+ readonly TYPE_DECIMAL: "Expected string (decimal), got {actual}";
186
+ /** `NaN` or an infinity. `{ actual }`: the value as text. */
187
+ readonly NOT_FINITE: "Expected finite number, got {actual}";
188
+ /** A decimal string in the wrong format. `{ value }`: the string. */
189
+ readonly DECIMAL_FORMAT: "Invalid decimal format: \"{value}\"";
190
+ /** A `never` type. */
191
+ readonly NEVER: "This type is impossible, must be an internal problem";
192
+ /** Not the literal value. `{ expected, actual }`: the literal, the value as text. */
193
+ readonly LITERAL: "Expected {expected}, got {actual}";
194
+ /** No union branch matches. `{ expected }`: the branch kinds (`[string(0)], [number(1)]`). */
195
+ readonly UNION: "Value does not match any of the allowed types: {expected}";
196
+ /**
197
+ * The discriminator of an object union holds no branch's value. Reported at the discriminator
198
+ * property. `{ property, allowed, actual }`: its name, the allowed values, the value as text.
199
+ */
200
+ readonly UNION_DISCRIMINATOR: "Expected one of {allowed}, got {actual}";
201
+ /** A property the type does not declare (`unknownProps: 'error'`). `{ property }`. */
202
+ readonly UNEXPECTED_PROPERTY: "Unexpected property";
203
+ /** A tuple of the wrong length. `{ length }`: the expected length. */
204
+ readonly TUPLE_LENGTH: "Expected array of length {length}";
205
+ /** `@expect.minLength` on a string. `{ min, actual }`: lengths. */
206
+ readonly MIN_LENGTH: "Expected minimum length of {min} characters, got {actual} characters";
207
+ /** `@expect.maxLength` on a string. `{ max, actual }`: lengths. */
208
+ readonly MAX_LENGTH: "Expected maximum length of {max} characters, got {actual} characters";
209
+ /** `@expect.minLength` on an array. `{ min, actual }`: item counts. */
210
+ readonly MIN_ITEMS: "Expected minimum length of {min} items, got {actual} items";
211
+ /** `@expect.maxLength` on an array. `{ max, actual }`: item counts. */
212
+ readonly MAX_ITEMS: "Expected maximum length of {max} items, got {actual} items";
213
+ /** `@expect.array.uniqueItems`: a duplicate item (reported at its index). */
214
+ readonly UNIQUE_ITEMS: "Duplicate items are not allowed";
215
+ /** `@expect.minKeys`. `{ min, actual }`: key counts. */
216
+ readonly MIN_KEYS: "Expected at least {min} properties, got {actual}";
217
+ /** `@expect.maxKeys`. `{ max, actual }`: key counts. */
218
+ readonly MAX_KEYS: "Expected at most {max} properties, got {actual}";
219
+ /** `@expect.pattern` without a message. `{ pattern, flags }`. */
220
+ readonly PATTERN: "Value is expected to match pattern \"{pattern}\"";
221
+ /** `@expect.int`. `{ actual }`: the value. */
222
+ readonly INT: "Expected integer, got {actual}";
223
+ /** `@expect.min`. `{ min, actual }`. */
224
+ readonly MIN: "Expected minimum {min}, got {actual}";
225
+ /** `@expect.max`. `{ max, actual }`. */
226
+ readonly MAX: "Expected maximum {max}, got {actual}";
227
+ /** `@expect.exclusiveMin`. `{ min, actual }`. */
228
+ readonly EXCLUSIVE_MIN: "Expected greater than {min}, got {actual}";
229
+ /** `@expect.exclusiveMax`. `{ max, actual }`. */
230
+ readonly EXCLUSIVE_MAX: "Expected less than {max}, got {actual}";
231
+ /** `string.email`. */
232
+ readonly EMAIL: "Invalid email format.";
233
+ /** `string.phone`. */
234
+ readonly PHONE: "Invalid phone number format.";
235
+ /** `string.date`. */
236
+ readonly DATE: "Invalid date format.";
237
+ /** `string.isoDate`. */
238
+ readonly ISO_DATE: "Invalid ISO date format.";
239
+ /** `string.uuid`. */
240
+ readonly UUID: "Invalid UUID format.";
241
+ /** `string.url`. */
242
+ readonly URL: "Invalid URL format.";
243
+ /** `string.ipv4`. */
244
+ readonly IPV4: "Invalid IPv4 address.";
245
+ /** `string.ipv6`. */
246
+ readonly IPV6: "Invalid IPv6 address.";
247
+ /** `string.ip`. */
248
+ readonly IP: "Invalid IP address.";
249
+ }>;
250
+ /** A built-in validator error code (a key of {@link VALIDATOR_MESSAGES}), or `CUSTOM`. */
251
+ type TValidatorErrorCode = keyof typeof VALIDATOR_MESSAGES | 'CUSTOM';
252
+ /** A validation error. */
160
253
  interface TError {
254
+ /** Dot path of the value (`''`: the root). */
161
255
  path: string;
256
+ /**
257
+ * The message: the code's template filled with `params` (English by default), or the app's
258
+ * own text from an annotation (`custom: true`), translated when a translator is used.
259
+ */
162
260
  message: string;
261
+ /**
262
+ * Stable code (UPPER_SNAKE): a {@link TValidatorErrorCode} for the validator's own errors,
263
+ * a plugin's own code, or `CUSTOM`. Optional only for errors made by hand.
264
+ */
265
+ code?: TValidatorErrorCode | (string & Record<never, never>);
266
+ /** The values the code's message needs (limits, expected type, pattern, …); JSON-safe. */
267
+ params?: Record<string, unknown>;
268
+ /**
269
+ * `true` when `message` is the app's own text (an annotation's message argument,
270
+ * `@expect.message`, a plugin's message) rather than the code's template: a translator
271
+ * gets that text as the message id instead of the code.
272
+ */
273
+ custom?: boolean;
274
+ /** The errors of each union branch (a union error), in branch order. */
163
275
  details?: TError[];
164
276
  }
277
+ /**
278
+ * Translates one error: `id` is the error's `code` for a built-in message, or its message text
279
+ * for app text (`custom`, or no code). Return `undefined` to keep the current message.
280
+ */
281
+ type TValidatorTranslate = (id: string, params: Record<string, unknown>, error: TError) => string | undefined;
282
+
283
+ /**
284
+ * What an error reports besides its message (see {@link Validator.error}): its code (`CUSTOM`
285
+ * when omitted), params and `custom` flag.
286
+ */
287
+ interface TErrorInfo extends Pick<TError, 'code' | 'params'> {
288
+ /**
289
+ * The message is app text (a translator gets it as the id) rather than the code's own
290
+ * message. Default: `true` without a `code`, `false` with one.
291
+ */
292
+ custom?: boolean;
293
+ }
165
294
  /**
166
295
  * A plugin function that can intercept validation.
167
296
  *
@@ -177,6 +306,11 @@ interface TValidatorOptions {
177
306
  unknownProps: 'strip' | 'ignore' | 'error';
178
307
  errorLimit: number;
179
308
  skipList?: Set<string>;
309
+ /**
310
+ * Translates the errors of a failed call (see {@link localizeErrors}) before they are
311
+ * reported (`errors`, the thrown {@link ValidatorError}).
312
+ */
313
+ translate?: TValidatorTranslate;
180
314
  }
181
315
  /** Context exposed to {@link TValidatorPlugin} functions. */
182
316
  interface TValidatorPluginContext {
@@ -260,8 +394,26 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
260
394
  protected push(name: string): void;
261
395
  protected pop(saveErrors: boolean): TError[] | null | undefined;
262
396
  protected clear(): void;
263
- protected error(message: string, path?: string, details?: TError[]): TError;
264
- /** Adds `error`, already made, to the errors of the current level (as {@link error} does). */
397
+ /**
398
+ * Reports an error at `path` (default: the current path). `info` gives its code and params;
399
+ * without it the error is app text with code `CUSTOM` (a plugin's own message).
400
+ */
401
+ protected error(message: string, path?: string, details?: TError[], info?: TErrorInfo): TError;
402
+ /**
403
+ * Reports built-in error `code`: `custom` (the app's message from an annotation) when given,
404
+ * else the code's template filled with `params`.
405
+ */
406
+ private fail;
407
+ /** Reports built-in error `code` at `path` (see {@link fail}), with `details` (a union's). */
408
+ private builtin;
409
+ /** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */
410
+ private mismatch;
411
+ /** The `@meta.required` error of `def` (custom message or the type's default). */
412
+ private required;
413
+ /**
414
+ * Adds `error`, already made, to the errors of the current level (as {@link error} does);
415
+ * `true` once the level holds as many errors as the limit.
416
+ */
265
417
  private report;
266
418
  protected throw(): void;
267
419
  /**
@@ -304,6 +456,19 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
304
456
  protected get root(): T;
305
457
  protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
306
458
  protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
459
+ /** Reports the error of union `def` no branch matched (`details`: the branches' errors). */
460
+ private unionError;
461
+ /**
462
+ * The errors of an object union over object `value` with a shared literal discriminator (see
463
+ * {@link unionDiscriminator}), reported in place of the union error: the errors of the branch
464
+ * the value's discriminator picks, or one `UNION_DISCRIMINATOR` error at the discriminator
465
+ * when it picks none. `undefined` (nothing reported) for other unions and values.
466
+ */
467
+ private discriminatedErrors;
468
+ /** Reports `errors`, made already, until the error limit is reached. */
469
+ private reportAll;
470
+ /** Whether missing props are allowed at the current depth (the `partial` option). */
471
+ private partialHere;
307
472
  /** A union branch failed after branches of nested unions over objects did (see `unionMemo`). */
308
473
  private wasteBranch;
309
474
  /** The outcome this call recorded for union `def` over `value` at `path` (see `unionMemo`). */
@@ -340,11 +505,21 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
340
505
  protected validateNumber(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: number): boolean;
341
506
  /** `@expect.int` / `min` / `max` / `exclusiveMin` / `exclusiveMax` checks for a number value. */
342
507
  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
508
  protected validateBoolean(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: boolean): boolean;
346
509
  /** `@meta.required` check for a boolean value (must be `true`). */
347
510
  protected validateBooleanConstraints(meta: TMetadataMap<AtscriptMetadata>, value: boolean): boolean;
511
+ /**
512
+ * `@expect.minKeys` / `maxKeys` checks for an object value, matched as `shape`: the keys the
513
+ * result keeps are counted (see {@link keptKeyCount}; `skipList`: the skip list keys here).
514
+ */
515
+ protected validateObjectConstraints(meta: TMetadataMap<AtscriptMetadata>, value: object, shape: TAtscriptAnnotatedType, skipList: Set<string> | undefined): boolean;
516
+ /**
517
+ * The keys of object `value` (matched as `shape`) the result keeps: every own key with
518
+ * `unknownProps: 'ignore'`; else (and with 'strip', and 'error', where the others are errors
519
+ * of their own) the keys `shape` declares, its pattern props match or the skip list names.
520
+ * Matched as anything other than an object type, every own key.
521
+ */
522
+ private keptKeyCount;
348
523
  }
349
524
 
350
525
  interface PrepareFixturesOptions {
@@ -1,5 +1,5 @@
1
- import { r as tsPlugin } from "./plugin-DhesoKce.mjs";
2
- import { E as Validator } from "./unique-name-hg1CyPIs.mjs";
1
+ import { r as tsPlugin } from "./plugin-Ct9Ko5JG.mjs";
2
+ import { j as Validator } from "./user-text-qL59indq.mjs";
3
3
  import path from "path";
4
4
  import { build } from "@atscript/core";
5
5
  import { readFileSync, writeFileSync } from "fs";