@atscript/typescript 0.1.102 → 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.
- package/dist/cli.cjs +244 -78
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{json-schema-CW_50s1W.mjs → json-schema-BGYiqs-J.mjs} +202 -69
- package/dist/{json-schema-B9xrfeoQ.cjs → json-schema-L8xJQiLc.cjs} +202 -69
- package/dist/{plugin-DORafTJq.cjs → plugin-2TNKdtc6.cjs} +43 -10
- package/dist/{plugin-CECXHL6M.mjs → plugin-BN6tnRsJ.mjs} +43 -10
- package/dist/test-utils.cjs +1 -1
- package/dist/test-utils.mjs +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.ts +14 -0
- package/dist/utils.mjs +1 -1
- package/package.json +2 -2
package/dist/cli.cjs
CHANGED
|
@@ -716,8 +716,25 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
716
716
|
}
|
|
717
717
|
return regex;
|
|
718
718
|
}
|
|
719
|
+
/**
|
|
720
|
+
* `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
|
|
721
|
+
* type does not itself accept `null`. Shared by the walk and the fast pre-check.
|
|
722
|
+
*/ function rejectsNullAsRequired(def) {
|
|
723
|
+
return def.metadata.size > 0 && !!def.metadata.get("meta.required") && !acceptsNull(def);
|
|
724
|
+
}
|
|
725
|
+
function acceptsNull(def) {
|
|
726
|
+
const type = def.type;
|
|
727
|
+
if (type.kind === "") {
|
|
728
|
+
const designType = type.designType;
|
|
729
|
+
return designType === "null" || designType === "any";
|
|
730
|
+
}
|
|
731
|
+
if (type.kind === "union") {
|
|
732
|
+
for (const item of type.items) if (item.optional || acceptsNull(item)) return true;
|
|
733
|
+
}
|
|
734
|
+
return false;
|
|
735
|
+
}
|
|
719
736
|
function fastCheck(def, value, flags, opts) {
|
|
720
|
-
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
737
|
+
if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
|
|
721
738
|
const type = def.type;
|
|
722
739
|
switch (type.kind) {
|
|
723
740
|
case "": return fastCheckFinal(def, value);
|
|
@@ -727,7 +744,7 @@ function fastCheck(def, value, flags, opts) {
|
|
|
727
744
|
const branchFlags = flags | IN_UNION;
|
|
728
745
|
for (const item of type.items) {
|
|
729
746
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
730
|
-
if (result !== FAIL) return result;
|
|
747
|
+
if (result !== FAIL) return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(def.metadata, value) : result;
|
|
731
748
|
}
|
|
732
749
|
return FAIL;
|
|
733
750
|
}
|
|
@@ -756,12 +773,9 @@ function fastCheckFinal(def, value) {
|
|
|
756
773
|
if (designType === "phantom") return PASS;
|
|
757
774
|
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
758
775
|
switch (designType) {
|
|
759
|
-
case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
|
|
760
|
-
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
|
|
761
|
-
case "boolean":
|
|
762
|
-
if (typeof value !== "boolean") return FAIL;
|
|
763
|
-
if (def.metadata.size === 0) return PASS;
|
|
764
|
-
return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
776
|
+
case "string": return typeof value === "string" ? fastCheckString(def.metadata, value) : FAIL;
|
|
777
|
+
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def.metadata, value) : FAIL;
|
|
778
|
+
case "boolean": return typeof value === "boolean" ? fastCheckBoolean(def.metadata, value) : FAIL;
|
|
765
779
|
case "any": return PASS;
|
|
766
780
|
case "never": return FAIL;
|
|
767
781
|
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
@@ -770,8 +784,24 @@ function fastCheckFinal(def, value) {
|
|
|
770
784
|
default: return BAIL;
|
|
771
785
|
}
|
|
772
786
|
}
|
|
773
|
-
|
|
774
|
-
|
|
787
|
+
/**
|
|
788
|
+
* Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
|
|
789
|
+
* (`null` / objects pass; the caller skips literal branches).
|
|
790
|
+
*/ function fastCheckUnionConstraints(metadata, value) {
|
|
791
|
+
if (metadata.size === 0) return PASS;
|
|
792
|
+
switch (typeof value) {
|
|
793
|
+
case "string": return fastCheckString(metadata, value);
|
|
794
|
+
case "number": return fastCheckNumber(metadata, value);
|
|
795
|
+
case "boolean": return fastCheckBoolean(metadata, value);
|
|
796
|
+
case "object": return Array.isArray(value) ? fastCheckArrayConstraints(metadata, value) : PASS;
|
|
797
|
+
default: return PASS;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
function fastCheckBoolean(metadata, value) {
|
|
801
|
+
if (metadata.size === 0) return PASS;
|
|
802
|
+
return metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
803
|
+
}
|
|
804
|
+
function fastCheckString(metadata, value) {
|
|
775
805
|
if (metadata.size === 0) return PASS;
|
|
776
806
|
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
777
807
|
const minLength = metadata.get("expect.minLength");
|
|
@@ -787,8 +817,7 @@ function fastCheckString(def, value) {
|
|
|
787
817
|
}
|
|
788
818
|
return PASS;
|
|
789
819
|
}
|
|
790
|
-
function fastCheckNumber(
|
|
791
|
-
const metadata = def.metadata;
|
|
820
|
+
function fastCheckNumber(metadata, value) {
|
|
792
821
|
if (metadata.size === 0) return PASS;
|
|
793
822
|
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
794
823
|
const min = metadata.get("expect.min");
|
|
@@ -831,11 +860,8 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
831
860
|
if (!Array.isArray(value)) return FAIL;
|
|
832
861
|
const metadata = def.metadata;
|
|
833
862
|
if (metadata.size > 0) {
|
|
834
|
-
const
|
|
835
|
-
if (
|
|
836
|
-
const maxLength = metadata.get("expect.maxLength");
|
|
837
|
-
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
838
|
-
if (metadata.get("expect.array.uniqueItems")) return BAIL;
|
|
863
|
+
const result = fastCheckArrayConstraints(metadata, value);
|
|
864
|
+
if (result !== PASS) return result;
|
|
839
865
|
}
|
|
840
866
|
const of = def.type.of;
|
|
841
867
|
const inUnion = (flags & IN_UNION) !== 0;
|
|
@@ -850,6 +876,13 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
850
876
|
}
|
|
851
877
|
return result;
|
|
852
878
|
}
|
|
879
|
+
function fastCheckArrayConstraints(metadata, value) {
|
|
880
|
+
const minLength = metadata.get("expect.minLength");
|
|
881
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
882
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
883
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
884
|
+
return metadata.get("expect.array.uniqueItems") ? BAIL : PASS;
|
|
885
|
+
}
|
|
853
886
|
|
|
854
887
|
//#endregion
|
|
855
888
|
//#region packages/typescript/src/runtime/validator.ts
|
|
@@ -965,7 +998,13 @@ function _define_property$4(obj, key, value) {
|
|
|
965
998
|
}
|
|
966
999
|
def = replaced;
|
|
967
1000
|
}
|
|
968
|
-
if (def.optional && (value === void 0 || value === null))
|
|
1001
|
+
if (def.optional && (value === void 0 || value === null)) {
|
|
1002
|
+
if (value === null && rejectsNullAsRequired(def)) {
|
|
1003
|
+
this.error(requiredMessage(def));
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
return true;
|
|
1007
|
+
}
|
|
969
1008
|
if (this.hasPlugins) for (const plugin of this.opts.plugins) {
|
|
970
1009
|
const result = plugin(this, def, value);
|
|
971
1010
|
if (result === false || result === true) return result;
|
|
@@ -1000,7 +1039,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1000
1039
|
this.stackErrors.push(null);
|
|
1001
1040
|
if (this.validateSafe(item, value)) {
|
|
1002
1041
|
this.stackErrors.pop();
|
|
1003
|
-
return
|
|
1042
|
+
return this.validateUnionConstraints(def, item, value);
|
|
1004
1043
|
}
|
|
1005
1044
|
const branchErrors = this.stackErrors.pop();
|
|
1006
1045
|
if (this.limitExceeded) this.limitExceeded = false;
|
|
@@ -1011,6 +1050,23 @@ function _define_property$4(obj, key, value) {
|
|
|
1011
1050
|
this.error(`Value does not match any of the allowed types: ${expected}`, void 0, details);
|
|
1012
1051
|
return false;
|
|
1013
1052
|
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
|
|
1055
|
+
* apply to the non-null value that matched `branch`, on top of the branch's own.
|
|
1056
|
+
* A literal branch (an enum value) is exempt: its value is fixed.
|
|
1057
|
+
*/ validateUnionConstraints(def, branch, value) {
|
|
1058
|
+
const metadata = def.metadata;
|
|
1059
|
+
if (metadata.size === 0 || branch.type.value !== void 0) return true;
|
|
1060
|
+
switch (typeof value) {
|
|
1061
|
+
case "string": return this.validateStringConstraints(metadata, value);
|
|
1062
|
+
case "number": return this.validateNumberConstraints(metadata, value);
|
|
1063
|
+
case "boolean": return this.validateBooleanConstraints(metadata, value);
|
|
1064
|
+
case "object":
|
|
1065
|
+
if (Array.isArray(value)) return this.validateArrayConstraints(metadata, value, arrayElementType(branch));
|
|
1066
|
+
return true;
|
|
1067
|
+
default: return true;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1014
1070
|
validateIntersection(def, value) {
|
|
1015
1071
|
for (const item of def.type.items) if (!this.validateSafe(item, value)) return false;
|
|
1016
1072
|
return true;
|
|
@@ -1037,7 +1093,22 @@ function _define_property$4(obj, key, value) {
|
|
|
1037
1093
|
this.error("Expected array");
|
|
1038
1094
|
return false;
|
|
1039
1095
|
}
|
|
1040
|
-
|
|
1096
|
+
if (def.metadata.size > 0 && !this.validateArrayConstraints(def.metadata, value, def.type.of)) return false;
|
|
1097
|
+
let i = 0;
|
|
1098
|
+
let passed = true;
|
|
1099
|
+
for (const item of value) {
|
|
1100
|
+
this.push(String(i));
|
|
1101
|
+
if (!this.validateSafe(def.type.of, item)) {
|
|
1102
|
+
passed = false;
|
|
1103
|
+
this.pop(true);
|
|
1104
|
+
if (this.limitExceeded) return false;
|
|
1105
|
+
} else this.pop(false);
|
|
1106
|
+
i++;
|
|
1107
|
+
}
|
|
1108
|
+
return passed;
|
|
1109
|
+
}
|
|
1110
|
+
/** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */ validateArrayConstraints(metadata, value, of) {
|
|
1111
|
+
const minLength = metadata.get("expect.minLength");
|
|
1041
1112
|
if (minLength !== void 0) {
|
|
1042
1113
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
1043
1114
|
if (value.length < length) {
|
|
@@ -1046,7 +1117,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1046
1117
|
return false;
|
|
1047
1118
|
}
|
|
1048
1119
|
}
|
|
1049
|
-
const maxLength =
|
|
1120
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
1050
1121
|
if (maxLength !== void 0) {
|
|
1051
1122
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1052
1123
|
if (value.length > length) {
|
|
@@ -1055,13 +1126,13 @@ function _define_property$4(obj, key, value) {
|
|
|
1055
1126
|
return false;
|
|
1056
1127
|
}
|
|
1057
1128
|
}
|
|
1058
|
-
const uniqueItems =
|
|
1129
|
+
const uniqueItems = metadata.get("expect.array.uniqueItems");
|
|
1059
1130
|
if (uniqueItems) {
|
|
1060
1131
|
const separator = "▼↩";
|
|
1061
1132
|
const seen = /* @__PURE__ */ new Set();
|
|
1062
1133
|
const keyProps = /* @__PURE__ */ new Set();
|
|
1063
|
-
if (
|
|
1064
|
-
for (const [key, val] of
|
|
1134
|
+
if (of?.type.kind === "object") {
|
|
1135
|
+
for (const [key, val] of of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
|
|
1065
1136
|
}
|
|
1066
1137
|
for (let idx = 0; idx < value.length; idx++) {
|
|
1067
1138
|
const item = value[idx];
|
|
@@ -1079,18 +1150,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1079
1150
|
seen.add(key);
|
|
1080
1151
|
}
|
|
1081
1152
|
}
|
|
1082
|
-
|
|
1083
|
-
let passed = true;
|
|
1084
|
-
for (const item of value) {
|
|
1085
|
-
this.push(String(i));
|
|
1086
|
-
if (!this.validateSafe(def.type.of, item)) {
|
|
1087
|
-
passed = false;
|
|
1088
|
-
this.pop(true);
|
|
1089
|
-
if (this.limitExceeded) return false;
|
|
1090
|
-
} else this.pop(false);
|
|
1091
|
-
i++;
|
|
1092
|
-
}
|
|
1093
|
-
return passed;
|
|
1153
|
+
return true;
|
|
1094
1154
|
}
|
|
1095
1155
|
validateObject(def, value) {
|
|
1096
1156
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
@@ -1228,8 +1288,11 @@ function _define_property$4(obj, key, value) {
|
|
|
1228
1288
|
}
|
|
1229
1289
|
}
|
|
1230
1290
|
validateString(def, value) {
|
|
1231
|
-
|
|
1232
|
-
|
|
1291
|
+
return this.validateStringConstraints(def.metadata, value);
|
|
1292
|
+
}
|
|
1293
|
+
/** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */ validateStringConstraints(metadata, value) {
|
|
1294
|
+
if (metadata.size === 0) return true;
|
|
1295
|
+
const filled = metadata.get("meta.required");
|
|
1233
1296
|
if (filled) {
|
|
1234
1297
|
if (value.trim().length === 0) {
|
|
1235
1298
|
const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
|
|
@@ -1237,7 +1300,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1237
1300
|
return false;
|
|
1238
1301
|
}
|
|
1239
1302
|
}
|
|
1240
|
-
const minLength =
|
|
1303
|
+
const minLength = metadata.get("expect.minLength");
|
|
1241
1304
|
if (minLength !== void 0) {
|
|
1242
1305
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
1243
1306
|
if (value.length < length) {
|
|
@@ -1246,7 +1309,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1246
1309
|
return false;
|
|
1247
1310
|
}
|
|
1248
1311
|
}
|
|
1249
|
-
const maxLength =
|
|
1312
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
1250
1313
|
if (maxLength !== void 0) {
|
|
1251
1314
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1252
1315
|
if (value.length > length) {
|
|
@@ -1255,7 +1318,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1255
1318
|
return false;
|
|
1256
1319
|
}
|
|
1257
1320
|
}
|
|
1258
|
-
const patterns =
|
|
1321
|
+
const patterns = metadata.get("expect.pattern");
|
|
1259
1322
|
for (const { pattern, flags, message } of patterns || []) {
|
|
1260
1323
|
if (!pattern) continue;
|
|
1261
1324
|
if (!getRegex(pattern, flags).test(value)) {
|
|
@@ -1266,14 +1329,17 @@ function _define_property$4(obj, key, value) {
|
|
|
1266
1329
|
return true;
|
|
1267
1330
|
}
|
|
1268
1331
|
validateNumber(def, value) {
|
|
1269
|
-
|
|
1270
|
-
|
|
1332
|
+
return this.validateNumberConstraints(def.metadata, value);
|
|
1333
|
+
}
|
|
1334
|
+
/** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
|
|
1335
|
+
if (metadata.size === 0) return true;
|
|
1336
|
+
const int = metadata.get("expect.int");
|
|
1271
1337
|
if (int && value % 1 !== 0) {
|
|
1272
1338
|
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
1273
1339
|
this.error(message);
|
|
1274
1340
|
return false;
|
|
1275
1341
|
}
|
|
1276
|
-
const min =
|
|
1342
|
+
const min = metadata.get("expect.min");
|
|
1277
1343
|
if (min !== void 0) {
|
|
1278
1344
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
1279
1345
|
if (value < minValue) {
|
|
@@ -1282,7 +1348,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1282
1348
|
return false;
|
|
1283
1349
|
}
|
|
1284
1350
|
}
|
|
1285
|
-
const max =
|
|
1351
|
+
const max = metadata.get("expect.max");
|
|
1286
1352
|
if (max !== void 0) {
|
|
1287
1353
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
1288
1354
|
if (value > maxValue) {
|
|
@@ -1294,8 +1360,11 @@ function _define_property$4(obj, key, value) {
|
|
|
1294
1360
|
return true;
|
|
1295
1361
|
}
|
|
1296
1362
|
validateBoolean(def, value) {
|
|
1297
|
-
|
|
1298
|
-
|
|
1363
|
+
return this.validateBooleanConstraints(def.metadata, value);
|
|
1364
|
+
}
|
|
1365
|
+
/** `@meta.required` check for a boolean value (must be `true`). */ validateBooleanConstraints(metadata, value) {
|
|
1366
|
+
if (metadata.size === 0) return true;
|
|
1367
|
+
const filled = metadata.get("meta.required");
|
|
1299
1368
|
if (filled) {
|
|
1300
1369
|
if (value !== true) {
|
|
1301
1370
|
const message = typeof filled === "object" && filled.message ? filled.message : `Must be checked`;
|
|
@@ -1335,6 +1404,23 @@ function _define_property$4(obj, key, value) {
|
|
|
1335
1404
|
if (this.hasReplace) this.replaceCache = /* @__PURE__ */ new WeakMap();
|
|
1336
1405
|
}
|
|
1337
1406
|
};
|
|
1407
|
+
/** The `@meta.required` error message for `def` (custom message or the type's default). */ function requiredMessage(def) {
|
|
1408
|
+
const filled = def.metadata.get("meta.required");
|
|
1409
|
+
if (typeof filled === "object" && filled.message) return filled.message;
|
|
1410
|
+
return def.type.kind === "" && def.type.designType === "boolean" ? "Must be checked" : "Must not be empty";
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* Element type of the array a union branch holds — through nested unions too
|
|
1414
|
+
* (`Items | undefined` with `type Items = Item[] | null`), so `@expect.array.key`
|
|
1415
|
+
* fields of the elements are found.
|
|
1416
|
+
*/ function arrayElementType(branch) {
|
|
1417
|
+
const type = branch.type;
|
|
1418
|
+
if (type.kind === "array") return type.of;
|
|
1419
|
+
if (type.kind === "union") for (const item of type.items) {
|
|
1420
|
+
const of = arrayElementType(item);
|
|
1421
|
+
if (of) return of;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1338
1424
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
|
|
1339
1425
|
constructor(errors) {
|
|
1340
1426
|
super(`${errors[0].path ? errors[0].path + ": " : ""}${errors[0].message}`), _define_property$4(this, "errors", void 0), _define_property$4(this, "name", void 0), this.errors = errors, this.name = "Validation Error";
|
|
@@ -1703,10 +1789,7 @@ function cloneTypeDef(type) {
|
|
|
1703
1789
|
type: "array",
|
|
1704
1790
|
items: build(d.type.of)
|
|
1705
1791
|
};
|
|
1706
|
-
|
|
1707
|
-
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1708
|
-
const maxLength = meta.get("expect.maxLength");
|
|
1709
|
-
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1792
|
+
applyArrayConstraints(schema, meta);
|
|
1710
1793
|
return schema;
|
|
1711
1794
|
},
|
|
1712
1795
|
union(d) {
|
|
@@ -1726,7 +1809,9 @@ function cloneTypeDef(type) {
|
|
|
1726
1809
|
}
|
|
1727
1810
|
};
|
|
1728
1811
|
}
|
|
1729
|
-
|
|
1812
|
+
const anyOf = d.type.items.map(build);
|
|
1813
|
+
if (meta.size > 0) applyUnionConstraints(anyOf, meta);
|
|
1814
|
+
return { anyOf };
|
|
1730
1815
|
},
|
|
1731
1816
|
intersection(d) {
|
|
1732
1817
|
return { allOf: d.type.items.map(build) };
|
|
@@ -1744,24 +1829,8 @@ function cloneTypeDef(type) {
|
|
|
1744
1829
|
if (d.type.designType && d.type.designType !== "any") {
|
|
1745
1830
|
const dt = d.type.designType;
|
|
1746
1831
|
schema.type = dt === "undefined" ? "null" : dt === "decimal" ? "string" : dt;
|
|
1747
|
-
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1748
|
-
}
|
|
1749
|
-
if (schema.type === "string") {
|
|
1750
|
-
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1751
|
-
const minLength = meta.get("expect.minLength");
|
|
1752
|
-
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1753
|
-
const maxLength = meta.get("expect.maxLength");
|
|
1754
|
-
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1755
|
-
const patterns = meta.get("expect.pattern");
|
|
1756
|
-
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1757
|
-
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1758
|
-
}
|
|
1759
|
-
if (schema.type === "number" || schema.type === "integer") {
|
|
1760
|
-
const min = meta.get("expect.min");
|
|
1761
|
-
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1762
|
-
const max = meta.get("expect.max");
|
|
1763
|
-
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1764
1832
|
}
|
|
1833
|
+
applyScalarConstraints(schema, meta);
|
|
1765
1834
|
return schema;
|
|
1766
1835
|
}
|
|
1767
1836
|
});
|
|
@@ -1773,6 +1842,70 @@ function cloneTypeDef(type) {
|
|
|
1773
1842
|
};
|
|
1774
1843
|
return schema;
|
|
1775
1844
|
}
|
|
1845
|
+
/** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
|
|
1846
|
+
const minLength = meta.get("expect.minLength");
|
|
1847
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1848
|
+
const maxLength = meta.get("expect.maxLength");
|
|
1849
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1850
|
+
}
|
|
1851
|
+
/** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
|
|
1852
|
+
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1853
|
+
if (schema.type === "string") {
|
|
1854
|
+
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1855
|
+
const minLength = meta.get("expect.minLength");
|
|
1856
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1857
|
+
const maxLength = meta.get("expect.maxLength");
|
|
1858
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1859
|
+
const patterns = meta.get("expect.pattern");
|
|
1860
|
+
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1861
|
+
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1862
|
+
}
|
|
1863
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
1864
|
+
const min = meta.get("expect.min");
|
|
1865
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1866
|
+
const max = meta.get("expect.max");
|
|
1867
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
const LOWER_BOUNDS = new Set([
|
|
1871
|
+
"minimum",
|
|
1872
|
+
"minLength",
|
|
1873
|
+
"minItems"
|
|
1874
|
+
]);
|
|
1875
|
+
const UPPER_BOUNDS = new Set([
|
|
1876
|
+
"maximum",
|
|
1877
|
+
"maxLength",
|
|
1878
|
+
"maxItems"
|
|
1879
|
+
]);
|
|
1880
|
+
/**
|
|
1881
|
+
* Puts union-level constraints onto the matching `anyOf` members (numbers, strings,
|
|
1882
|
+
* arrays; nested `anyOf` members recursively). When a member already has the same
|
|
1883
|
+
* bound, the stricter one wins; patterns accumulate.
|
|
1884
|
+
*/ function applyUnionConstraints(members, meta) {
|
|
1885
|
+
for (const member of members) {
|
|
1886
|
+
if (Array.isArray(member.anyOf)) {
|
|
1887
|
+
applyUnionConstraints(member.anyOf, meta);
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
if (member.const !== void 0) continue;
|
|
1891
|
+
const type = member.type;
|
|
1892
|
+
const extra = { type };
|
|
1893
|
+
if (type === "array") applyArrayConstraints(extra, meta);
|
|
1894
|
+
else if (type === "string" || type === "number" || type === "integer") applyScalarConstraints(extra, meta);
|
|
1895
|
+
else continue;
|
|
1896
|
+
if (extra.type === "integer") member.type = "integer";
|
|
1897
|
+
for (const [key, value] of Object.entries(extra)) {
|
|
1898
|
+
if (key === "type") continue;
|
|
1899
|
+
if (key === "pattern" || key === "allOf") {
|
|
1900
|
+
const patterns = key === "pattern" ? [{ pattern: value }] : value;
|
|
1901
|
+
if (member.pattern === void 0 && key === "pattern" && !member.allOf) member.pattern = value;
|
|
1902
|
+
else member.allOf = (member.allOf || []).concat(patterns);
|
|
1903
|
+
} else if (member[key] === void 0) member[key] = value;
|
|
1904
|
+
else if (LOWER_BOUNDS.has(key)) member[key] = Math.max(member[key], value);
|
|
1905
|
+
else if (UPPER_BOUNDS.has(key)) member[key] = Math.min(member[key], value);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1776
1909
|
|
|
1777
1910
|
//#endregion
|
|
1778
1911
|
//#region packages/typescript/src/codegen/js-renderer.ts
|
|
@@ -2156,7 +2289,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2156
2289
|
const ref = node;
|
|
2157
2290
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
2158
2291
|
const handle = this.toAnnotatedHandle(decl, true);
|
|
2159
|
-
|
|
2292
|
+
if (skipAnnotations) return handle;
|
|
2293
|
+
const own = this.doc.evalAnnotationsForNode(node);
|
|
2294
|
+
const referred = this.refTargetAnnotations(ref, decl);
|
|
2295
|
+
return this.applyExpectAnnotations(handle, referred ? this.doc.mergeNodesAnnotations(referred, own) : own);
|
|
2160
2296
|
}
|
|
2161
2297
|
case "primitive": {
|
|
2162
2298
|
const prim = node;
|
|
@@ -2238,6 +2374,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2238
2374
|
}, true);
|
|
2239
2375
|
break;
|
|
2240
2376
|
case "expect.int":
|
|
2377
|
+
case "meta.required":
|
|
2241
2378
|
handle.annotate(a.name, true);
|
|
2242
2379
|
break;
|
|
2243
2380
|
case "expect.array.uniqueItems":
|
|
@@ -2257,12 +2394,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2257
2394
|
case "ref": {
|
|
2258
2395
|
const ref = node;
|
|
2259
2396
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
return this;
|
|
2265
|
-
}
|
|
2397
|
+
const primitive = this.inlinedPrimitive(ref, decl);
|
|
2398
|
+
if (primitive) {
|
|
2399
|
+
this.annotateType(primitive, name);
|
|
2400
|
+
return this;
|
|
2266
2401
|
}
|
|
2267
2402
|
if (this._adHocAnnotations && this.hasAdHocAnnotationsThroughRef()) {
|
|
2268
2403
|
let resolved = decl ? this.doc.mergeIntersection(decl) : void 0;
|
|
@@ -2413,13 +2548,44 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
2413
2548
|
}
|
|
2414
2549
|
defineGroup(node) {
|
|
2415
2550
|
const items = node.unwrap();
|
|
2416
|
-
for (const item of items) this.write(".item(").indent().annotateType(item).write(" .$type").writeln(`)`).unindent();
|
|
2551
|
+
for (const item of items) this.write(".item(").indent().annotateType(item).defineInlinePrimitiveMetadata(item).write(" .$type").writeln(`)`).unindent();
|
|
2417
2552
|
return this;
|
|
2418
2553
|
}
|
|
2419
2554
|
defineArray(node) {
|
|
2420
|
-
|
|
2555
|
+
const of = node.getDefinition();
|
|
2556
|
+
this.write(".of(").indent().annotateType(of).defineInlinePrimitiveMetadata(of).write(" .$type").writeln(`)`).unindent();
|
|
2421
2557
|
return this;
|
|
2422
2558
|
}
|
|
2559
|
+
/**
|
|
2560
|
+
* A built-in primitive extension used directly as a union / tuple member or array
|
|
2561
|
+
* element (`number.int | null`, `string.email[]`) is inlined by `annotateType` without
|
|
2562
|
+
* its built-in annotations (`expect.int`, the email pattern, …) — a prop gets them
|
|
2563
|
+
* through `defineMetadata`, a member has no prop to carry them, so emit them here.
|
|
2564
|
+
*/ defineInlinePrimitiveMetadata(node) {
|
|
2565
|
+
const primitive = node && this.inlinedPrimitive(node);
|
|
2566
|
+
if (primitive?.annotations?.length) {
|
|
2567
|
+
this.indent();
|
|
2568
|
+
for (const an of primitive.annotations) this.resolveAnnotationValue(primitive, an);
|
|
2569
|
+
this.unindent();
|
|
2570
|
+
}
|
|
2571
|
+
return this;
|
|
2572
|
+
}
|
|
2573
|
+
/**
|
|
2574
|
+
* The built-in primitive a ref is inlined as by `annotateType` — not a named alias that
|
|
2575
|
+
* resolves to one (`type MyString = string`), which is emitted as a `refTo`.
|
|
2576
|
+
*/ inlinedPrimitive(node, decl = (0, _atscript_core.isRef)(node) ? this.doc.unwindType(node.id, node.chain)?.def : void 0) {
|
|
2577
|
+
if (!(0, _atscript_core.isRef)(node) || !(0, _atscript_core.isPrimitive)(decl)) return;
|
|
2578
|
+
const ownerDecl = this.resolveOwner(this.doc, node.id);
|
|
2579
|
+
if (ownerDecl?.node && (ownerDecl.node.entity === "type" || ownerDecl.node.entity === "interface")) return;
|
|
2580
|
+
return decl;
|
|
2581
|
+
}
|
|
2582
|
+
/** Annotations a ref inherits from its target (see `annotateType`'s ref case). */ refTargetAnnotations(ref, decl) {
|
|
2583
|
+
const primitive = this.inlinedPrimitive(ref, decl);
|
|
2584
|
+
if (primitive) return primitive.annotations;
|
|
2585
|
+
if (ref.hasChain) return;
|
|
2586
|
+
const ownerDecl = this.resolveOwner(this.doc, ref.id);
|
|
2587
|
+
return ownerDecl?.node ? ownerDecl.doc.filterPassedWhenReferred(ownerDecl.doc.evalAnnotationsForNode(ownerDecl.node)) : void 0;
|
|
2588
|
+
}
|
|
2423
2589
|
defineMetadata(node) {
|
|
2424
2590
|
let annotations;
|
|
2425
2591
|
const nodeDef = node.getDefinition?.();
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
2
|
+
const require_plugin = require('./plugin-2TNKdtc6.cjs');
|
|
3
3
|
|
|
4
4
|
exports.default = require_plugin.tsPlugin;
|
|
5
5
|
exports.tsPlugin = require_plugin.tsPlugin;
|
package/dist/index.mjs
CHANGED