@atscript/typescript 0.2.5 → 0.2.7

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/utils.d.ts CHANGED
@@ -667,9 +667,19 @@ declare function isAnnotatedTypeOfPrimitive(t: TAtscriptAnnotatedType): boolean;
667
667
  * - Strings targeting `number` are trimmed and parsed via `Number()`; only
668
668
  * finite results are accepted.
669
669
  * - Strings targeting `boolean` accept `"true"/"1"` and `"false"/"0"`.
670
- * - Unions try each branch in declared order; the first successful parse wins.
670
+ * - Unions pick one branch and coerce the value against it only (linear, no
671
+ * backtracking). A scalar branch takes what it parses; an object (tuple)
672
+ * branch takes a plain object (an array of its length) unless a literal
673
+ * property (item) — `kind: 'cat'`, `kind: 'cat' | null` — holds a value that
674
+ * does not coerce to it, a string compared after the normalizers of the
675
+ * literal and of the unions it is in. An object takes the branch its
676
+ * discriminator names (as the validator does: the literal itself first, else
677
+ * `"2"` names a `2` literal), else the first whose shape it has (required
678
+ * props present, no undeclared key), else the first that takes it. When no
679
+ * branch takes the value, it is returned untouched.
671
680
  * - Objects recurse into props when the input is a plain object (covers
672
- * `@Query()` DTOs where every field arrives as a string).
681
+ * `@Query()` DTOs where every field arrives as a string), whatever their
682
+ * literal props hold unless tried as a union branch.
673
683
  * - Arrays and tuples coerce items; intersections apply each member in order.
674
684
  * - `string` / `decimal` targets never change string input.
675
685
  *
@@ -707,9 +717,11 @@ declare function coerceScalar(designType: TAtscriptTypeFinal['designType'], valu
707
717
  * `detectDiscriminator`) uses the branch the value's discriminator names;
708
718
  * - otherwise every branch that can take the value is tried (a literal branch can take a string
709
719
  * only when the normalized string equals its literal): when they all give the same result it
710
- * is used; when they differ, the branches whose result their own validator accepts (default
711
- * options) are kept, and the result is used when they agree; else the value is returned
712
- * untouched (ambiguous).
720
+ * is used; when they differ, the branches whose result their own validator accepts are kept,
721
+ * and the result is used when they agree; else the value is returned untouched (ambiguous).
722
+ * The branch validators take `opts` — pass the options the result will be validated with
723
+ * (`partial`, `unknownProps`, …) so a branch is judged as the validator will judge it; default
724
+ * options when omitted. (`unknownProps: 'strip'` judges as `'ignore'`: nothing is stripped.)
713
725
  *
714
726
  * The intended flow is `coerceForType` → `normalizeForType` → `validate`.
715
727
  *
@@ -717,9 +729,17 @@ declare function coerceScalar(designType: TAtscriptTypeFinal['designType'], valu
717
729
  * ```ts
718
730
  * // @normalize.trim @normalize.lowercase email: string.email
719
731
  * normalizeForType(User, { email: ' Ann@Example.COM ' }) // → { email: 'ann@example.com' }
732
+ * // judged as a partial update will be validated
733
+ * normalizeForType(Patch, body, { partial: true })
720
734
  * ```
721
735
  */
722
- declare function normalizeForType<T = unknown>(def: TAtscriptAnnotatedType, value: T): T;
736
+ declare function normalizeForType<T = unknown>(def: TAtscriptAnnotatedType, value: T, opts?: Partial<TValidatorOptions>): T;
737
+ /**
738
+ * Whether {@link normalizeForType} can change a value of type `def`: `def` or a type its walk
739
+ * reaches (props, pattern props, items, union and intersection members) carries `@normalize.*`.
740
+ * Cached per type and kept current across type writes, so callers can skip their own walks.
741
+ */
742
+ declare function typeHasNormalizers(def: TAtscriptAnnotatedType): boolean;
723
743
 
724
744
  /** A JSON Schema object (draft-compatible). */
725
745
  type TJsonSchema = Record<string, any>;
@@ -896,9 +916,9 @@ declare function throwFeatureDisabled(feature: string, option: string, annotatio
896
916
 
897
917
  /**
898
918
  * The JSON schema of `type` built by the runtime (`jsonSchema: 'lazy'`): built once, again after
899
- * a metadata write (a block, a runtime edit: see `metadataWrites`). Called by generated code
900
- * (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same for every call
901
- * of a type).
919
+ * a type tree write (a block, a runtime edit of metadata or shape: see `typeWrites`). Called by
920
+ * generated code (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same
921
+ * for every call of a type).
902
922
  */
903
923
  declare function lazyJsonSchema(type: TAtscriptAnnotatedType, opts?: TJsonSchemaOptions): TJsonSchema;
904
924
  /**
@@ -1313,7 +1333,7 @@ declare function translateServed<T>(json: T, t: (text: string) => string | undef
1313
1333
  * a base shows through every layer over it.
1314
1334
  *
1315
1335
  * Reads go through a flattened view of the effective entries, rebuilt on the first read after any
1316
- * metadata write (one global write counter: `metadataWriteCount`). The view is used only while
1336
+ * type tree write (one global write counter: `typeWriteCount`). The view is used only while
1317
1337
  * every map below is tracked (an `AnnotationMap` or a `LayerMap`, whose writes the counter
1318
1338
  * sees); otherwise the reads are live. A raw write the counter does not see (a node's map swapped,
1319
1339
  * `node.metadata = X`, or edited in place) must be followed by `invalidateCompiledValidators()`,
@@ -1338,11 +1358,11 @@ declare class LayerMap<K = unknown, V = unknown> extends AnnotationMap<K, V> {
1338
1358
  r: boolean;
1339
1359
  /**
1340
1360
  * The flattened view: every effective entry (as the layered reads give it, in their order),
1341
- * built on the first read after a metadata write (`w` ≠ `metadataWrites`); `undefined` while a
1361
+ * built on the first read after a type tree write (`w` ≠ `typeWrites`); `undefined` while a
1342
1362
  * map below is not tracked (a plain `Map` or a subclass: its writes are not counted, read live).
1343
1363
  */
1344
1364
  private f;
1345
- /** `metadataWrites` when {@link f} was built (`-1`: not built). */
1365
+ /** `typeWrites` when {@link f} was built (`-1`: not built). */
1346
1366
  private w;
1347
1367
  constructor(bases: TLayerNode[], reference?: boolean, own?: Iterable<readonly [K, V]> | null);
1348
1368
  /**
@@ -1525,5 +1545,5 @@ type NavPropsOf<T> = T extends {
1525
1545
  __navProps: infer N extends Record<string, unknown>;
1526
1546
  } ? N : Record<string, never>;
1527
1547
 
1528
- export { $ak, $alias, $blk, $end, $f, $fc, $ft, $nr, $ps, $q, $sh, $ut, LayerMap, SERIALIZE_VERSION, VALIDATOR_ERROR_CODES, VALIDATOR_MESSAGES, Validator, ValidatorError, annotate, appliedBlockModules, buildJsonSchema, bundledSchemaOf, coerceForType, coerceScalar, createAnnotatedTypeNode, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, detectDiscriminator, flattenAnnotatedType, forAnnotatedType, formatValidatorMessage, fromJsonSchema, invalidateCompiledValidators, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, lazyJsonSchema, localizeErrors, mergeJsonSchemas, messagesTranslator, metaEmpty, normalizeForType, pendingStatements, registerCompiledCheck, registerUserText, serializeAnnotatedType, throwFeatureDisabled, translateServed, unionDiscriminator, userTextKeys, userTextOf, watchLayer };
1548
+ export { $ak, $alias, $blk, $end, $f, $fc, $ft, $nr, $ps, $q, $sh, $ut, LayerMap, SERIALIZE_VERSION, VALIDATOR_ERROR_CODES, VALIDATOR_MESSAGES, Validator, ValidatorError, annotate, appliedBlockModules, buildJsonSchema, bundledSchemaOf, coerceForType, coerceScalar, createAnnotatedTypeNode, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, detectDiscriminator, flattenAnnotatedType, forAnnotatedType, formatValidatorMessage, fromJsonSchema, invalidateCompiledValidators, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, lazyJsonSchema, localizeErrors, mergeJsonSchemas, messagesTranslator, metaEmpty, normalizeForType, pendingStatements, registerCompiledCheck, registerUserText, serializeAnnotatedType, throwFeatureDisabled, translateServed, typeHasNormalizers, unionDiscriminator, userTextKeys, userTextOf, watchLayer };
1529
1549
  export type { AtscriptExprNode, AtscriptOrderItem, AtscriptQueryComparison, AtscriptQueryFieldRef, AtscriptQueryNode, AtscriptRef, FlatOf, InferDataType, NavPropsOf, OwnPropsOf, PrimaryKeyOf, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptDataType, TAtscriptField, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TError, TErrorInfo, TFlattenOptions, TJsonSchema, TJsonSchemaOptions, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TUnionDiscriminator, TUserTextFields, TValidatorDiscriminator, TValidatorErrorCode, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValidatorTranslate, TValueResolver };