@atscript/typescript 0.1.6 → 0.1.8

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 CHANGED
@@ -601,14 +601,22 @@ var Validator = class {
601
601
  return false;
602
602
  }
603
603
  const minLength = def.metadata.get("expect.minLength");
604
- if (typeof minLength === "number" && value.length < minLength) {
605
- this.error(`Expected minimum length of ${minLength} items, got ${value.length} items`);
606
- return false;
604
+ if (minLength) {
605
+ const length = typeof minLength === "number" ? minLength : minLength.length;
606
+ if (value.length < length) {
607
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
608
+ this.error(message);
609
+ return false;
610
+ }
607
611
  }
608
612
  const maxLength = def.metadata.get("expect.maxLength");
609
- if (typeof maxLength === "number" && value.length > maxLength) {
610
- this.error(`Expected maximum length of ${maxLength} items, got ${value.length} items`);
611
- return false;
613
+ if (maxLength) {
614
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
615
+ if (value.length > length) {
616
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
617
+ this.error(message);
618
+ return false;
619
+ }
612
620
  }
613
621
  let i = 0;
614
622
  let passed = true;
@@ -740,14 +748,22 @@ else {
740
748
  }
741
749
  validateString(def, value) {
742
750
  const minLength = def.metadata.get("expect.minLength");
743
- if (typeof minLength === "number" && value.length < minLength) {
744
- this.error(`Expected minimum length of ${minLength} characters, got ${value.length} characters`);
745
- return false;
751
+ if (minLength) {
752
+ const length = typeof minLength === "number" ? minLength : minLength.length;
753
+ if (value.length < length) {
754
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
755
+ this.error(message);
756
+ return false;
757
+ }
746
758
  }
747
759
  const maxLength = def.metadata.get("expect.maxLength");
748
- if (typeof maxLength === "number" && value.length > maxLength) {
749
- this.error(`Expected maximum length of ${maxLength} characters, got ${value.length} characters`);
750
- return false;
760
+ if (maxLength) {
761
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
762
+ if (value.length > length) {
763
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
764
+ this.error(message);
765
+ return false;
766
+ }
751
767
  }
752
768
  const patterns = def.metadata.get("expect.pattern");
753
769
  for (const { pattern, flags, message } of patterns || []) {
@@ -767,19 +783,28 @@ else {
767
783
  }
768
784
  validateNumber(def, value) {
769
785
  const int = def.metadata.get("expect.int");
770
- if (typeof int === "boolean" && int && value % 1 !== 0) {
771
- this.error(`Expected integer, got ${value}`);
786
+ if (int && value % 1 !== 0) {
787
+ const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
788
+ this.error(message);
772
789
  return false;
773
790
  }
774
791
  const min = def.metadata.get("expect.min");
775
- if (typeof min === "number" && value < min) {
776
- this.error(`Expected minimum ${min}, got ${value}`);
777
- return false;
792
+ if (min) {
793
+ const minValue = typeof min === "number" ? min : min.minValue;
794
+ if (value < minValue) {
795
+ const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
796
+ this.error(message);
797
+ return false;
798
+ }
778
799
  }
779
800
  const max = def.metadata.get("expect.max");
780
- if (typeof max === "number" && value > max) {
781
- this.error(`Expected maximum ${max}, got ${value}`);
782
- return false;
801
+ if (max) {
802
+ const maxValue = typeof max === "number" ? max : max.maxValue;
803
+ if (value > maxValue) {
804
+ const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
805
+ this.error(message);
806
+ return false;
807
+ }
783
808
  }
784
809
  return true;
785
810
  }
@@ -961,9 +986,9 @@ function buildJsonSchema(type) {
961
986
  items: build$1(d.type.of)
962
987
  };
963
988
  const minLength = meta.get("expect.minLength");
964
- if (typeof minLength === "number") schema.minItems = minLength;
989
+ if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
965
990
  const maxLength = meta.get("expect.maxLength");
966
- if (typeof maxLength === "number") schema.maxItems = maxLength;
991
+ if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
967
992
  return schema;
968
993
  },
969
994
  union(d) {
@@ -988,18 +1013,18 @@ function buildJsonSchema(type) {
988
1013
  }
989
1014
  if (schema.type === "string") {
990
1015
  const minLength = meta.get("expect.minLength");
991
- if (typeof minLength === "number") schema.minLength = minLength;
1016
+ if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
992
1017
  const maxLength = meta.get("expect.maxLength");
993
- if (typeof maxLength === "number") schema.maxLength = maxLength;
1018
+ if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
994
1019
  const patterns = meta.get("expect.pattern");
995
1020
  if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
996
1021
  else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
997
1022
  }
998
1023
  if (schema.type === "number" || schema.type === "integer") {
999
1024
  const min = meta.get("expect.min");
1000
- if (typeof min === "number") schema.minimum = min;
1025
+ if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
1001
1026
  const max = meta.get("expect.max");
1002
- if (typeof max === "number") schema.maximum = max;
1027
+ if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
1003
1028
  }
1004
1029
  return schema;
1005
1030
  }
@@ -1214,9 +1239,22 @@ else handle.prop(prop.id, propHandle.$type);
1214
1239
  switch (a.name) {
1215
1240
  case "expect.minLength":
1216
1241
  case "expect.maxLength":
1242
+ if (a.args[0]) handle.annotate(a.name, {
1243
+ length: Number(a.args[0].text),
1244
+ message: a.args[1]?.text
1245
+ });
1246
+ break;
1217
1247
  case "expect.min":
1248
+ if (a.args[0]) handle.annotate(a.name, {
1249
+ minValue: Number(a.args[0].text),
1250
+ message: a.args[1]?.text
1251
+ });
1252
+ break;
1218
1253
  case "expect.max":
1219
- if (a.args[0]) handle.annotate(a.name, Number(a.args[0].text));
1254
+ if (a.args[0]) handle.annotate(a.name, {
1255
+ maxValue: Number(a.args[0].text),
1256
+ message: a.args[1]?.text
1257
+ });
1220
1258
  break;
1221
1259
  case "expect.pattern":
1222
1260
  handle.annotate(a.name, {
package/dist/index.cjs CHANGED
@@ -598,14 +598,22 @@ var Validator = class {
598
598
  return false;
599
599
  }
600
600
  const minLength = def.metadata.get("expect.minLength");
601
- if (typeof minLength === "number" && value.length < minLength) {
602
- this.error(`Expected minimum length of ${minLength} items, got ${value.length} items`);
603
- return false;
601
+ if (minLength) {
602
+ const length = typeof minLength === "number" ? minLength : minLength.length;
603
+ if (value.length < length) {
604
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
605
+ this.error(message);
606
+ return false;
607
+ }
604
608
  }
605
609
  const maxLength = def.metadata.get("expect.maxLength");
606
- if (typeof maxLength === "number" && value.length > maxLength) {
607
- this.error(`Expected maximum length of ${maxLength} items, got ${value.length} items`);
608
- return false;
610
+ if (maxLength) {
611
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
612
+ if (value.length > length) {
613
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
614
+ this.error(message);
615
+ return false;
616
+ }
609
617
  }
610
618
  let i = 0;
611
619
  let passed = true;
@@ -737,14 +745,22 @@ else {
737
745
  }
738
746
  validateString(def, value) {
739
747
  const minLength = def.metadata.get("expect.minLength");
740
- if (typeof minLength === "number" && value.length < minLength) {
741
- this.error(`Expected minimum length of ${minLength} characters, got ${value.length} characters`);
742
- return false;
748
+ if (minLength) {
749
+ const length = typeof minLength === "number" ? minLength : minLength.length;
750
+ if (value.length < length) {
751
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
752
+ this.error(message);
753
+ return false;
754
+ }
743
755
  }
744
756
  const maxLength = def.metadata.get("expect.maxLength");
745
- if (typeof maxLength === "number" && value.length > maxLength) {
746
- this.error(`Expected maximum length of ${maxLength} characters, got ${value.length} characters`);
747
- return false;
757
+ if (maxLength) {
758
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
759
+ if (value.length > length) {
760
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
761
+ this.error(message);
762
+ return false;
763
+ }
748
764
  }
749
765
  const patterns = def.metadata.get("expect.pattern");
750
766
  for (const { pattern, flags, message } of patterns || []) {
@@ -764,19 +780,28 @@ else {
764
780
  }
765
781
  validateNumber(def, value) {
766
782
  const int = def.metadata.get("expect.int");
767
- if (typeof int === "boolean" && int && value % 1 !== 0) {
768
- this.error(`Expected integer, got ${value}`);
783
+ if (int && value % 1 !== 0) {
784
+ const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
785
+ this.error(message);
769
786
  return false;
770
787
  }
771
788
  const min = def.metadata.get("expect.min");
772
- if (typeof min === "number" && value < min) {
773
- this.error(`Expected minimum ${min}, got ${value}`);
774
- return false;
789
+ if (min) {
790
+ const minValue = typeof min === "number" ? min : min.minValue;
791
+ if (value < minValue) {
792
+ const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
793
+ this.error(message);
794
+ return false;
795
+ }
775
796
  }
776
797
  const max = def.metadata.get("expect.max");
777
- if (typeof max === "number" && value > max) {
778
- this.error(`Expected maximum ${max}, got ${value}`);
779
- return false;
798
+ if (max) {
799
+ const maxValue = typeof max === "number" ? max : max.maxValue;
800
+ if (value > maxValue) {
801
+ const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
802
+ this.error(message);
803
+ return false;
804
+ }
780
805
  }
781
806
  return true;
782
807
  }
@@ -958,9 +983,9 @@ function buildJsonSchema(type) {
958
983
  items: build(d.type.of)
959
984
  };
960
985
  const minLength = meta.get("expect.minLength");
961
- if (typeof minLength === "number") schema.minItems = minLength;
986
+ if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
962
987
  const maxLength = meta.get("expect.maxLength");
963
- if (typeof maxLength === "number") schema.maxItems = maxLength;
988
+ if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
964
989
  return schema;
965
990
  },
966
991
  union(d) {
@@ -985,18 +1010,18 @@ function buildJsonSchema(type) {
985
1010
  }
986
1011
  if (schema.type === "string") {
987
1012
  const minLength = meta.get("expect.minLength");
988
- if (typeof minLength === "number") schema.minLength = minLength;
1013
+ if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
989
1014
  const maxLength = meta.get("expect.maxLength");
990
- if (typeof maxLength === "number") schema.maxLength = maxLength;
1015
+ if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
991
1016
  const patterns = meta.get("expect.pattern");
992
1017
  if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
993
1018
  else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
994
1019
  }
995
1020
  if (schema.type === "number" || schema.type === "integer") {
996
1021
  const min = meta.get("expect.min");
997
- if (typeof min === "number") schema.minimum = min;
1022
+ if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
998
1023
  const max = meta.get("expect.max");
999
- if (typeof max === "number") schema.maximum = max;
1024
+ if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
1000
1025
  }
1001
1026
  return schema;
1002
1027
  }
@@ -1211,9 +1236,22 @@ else handle.prop(prop.id, propHandle.$type);
1211
1236
  switch (a.name) {
1212
1237
  case "expect.minLength":
1213
1238
  case "expect.maxLength":
1239
+ if (a.args[0]) handle.annotate(a.name, {
1240
+ length: Number(a.args[0].text),
1241
+ message: a.args[1]?.text
1242
+ });
1243
+ break;
1214
1244
  case "expect.min":
1245
+ if (a.args[0]) handle.annotate(a.name, {
1246
+ minValue: Number(a.args[0].text),
1247
+ message: a.args[1]?.text
1248
+ });
1249
+ break;
1215
1250
  case "expect.max":
1216
- if (a.args[0]) handle.annotate(a.name, Number(a.args[0].text));
1251
+ if (a.args[0]) handle.annotate(a.name, {
1252
+ maxValue: Number(a.args[0].text),
1253
+ message: a.args[1]?.text
1254
+ });
1217
1255
  break;
1218
1256
  case "expect.pattern":
1219
1257
  handle.annotate(a.name, {
package/dist/index.mjs CHANGED
@@ -574,14 +574,22 @@ var Validator = class {
574
574
  return false;
575
575
  }
576
576
  const minLength = def.metadata.get("expect.minLength");
577
- if (typeof minLength === "number" && value.length < minLength) {
578
- this.error(`Expected minimum length of ${minLength} items, got ${value.length} items`);
579
- return false;
577
+ if (minLength) {
578
+ const length = typeof minLength === "number" ? minLength : minLength.length;
579
+ if (value.length < length) {
580
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
581
+ this.error(message);
582
+ return false;
583
+ }
580
584
  }
581
585
  const maxLength = def.metadata.get("expect.maxLength");
582
- if (typeof maxLength === "number" && value.length > maxLength) {
583
- this.error(`Expected maximum length of ${maxLength} items, got ${value.length} items`);
584
- return false;
586
+ if (maxLength) {
587
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
588
+ if (value.length > length) {
589
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
590
+ this.error(message);
591
+ return false;
592
+ }
585
593
  }
586
594
  let i = 0;
587
595
  let passed = true;
@@ -713,14 +721,22 @@ else {
713
721
  }
714
722
  validateString(def, value) {
715
723
  const minLength = def.metadata.get("expect.minLength");
716
- if (typeof minLength === "number" && value.length < minLength) {
717
- this.error(`Expected minimum length of ${minLength} characters, got ${value.length} characters`);
718
- return false;
724
+ if (minLength) {
725
+ const length = typeof minLength === "number" ? minLength : minLength.length;
726
+ if (value.length < length) {
727
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
728
+ this.error(message);
729
+ return false;
730
+ }
719
731
  }
720
732
  const maxLength = def.metadata.get("expect.maxLength");
721
- if (typeof maxLength === "number" && value.length > maxLength) {
722
- this.error(`Expected maximum length of ${maxLength} characters, got ${value.length} characters`);
723
- return false;
733
+ if (maxLength) {
734
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
735
+ if (value.length > length) {
736
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
737
+ this.error(message);
738
+ return false;
739
+ }
724
740
  }
725
741
  const patterns = def.metadata.get("expect.pattern");
726
742
  for (const { pattern, flags, message } of patterns || []) {
@@ -740,19 +756,28 @@ else {
740
756
  }
741
757
  validateNumber(def, value) {
742
758
  const int = def.metadata.get("expect.int");
743
- if (typeof int === "boolean" && int && value % 1 !== 0) {
744
- this.error(`Expected integer, got ${value}`);
759
+ if (int && value % 1 !== 0) {
760
+ const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
761
+ this.error(message);
745
762
  return false;
746
763
  }
747
764
  const min = def.metadata.get("expect.min");
748
- if (typeof min === "number" && value < min) {
749
- this.error(`Expected minimum ${min}, got ${value}`);
750
- return false;
765
+ if (min) {
766
+ const minValue = typeof min === "number" ? min : min.minValue;
767
+ if (value < minValue) {
768
+ const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
769
+ this.error(message);
770
+ return false;
771
+ }
751
772
  }
752
773
  const max = def.metadata.get("expect.max");
753
- if (typeof max === "number" && value > max) {
754
- this.error(`Expected maximum ${max}, got ${value}`);
755
- return false;
774
+ if (max) {
775
+ const maxValue = typeof max === "number" ? max : max.maxValue;
776
+ if (value > maxValue) {
777
+ const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
778
+ this.error(message);
779
+ return false;
780
+ }
756
781
  }
757
782
  return true;
758
783
  }
@@ -934,9 +959,9 @@ function buildJsonSchema(type) {
934
959
  items: build(d.type.of)
935
960
  };
936
961
  const minLength = meta.get("expect.minLength");
937
- if (typeof minLength === "number") schema.minItems = minLength;
962
+ if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
938
963
  const maxLength = meta.get("expect.maxLength");
939
- if (typeof maxLength === "number") schema.maxItems = maxLength;
964
+ if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
940
965
  return schema;
941
966
  },
942
967
  union(d) {
@@ -961,18 +986,18 @@ function buildJsonSchema(type) {
961
986
  }
962
987
  if (schema.type === "string") {
963
988
  const minLength = meta.get("expect.minLength");
964
- if (typeof minLength === "number") schema.minLength = minLength;
989
+ if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
965
990
  const maxLength = meta.get("expect.maxLength");
966
- if (typeof maxLength === "number") schema.maxLength = maxLength;
991
+ if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
967
992
  const patterns = meta.get("expect.pattern");
968
993
  if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
969
994
  else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
970
995
  }
971
996
  if (schema.type === "number" || schema.type === "integer") {
972
997
  const min = meta.get("expect.min");
973
- if (typeof min === "number") schema.minimum = min;
998
+ if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
974
999
  const max = meta.get("expect.max");
975
- if (typeof max === "number") schema.maximum = max;
1000
+ if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
976
1001
  }
977
1002
  return schema;
978
1003
  }
@@ -1187,9 +1212,22 @@ else handle.prop(prop.id, propHandle.$type);
1187
1212
  switch (a.name) {
1188
1213
  case "expect.minLength":
1189
1214
  case "expect.maxLength":
1215
+ if (a.args[0]) handle.annotate(a.name, {
1216
+ length: Number(a.args[0].text),
1217
+ message: a.args[1]?.text
1218
+ });
1219
+ break;
1190
1220
  case "expect.min":
1221
+ if (a.args[0]) handle.annotate(a.name, {
1222
+ minValue: Number(a.args[0].text),
1223
+ message: a.args[1]?.text
1224
+ });
1225
+ break;
1191
1226
  case "expect.max":
1192
- if (a.args[0]) handle.annotate(a.name, Number(a.args[0].text));
1227
+ if (a.args[0]) handle.annotate(a.name, {
1228
+ maxValue: Number(a.args[0].text),
1229
+ message: a.args[1]?.text
1230
+ });
1193
1231
  break;
1194
1232
  case "expect.pattern":
1195
1233
  handle.annotate(a.name, {
package/dist/utils.cjs CHANGED
@@ -154,14 +154,22 @@ var Validator = class {
154
154
  return false;
155
155
  }
156
156
  const minLength = def.metadata.get("expect.minLength");
157
- if (typeof minLength === "number" && value.length < minLength) {
158
- this.error(`Expected minimum length of ${minLength} items, got ${value.length} items`);
159
- return false;
157
+ if (minLength) {
158
+ const length = typeof minLength === "number" ? minLength : minLength.length;
159
+ if (value.length < length) {
160
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
161
+ this.error(message);
162
+ return false;
163
+ }
160
164
  }
161
165
  const maxLength = def.metadata.get("expect.maxLength");
162
- if (typeof maxLength === "number" && value.length > maxLength) {
163
- this.error(`Expected maximum length of ${maxLength} items, got ${value.length} items`);
164
- return false;
166
+ if (maxLength) {
167
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
168
+ if (value.length > length) {
169
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
170
+ this.error(message);
171
+ return false;
172
+ }
165
173
  }
166
174
  let i = 0;
167
175
  let passed = true;
@@ -293,14 +301,22 @@ else {
293
301
  }
294
302
  validateString(def, value) {
295
303
  const minLength = def.metadata.get("expect.minLength");
296
- if (typeof minLength === "number" && value.length < minLength) {
297
- this.error(`Expected minimum length of ${minLength} characters, got ${value.length} characters`);
298
- return false;
304
+ if (minLength) {
305
+ const length = typeof minLength === "number" ? minLength : minLength.length;
306
+ if (value.length < length) {
307
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
308
+ this.error(message);
309
+ return false;
310
+ }
299
311
  }
300
312
  const maxLength = def.metadata.get("expect.maxLength");
301
- if (typeof maxLength === "number" && value.length > maxLength) {
302
- this.error(`Expected maximum length of ${maxLength} characters, got ${value.length} characters`);
303
- return false;
313
+ if (maxLength) {
314
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
315
+ if (value.length > length) {
316
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
317
+ this.error(message);
318
+ return false;
319
+ }
304
320
  }
305
321
  const patterns = def.metadata.get("expect.pattern");
306
322
  for (const { pattern, flags, message } of patterns || []) {
@@ -320,19 +336,28 @@ else {
320
336
  }
321
337
  validateNumber(def, value) {
322
338
  const int = def.metadata.get("expect.int");
323
- if (typeof int === "boolean" && int && value % 1 !== 0) {
324
- this.error(`Expected integer, got ${value}`);
339
+ if (int && value % 1 !== 0) {
340
+ const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
341
+ this.error(message);
325
342
  return false;
326
343
  }
327
344
  const min = def.metadata.get("expect.min");
328
- if (typeof min === "number" && value < min) {
329
- this.error(`Expected minimum ${min}, got ${value}`);
330
- return false;
345
+ if (min) {
346
+ const minValue = typeof min === "number" ? min : min.minValue;
347
+ if (value < minValue) {
348
+ const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
349
+ this.error(message);
350
+ return false;
351
+ }
331
352
  }
332
353
  const max = def.metadata.get("expect.max");
333
- if (typeof max === "number" && value > max) {
334
- this.error(`Expected maximum ${max}, got ${value}`);
335
- return false;
354
+ if (max) {
355
+ const maxValue = typeof max === "number" ? max : max.maxValue;
356
+ if (value > maxValue) {
357
+ const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
358
+ this.error(message);
359
+ return false;
360
+ }
336
361
  }
337
362
  return true;
338
363
  }
@@ -527,9 +552,9 @@ function buildJsonSchema(type) {
527
552
  items: build(d.type.of)
528
553
  };
529
554
  const minLength = meta.get("expect.minLength");
530
- if (typeof minLength === "number") schema.minItems = minLength;
555
+ if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
531
556
  const maxLength = meta.get("expect.maxLength");
532
- if (typeof maxLength === "number") schema.maxItems = maxLength;
557
+ if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
533
558
  return schema;
534
559
  },
535
560
  union(d) {
@@ -554,18 +579,18 @@ function buildJsonSchema(type) {
554
579
  }
555
580
  if (schema.type === "string") {
556
581
  const minLength = meta.get("expect.minLength");
557
- if (typeof minLength === "number") schema.minLength = minLength;
582
+ if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
558
583
  const maxLength = meta.get("expect.maxLength");
559
- if (typeof maxLength === "number") schema.maxLength = maxLength;
584
+ if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
560
585
  const patterns = meta.get("expect.pattern");
561
586
  if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
562
587
  else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
563
588
  }
564
589
  if (schema.type === "number" || schema.type === "integer") {
565
590
  const min = meta.get("expect.min");
566
- if (typeof min === "number") schema.minimum = min;
591
+ if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
567
592
  const max = meta.get("expect.max");
568
- if (typeof max === "number") schema.maximum = max;
593
+ if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
569
594
  }
570
595
  return schema;
571
596
  }
@@ -631,14 +656,14 @@ function fromJsonSchema(schema) {
631
656
  }
632
657
  const itemType = s.items ? convert(s.items) : defineAnnotatedType().designType("any").$type;
633
658
  const handle = defineAnnotatedType("array").of(itemType);
634
- if (typeof s.minItems === "number") handle.annotate("expect.minLength", s.minItems);
635
- if (typeof s.maxItems === "number") handle.annotate("expect.maxLength", s.maxItems);
659
+ if (typeof s.minItems === "number") handle.annotate("expect.minLength", { length: s.minItems });
660
+ if (typeof s.maxItems === "number") handle.annotate("expect.maxLength", { length: s.maxItems });
636
661
  return handle.$type;
637
662
  }
638
663
  if (s.type === "string") {
639
664
  const handle = defineAnnotatedType().designType("string").tags("string");
640
- if (typeof s.minLength === "number") handle.annotate("expect.minLength", s.minLength);
641
- if (typeof s.maxLength === "number") handle.annotate("expect.maxLength", s.maxLength);
665
+ if (typeof s.minLength === "number") handle.annotate("expect.minLength", { length: s.minLength });
666
+ if (typeof s.maxLength === "number") handle.annotate("expect.maxLength", { length: s.maxLength });
642
667
  if (s.pattern) handle.annotate("expect.pattern", { pattern: s.pattern }, true);
643
668
  if (s.allOf) {
644
669
  for (const item of s.allOf) if (item.pattern) handle.annotate("expect.pattern", { pattern: item.pattern }, true);
@@ -648,14 +673,14 @@ function fromJsonSchema(schema) {
648
673
  if (s.type === "integer") {
649
674
  const handle = defineAnnotatedType().designType("number").tags("number");
650
675
  handle.annotate("expect.int", true);
651
- if (typeof s.minimum === "number") handle.annotate("expect.min", s.minimum);
652
- if (typeof s.maximum === "number") handle.annotate("expect.max", s.maximum);
676
+ if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
677
+ if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
653
678
  return handle.$type;
654
679
  }
655
680
  if (s.type === "number") {
656
681
  const handle = defineAnnotatedType().designType("number").tags("number");
657
- if (typeof s.minimum === "number") handle.annotate("expect.min", s.minimum);
658
- if (typeof s.maximum === "number") handle.annotate("expect.max", s.maximum);
682
+ if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
683
+ if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
659
684
  return handle.$type;
660
685
  }
661
686
  if (s.type === "boolean") return defineAnnotatedType().designType("boolean").tags("boolean").$type;
@@ -701,10 +726,7 @@ function serializeTypeDef(def, path, options) {
701
726
  },
702
727
  object(d) {
703
728
  const props = {};
704
- for (const [key, val] of d.type.props.entries()) {
705
- if (isPhantomType(val)) continue;
706
- props[key] = serializeNode(val, [...path, key], options);
707
- }
729
+ for (const [key, val] of d.type.props.entries()) props[key] = serializeNode(val, [...path, key], options);
708
730
  const propsPatterns = d.type.propsPatterns.map((pp) => ({
709
731
  pattern: {
710
732
  source: pp.pattern.source,
package/dist/utils.mjs CHANGED
@@ -153,14 +153,22 @@ var Validator = class {
153
153
  return false;
154
154
  }
155
155
  const minLength = def.metadata.get("expect.minLength");
156
- if (typeof minLength === "number" && value.length < minLength) {
157
- this.error(`Expected minimum length of ${minLength} items, got ${value.length} items`);
158
- return false;
156
+ if (minLength) {
157
+ const length = typeof minLength === "number" ? minLength : minLength.length;
158
+ if (value.length < length) {
159
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
160
+ this.error(message);
161
+ return false;
162
+ }
159
163
  }
160
164
  const maxLength = def.metadata.get("expect.maxLength");
161
- if (typeof maxLength === "number" && value.length > maxLength) {
162
- this.error(`Expected maximum length of ${maxLength} items, got ${value.length} items`);
163
- return false;
165
+ if (maxLength) {
166
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
167
+ if (value.length > length) {
168
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
169
+ this.error(message);
170
+ return false;
171
+ }
164
172
  }
165
173
  let i = 0;
166
174
  let passed = true;
@@ -292,14 +300,22 @@ else {
292
300
  }
293
301
  validateString(def, value) {
294
302
  const minLength = def.metadata.get("expect.minLength");
295
- if (typeof minLength === "number" && value.length < minLength) {
296
- this.error(`Expected minimum length of ${minLength} characters, got ${value.length} characters`);
297
- return false;
303
+ if (minLength) {
304
+ const length = typeof minLength === "number" ? minLength : minLength.length;
305
+ if (value.length < length) {
306
+ const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
307
+ this.error(message);
308
+ return false;
309
+ }
298
310
  }
299
311
  const maxLength = def.metadata.get("expect.maxLength");
300
- if (typeof maxLength === "number" && value.length > maxLength) {
301
- this.error(`Expected maximum length of ${maxLength} characters, got ${value.length} characters`);
302
- return false;
312
+ if (maxLength) {
313
+ const length = typeof maxLength === "number" ? maxLength : maxLength.length;
314
+ if (value.length > length) {
315
+ const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
316
+ this.error(message);
317
+ return false;
318
+ }
303
319
  }
304
320
  const patterns = def.metadata.get("expect.pattern");
305
321
  for (const { pattern, flags, message } of patterns || []) {
@@ -319,19 +335,28 @@ else {
319
335
  }
320
336
  validateNumber(def, value) {
321
337
  const int = def.metadata.get("expect.int");
322
- if (typeof int === "boolean" && int && value % 1 !== 0) {
323
- this.error(`Expected integer, got ${value}`);
338
+ if (int && value % 1 !== 0) {
339
+ const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
340
+ this.error(message);
324
341
  return false;
325
342
  }
326
343
  const min = def.metadata.get("expect.min");
327
- if (typeof min === "number" && value < min) {
328
- this.error(`Expected minimum ${min}, got ${value}`);
329
- return false;
344
+ if (min) {
345
+ const minValue = typeof min === "number" ? min : min.minValue;
346
+ if (value < minValue) {
347
+ const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
348
+ this.error(message);
349
+ return false;
350
+ }
330
351
  }
331
352
  const max = def.metadata.get("expect.max");
332
- if (typeof max === "number" && value > max) {
333
- this.error(`Expected maximum ${max}, got ${value}`);
334
- return false;
353
+ if (max) {
354
+ const maxValue = typeof max === "number" ? max : max.maxValue;
355
+ if (value > maxValue) {
356
+ const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
357
+ this.error(message);
358
+ return false;
359
+ }
335
360
  }
336
361
  return true;
337
362
  }
@@ -526,9 +551,9 @@ function buildJsonSchema(type) {
526
551
  items: build(d.type.of)
527
552
  };
528
553
  const minLength = meta.get("expect.minLength");
529
- if (typeof minLength === "number") schema.minItems = minLength;
554
+ if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
530
555
  const maxLength = meta.get("expect.maxLength");
531
- if (typeof maxLength === "number") schema.maxItems = maxLength;
556
+ if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
532
557
  return schema;
533
558
  },
534
559
  union(d) {
@@ -553,18 +578,18 @@ function buildJsonSchema(type) {
553
578
  }
554
579
  if (schema.type === "string") {
555
580
  const minLength = meta.get("expect.minLength");
556
- if (typeof minLength === "number") schema.minLength = minLength;
581
+ if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
557
582
  const maxLength = meta.get("expect.maxLength");
558
- if (typeof maxLength === "number") schema.maxLength = maxLength;
583
+ if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
559
584
  const patterns = meta.get("expect.pattern");
560
585
  if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
561
586
  else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
562
587
  }
563
588
  if (schema.type === "number" || schema.type === "integer") {
564
589
  const min = meta.get("expect.min");
565
- if (typeof min === "number") schema.minimum = min;
590
+ if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
566
591
  const max = meta.get("expect.max");
567
- if (typeof max === "number") schema.maximum = max;
592
+ if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
568
593
  }
569
594
  return schema;
570
595
  }
@@ -630,14 +655,14 @@ function fromJsonSchema(schema) {
630
655
  }
631
656
  const itemType = s.items ? convert(s.items) : defineAnnotatedType().designType("any").$type;
632
657
  const handle = defineAnnotatedType("array").of(itemType);
633
- if (typeof s.minItems === "number") handle.annotate("expect.minLength", s.minItems);
634
- if (typeof s.maxItems === "number") handle.annotate("expect.maxLength", s.maxItems);
658
+ if (typeof s.minItems === "number") handle.annotate("expect.minLength", { length: s.minItems });
659
+ if (typeof s.maxItems === "number") handle.annotate("expect.maxLength", { length: s.maxItems });
635
660
  return handle.$type;
636
661
  }
637
662
  if (s.type === "string") {
638
663
  const handle = defineAnnotatedType().designType("string").tags("string");
639
- if (typeof s.minLength === "number") handle.annotate("expect.minLength", s.minLength);
640
- if (typeof s.maxLength === "number") handle.annotate("expect.maxLength", s.maxLength);
664
+ if (typeof s.minLength === "number") handle.annotate("expect.minLength", { length: s.minLength });
665
+ if (typeof s.maxLength === "number") handle.annotate("expect.maxLength", { length: s.maxLength });
641
666
  if (s.pattern) handle.annotate("expect.pattern", { pattern: s.pattern }, true);
642
667
  if (s.allOf) {
643
668
  for (const item of s.allOf) if (item.pattern) handle.annotate("expect.pattern", { pattern: item.pattern }, true);
@@ -647,14 +672,14 @@ function fromJsonSchema(schema) {
647
672
  if (s.type === "integer") {
648
673
  const handle = defineAnnotatedType().designType("number").tags("number");
649
674
  handle.annotate("expect.int", true);
650
- if (typeof s.minimum === "number") handle.annotate("expect.min", s.minimum);
651
- if (typeof s.maximum === "number") handle.annotate("expect.max", s.maximum);
675
+ if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
676
+ if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
652
677
  return handle.$type;
653
678
  }
654
679
  if (s.type === "number") {
655
680
  const handle = defineAnnotatedType().designType("number").tags("number");
656
- if (typeof s.minimum === "number") handle.annotate("expect.min", s.minimum);
657
- if (typeof s.maximum === "number") handle.annotate("expect.max", s.maximum);
681
+ if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
682
+ if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
658
683
  return handle.$type;
659
684
  }
660
685
  if (s.type === "boolean") return defineAnnotatedType().designType("boolean").tags("boolean").$type;
@@ -700,10 +725,7 @@ function serializeTypeDef(def, path, options) {
700
725
  },
701
726
  object(d) {
702
727
  const props = {};
703
- for (const [key, val] of d.type.props.entries()) {
704
- if (isPhantomType(val)) continue;
705
- props[key] = serializeNode(val, [...path, key], options);
706
- }
728
+ for (const [key, val] of d.type.props.entries()) props[key] = serializeNode(val, [...path, key], options);
707
729
  const propsPatterns = d.type.propsPatterns.map((pp) => ({
708
730
  pattern: {
709
731
  source: pp.pattern.source,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/typescript",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Atscript: typescript-gen support.",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -70,7 +70,7 @@
70
70
  "homepage": "https://github.com/moostjs/atscript/tree/main/packages/typescript#readme",
71
71
  "license": "ISC",
72
72
  "peerDependencies": {
73
- "@atscript/core": "^0.1.6"
73
+ "@atscript/core": "^0.1.8"
74
74
  },
75
75
  "dependencies": {
76
76
  "@moostjs/event-cli": "^0.5.32",