@atscript/typescript 0.1.101 → 0.1.103

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.
@@ -25,7 +25,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
25
25
  }) : target, mod));
26
26
 
27
27
  //#endregion
28
- const require_json_schema = require('./json-schema-CtQ_rzJn.cjs');
28
+ const require_json_schema = require('./json-schema-L8xJQiLc.cjs');
29
29
  let path = require("path");
30
30
  path = __toESM(path);
31
31
  let _atscript_core = require("@atscript/core");
@@ -1043,7 +1043,10 @@ var JsRenderer = class extends BaseRenderer {
1043
1043
  const ref = node;
1044
1044
  const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
1045
1045
  const handle = this.toAnnotatedHandle(decl, true);
1046
- return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
1046
+ if (skipAnnotations) return handle;
1047
+ const own = this.doc.evalAnnotationsForNode(node);
1048
+ const referred = this.refTargetAnnotations(ref, decl);
1049
+ return this.applyExpectAnnotations(handle, referred ? this.doc.mergeNodesAnnotations(referred, own) : own);
1047
1050
  }
1048
1051
  case "primitive": {
1049
1052
  const prim = node;
@@ -1125,6 +1128,7 @@ var JsRenderer = class extends BaseRenderer {
1125
1128
  }, true);
1126
1129
  break;
1127
1130
  case "expect.int":
1131
+ case "meta.required":
1128
1132
  handle.annotate(a.name, true);
1129
1133
  break;
1130
1134
  case "expect.array.uniqueItems":
@@ -1144,12 +1148,10 @@ var JsRenderer = class extends BaseRenderer {
1144
1148
  case "ref": {
1145
1149
  const ref = node;
1146
1150
  const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
1147
- if ((0, _atscript_core.isPrimitive)(decl)) {
1148
- const ownerDecl = this.resolveOwner(this.doc, ref.id);
1149
- if (!ownerDecl?.node || ownerDecl.node.entity !== "type" && ownerDecl.node.entity !== "interface") {
1150
- this.annotateType(decl, name);
1151
- return this;
1152
- }
1151
+ const primitive = this.inlinedPrimitive(ref, decl);
1152
+ if (primitive) {
1153
+ this.annotateType(primitive, name);
1154
+ return this;
1153
1155
  }
1154
1156
  if (this._adHocAnnotations && this.hasAdHocAnnotationsThroughRef()) {
1155
1157
  let resolved = decl ? this.doc.mergeIntersection(decl) : void 0;
@@ -1300,13 +1302,44 @@ var JsRenderer = class extends BaseRenderer {
1300
1302
  }
1301
1303
  defineGroup(node) {
1302
1304
  const items = node.unwrap();
1303
- for (const item of items) this.write(".item(").indent().annotateType(item).write(" .$type").writeln(`)`).unindent();
1305
+ for (const item of items) this.write(".item(").indent().annotateType(item).defineInlinePrimitiveMetadata(item).write(" .$type").writeln(`)`).unindent();
1304
1306
  return this;
1305
1307
  }
1306
1308
  defineArray(node) {
1307
- this.write(".of(").indent().annotateType(node.getDefinition()).write(" .$type").writeln(`)`).unindent();
1309
+ const of = node.getDefinition();
1310
+ this.write(".of(").indent().annotateType(of).defineInlinePrimitiveMetadata(of).write(" .$type").writeln(`)`).unindent();
1311
+ return this;
1312
+ }
1313
+ /**
1314
+ * A built-in primitive extension used directly as a union / tuple member or array
1315
+ * element (`number.int | null`, `string.email[]`) is inlined by `annotateType` without
1316
+ * its built-in annotations (`expect.int`, the email pattern, …) — a prop gets them
1317
+ * through `defineMetadata`, a member has no prop to carry them, so emit them here.
1318
+ */ defineInlinePrimitiveMetadata(node) {
1319
+ const primitive = node && this.inlinedPrimitive(node);
1320
+ if (primitive?.annotations?.length) {
1321
+ this.indent();
1322
+ for (const an of primitive.annotations) this.resolveAnnotationValue(primitive, an);
1323
+ this.unindent();
1324
+ }
1308
1325
  return this;
1309
1326
  }
1327
+ /**
1328
+ * The built-in primitive a ref is inlined as by `annotateType` — not a named alias that
1329
+ * resolves to one (`type MyString = string`), which is emitted as a `refTo`.
1330
+ */ inlinedPrimitive(node, decl = (0, _atscript_core.isRef)(node) ? this.doc.unwindType(node.id, node.chain)?.def : void 0) {
1331
+ if (!(0, _atscript_core.isRef)(node) || !(0, _atscript_core.isPrimitive)(decl)) return;
1332
+ const ownerDecl = this.resolveOwner(this.doc, node.id);
1333
+ if (ownerDecl?.node && (ownerDecl.node.entity === "type" || ownerDecl.node.entity === "interface")) return;
1334
+ return decl;
1335
+ }
1336
+ /** Annotations a ref inherits from its target (see `annotateType`'s ref case). */ refTargetAnnotations(ref, decl) {
1337
+ const primitive = this.inlinedPrimitive(ref, decl);
1338
+ if (primitive) return primitive.annotations;
1339
+ if (ref.hasChain) return;
1340
+ const ownerDecl = this.resolveOwner(this.doc, ref.id);
1341
+ return ownerDecl?.node ? ownerDecl.doc.filterPassedWhenReferred(ownerDecl.doc.evalAnnotationsForNode(ownerDecl.node)) : void 0;
1342
+ }
1310
1343
  defineMetadata(node) {
1311
1344
  let annotations;
1312
1345
  const nodeDef = node.getDefinition?.();
@@ -1,4 +1,4 @@
1
- import { l as defineAnnotatedType, t as buildJsonSchema } from "./json-schema-BogNt-9w.mjs";
1
+ import { l as defineAnnotatedType, t as buildJsonSchema } from "./json-schema-BGYiqs-J.mjs";
2
2
  import path from "path";
3
3
  import { DEFAULT_FORMAT, flattenInterfaceNode, getRelPath, hasNavPropAnnotation, isArray, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isInterface, isPrimitive, isProp, isQueryLogical, isRef, isStructure } from "@atscript/core";
4
4
 
@@ -1015,7 +1015,10 @@ var JsRenderer = class extends BaseRenderer {
1015
1015
  const ref = node;
1016
1016
  const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
1017
1017
  const handle = this.toAnnotatedHandle(decl, true);
1018
- return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
1018
+ if (skipAnnotations) return handle;
1019
+ const own = this.doc.evalAnnotationsForNode(node);
1020
+ const referred = this.refTargetAnnotations(ref, decl);
1021
+ return this.applyExpectAnnotations(handle, referred ? this.doc.mergeNodesAnnotations(referred, own) : own);
1019
1022
  }
1020
1023
  case "primitive": {
1021
1024
  const prim = node;
@@ -1097,6 +1100,7 @@ var JsRenderer = class extends BaseRenderer {
1097
1100
  }, true);
1098
1101
  break;
1099
1102
  case "expect.int":
1103
+ case "meta.required":
1100
1104
  handle.annotate(a.name, true);
1101
1105
  break;
1102
1106
  case "expect.array.uniqueItems":
@@ -1116,12 +1120,10 @@ var JsRenderer = class extends BaseRenderer {
1116
1120
  case "ref": {
1117
1121
  const ref = node;
1118
1122
  const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
1119
- if (isPrimitive(decl)) {
1120
- const ownerDecl = this.resolveOwner(this.doc, ref.id);
1121
- if (!ownerDecl?.node || ownerDecl.node.entity !== "type" && ownerDecl.node.entity !== "interface") {
1122
- this.annotateType(decl, name);
1123
- return this;
1124
- }
1123
+ const primitive = this.inlinedPrimitive(ref, decl);
1124
+ if (primitive) {
1125
+ this.annotateType(primitive, name);
1126
+ return this;
1125
1127
  }
1126
1128
  if (this._adHocAnnotations && this.hasAdHocAnnotationsThroughRef()) {
1127
1129
  let resolved = decl ? this.doc.mergeIntersection(decl) : void 0;
@@ -1272,13 +1274,44 @@ var JsRenderer = class extends BaseRenderer {
1272
1274
  }
1273
1275
  defineGroup(node) {
1274
1276
  const items = node.unwrap();
1275
- for (const item of items) this.write(".item(").indent().annotateType(item).write(" .$type").writeln(`)`).unindent();
1277
+ for (const item of items) this.write(".item(").indent().annotateType(item).defineInlinePrimitiveMetadata(item).write(" .$type").writeln(`)`).unindent();
1276
1278
  return this;
1277
1279
  }
1278
1280
  defineArray(node) {
1279
- this.write(".of(").indent().annotateType(node.getDefinition()).write(" .$type").writeln(`)`).unindent();
1281
+ const of = node.getDefinition();
1282
+ this.write(".of(").indent().annotateType(of).defineInlinePrimitiveMetadata(of).write(" .$type").writeln(`)`).unindent();
1283
+ return this;
1284
+ }
1285
+ /**
1286
+ * A built-in primitive extension used directly as a union / tuple member or array
1287
+ * element (`number.int | null`, `string.email[]`) is inlined by `annotateType` without
1288
+ * its built-in annotations (`expect.int`, the email pattern, …) — a prop gets them
1289
+ * through `defineMetadata`, a member has no prop to carry them, so emit them here.
1290
+ */ defineInlinePrimitiveMetadata(node) {
1291
+ const primitive = node && this.inlinedPrimitive(node);
1292
+ if (primitive?.annotations?.length) {
1293
+ this.indent();
1294
+ for (const an of primitive.annotations) this.resolveAnnotationValue(primitive, an);
1295
+ this.unindent();
1296
+ }
1280
1297
  return this;
1281
1298
  }
1299
+ /**
1300
+ * The built-in primitive a ref is inlined as by `annotateType` — not a named alias that
1301
+ * resolves to one (`type MyString = string`), which is emitted as a `refTo`.
1302
+ */ inlinedPrimitive(node, decl = isRef(node) ? this.doc.unwindType(node.id, node.chain)?.def : void 0) {
1303
+ if (!isRef(node) || !isPrimitive(decl)) return;
1304
+ const ownerDecl = this.resolveOwner(this.doc, node.id);
1305
+ if (ownerDecl?.node && (ownerDecl.node.entity === "type" || ownerDecl.node.entity === "interface")) return;
1306
+ return decl;
1307
+ }
1308
+ /** Annotations a ref inherits from its target (see `annotateType`'s ref case). */ refTargetAnnotations(ref, decl) {
1309
+ const primitive = this.inlinedPrimitive(ref, decl);
1310
+ if (primitive) return primitive.annotations;
1311
+ if (ref.hasChain) return;
1312
+ const ownerDecl = this.resolveOwner(this.doc, ref.id);
1313
+ return ownerDecl?.node ? ownerDecl.doc.filterPassedWhenReferred(ownerDecl.doc.evalAnnotationsForNode(ownerDecl.node)) : void 0;
1314
+ }
1282
1315
  defineMetadata(node) {
1283
1316
  let annotations;
1284
1317
  const nodeDef = node.getDefinition?.();
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_plugin = require('./plugin-Cu6Nez-H.cjs');
2
+ const require_plugin = require('./plugin-2TNKdtc6.cjs');
3
3
  let path = require("path");
4
4
  path = require_plugin.__toESM(path);
5
5
  let _atscript_core = require("@atscript/core");
@@ -1,4 +1,4 @@
1
- import { t as tsPlugin } from "./plugin-DG3jlbSN.mjs";
1
+ import { t as tsPlugin } from "./plugin-BN6tnRsJ.mjs";
2
2
  import path from "path";
3
3
  import { build } from "@atscript/core";
4
4
  import { readFileSync, writeFileSync } from "fs";
package/dist/utils.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_json_schema = require('./json-schema-CtQ_rzJn.cjs');
2
+ const require_json_schema = require('./json-schema-L8xJQiLc.cjs');
3
3
 
4
4
  //#region packages/typescript/src/runtime/coerce.ts
5
5
  /**
@@ -44,36 +44,40 @@ const require_json_schema = require('./json-schema-CtQ_rzJn.cjs');
44
44
  }
45
45
  /** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
46
46
  if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
47
- return require_json_schema.forAnnotatedType(def, {
48
- final: (d) => {
49
- const result = scalar(d.type.designType, value);
50
- if (result !== NO_MATCH && d.type.value !== void 0 && result !== d.type.value) return NO_MATCH;
47
+ const type = def.type;
48
+ switch (type.kind) {
49
+ case "": {
50
+ const final = type;
51
+ if (final.designType === "phantom") return value;
52
+ const result = scalar(final.designType, value);
53
+ if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
51
54
  return result;
52
- },
53
- phantom: () => value,
54
- object: (d) => {
55
+ }
56
+ case "object":
55
57
  if (!isPlainObject(value)) return NO_MATCH;
56
- return coerceProps(d, value);
57
- },
58
- array: (d) => Array.isArray(value) ? coerceItems(value, d.type.of) : NO_MATCH,
59
- tuple: (d) => Array.isArray(value) && value.length === d.type.items.length ? coerceItems(value, void 0, d.type.items) : NO_MATCH,
60
- union: (d) => {
61
- for (const item of d.type.items) {
58
+ return coerceProps(def, value);
59
+ case "array": return Array.isArray(value) ? coerceItems(value, type.of) : NO_MATCH;
60
+ case "tuple": {
61
+ const items = type.items;
62
+ return Array.isArray(value) && value.length === items.length ? coerceItems(value, void 0, items) : NO_MATCH;
63
+ }
64
+ case "union":
65
+ for (const item of type.items) {
62
66
  const result = coerce(item, value);
63
67
  if (result !== NO_MATCH) return result;
64
68
  }
65
69
  return NO_MATCH;
66
- },
67
- intersection: (d) => {
70
+ case "intersection": {
68
71
  let current = value;
69
- for (const item of d.type.items) {
72
+ for (const item of type.items) {
70
73
  const result = coerce(item, current);
71
74
  if (result === NO_MATCH) return NO_MATCH;
72
75
  current = result;
73
76
  }
74
77
  return current;
75
78
  }
76
- });
79
+ default: throw new Error(`Unknown type kind "${type.kind}"`);
80
+ }
77
81
  }
78
82
  function scalar(designType, value) {
79
83
  switch (designType) {
package/dist/utils.d.ts CHANGED
@@ -80,18 +80,39 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
80
80
  * @throws {ValidatorError} When validation fails and `safe` is not `true`.
81
81
  */
82
82
  validate<TT = DataType>(value: any, safe?: boolean, context?: unknown): value is TT;
83
+ /**
84
+ * Plain `Validator` without plugins / `replace`: eligible for the allocation-free
85
+ * shortcuts. Subclasses may override the protected `validate*` hooks, so they
86
+ * always take the full walk. (A getter, not a constructor-set field: validators are
87
+ * often built per call, so construction stays as cheap as before.)
88
+ */
89
+ private get shortcuts();
83
90
  protected validateSafe(def: TAtscriptAnnotatedType, value: any): boolean;
84
91
  protected get path(): string;
85
92
  protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
86
93
  protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
94
+ /**
95
+ * Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
96
+ * apply to the non-null value that matched `branch`, on top of the branch's own.
97
+ * A literal branch (an enum value) is exempt: its value is fixed.
98
+ */
99
+ protected validateUnionConstraints(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, branch: TAtscriptAnnotatedType, value: any): boolean;
87
100
  protected validateIntersection(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
88
101
  protected validateTuple(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
89
102
  protected validateArray(def: TAtscriptAnnotatedType<TAtscriptTypeArray>, value: any): boolean;
103
+ /** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */
104
+ protected validateArrayConstraints(metadata: TMetadataMap<AtscriptMetadata>, value: any[], of?: TAtscriptAnnotatedType): boolean;
90
105
  protected validateObject(def: TAtscriptAnnotatedType<TAtscriptTypeObject>, value: any): boolean;
91
106
  protected validatePrimitive(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: any): boolean;
92
107
  protected validateString(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: string): boolean;
108
+ /** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */
109
+ protected validateStringConstraints(metadata: TMetadataMap<AtscriptMetadata>, value: string): boolean;
93
110
  protected validateNumber(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: number): boolean;
111
+ /** `@expect.int` / `min` / `max` checks for a number value. */
112
+ protected validateNumberConstraints(metadata: TMetadataMap<AtscriptMetadata>, value: number): boolean;
94
113
  protected validateBoolean(def: TAtscriptAnnotatedType<TAtscriptTypeFinal>, value: boolean): boolean;
114
+ /** `@meta.required` check for a boolean value (must be `true`). */
115
+ protected validateBooleanConstraints(metadata: TMetadataMap<AtscriptMetadata>, value: boolean): boolean;
95
116
  }
96
117
  /** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */
97
118
  declare class ValidatorError extends Error {
package/dist/utils.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTypeOfPrimitive, f as isPhantomType, i as mergeJsonSchemas, l as defineAnnotatedType, m as ValidatorError, n as detectDiscriminator, o as annotate, p as Validator, r as fromJsonSchema, s as cloneRefProp, t as buildJsonSchema, u as isAnnotatedType } from "./json-schema-BogNt-9w.mjs";
1
+ import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTypeOfPrimitive, f as isPhantomType, i as mergeJsonSchemas, l as defineAnnotatedType, m as ValidatorError, n as detectDiscriminator, o as annotate, p as Validator, r as fromJsonSchema, s as cloneRefProp, t as buildJsonSchema, u as isAnnotatedType } from "./json-schema-BGYiqs-J.mjs";
2
2
 
3
3
  //#region packages/typescript/src/runtime/coerce.ts
4
4
  /**
@@ -43,36 +43,40 @@ import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTy
43
43
  }
44
44
  /** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
45
45
  if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
46
- return forAnnotatedType(def, {
47
- final: (d) => {
48
- const result = scalar(d.type.designType, value);
49
- if (result !== NO_MATCH && d.type.value !== void 0 && result !== d.type.value) return NO_MATCH;
46
+ const type = def.type;
47
+ switch (type.kind) {
48
+ case "": {
49
+ const final = type;
50
+ if (final.designType === "phantom") return value;
51
+ const result = scalar(final.designType, value);
52
+ if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
50
53
  return result;
51
- },
52
- phantom: () => value,
53
- object: (d) => {
54
+ }
55
+ case "object":
54
56
  if (!isPlainObject(value)) return NO_MATCH;
55
- return coerceProps(d, value);
56
- },
57
- array: (d) => Array.isArray(value) ? coerceItems(value, d.type.of) : NO_MATCH,
58
- tuple: (d) => Array.isArray(value) && value.length === d.type.items.length ? coerceItems(value, void 0, d.type.items) : NO_MATCH,
59
- union: (d) => {
60
- for (const item of d.type.items) {
57
+ return coerceProps(def, value);
58
+ case "array": return Array.isArray(value) ? coerceItems(value, type.of) : NO_MATCH;
59
+ case "tuple": {
60
+ const items = type.items;
61
+ return Array.isArray(value) && value.length === items.length ? coerceItems(value, void 0, items) : NO_MATCH;
62
+ }
63
+ case "union":
64
+ for (const item of type.items) {
61
65
  const result = coerce(item, value);
62
66
  if (result !== NO_MATCH) return result;
63
67
  }
64
68
  return NO_MATCH;
65
- },
66
- intersection: (d) => {
69
+ case "intersection": {
67
70
  let current = value;
68
- for (const item of d.type.items) {
71
+ for (const item of type.items) {
69
72
  const result = coerce(item, current);
70
73
  if (result === NO_MATCH) return NO_MATCH;
71
74
  current = result;
72
75
  }
73
76
  return current;
74
77
  }
75
- });
78
+ default: throw new Error(`Unknown type kind "${type.kind}"`);
79
+ }
76
80
  }
77
81
  function scalar(designType, value) {
78
82
  switch (designType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/typescript",
3
- "version": "0.1.101",
3
+ "version": "0.1.103",
4
4
  "description": "Atscript: typescript-gen support.",
5
5
  "keywords": [
6
6
  "annotations",
@@ -73,7 +73,7 @@
73
73
  },
74
74
  "peerDependencies": {
75
75
  "@atscript/db": "^0.1.97",
76
- "@atscript/core": "^0.1.101"
76
+ "@atscript/core": "^0.1.103"
77
77
  },
78
78
  "peerDependenciesMeta": {
79
79
  "@atscript/db": {