@atscript/typescript 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +76 -49
- package/dist/index.cjs +76 -49
- package/dist/index.mjs +76 -49
- package/dist/utils.cjs +59 -34
- package/dist/utils.mjs +59 -34
- package/package.json +2 -2
package/dist/cli.cjs
CHANGED
|
@@ -298,8 +298,9 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
298
298
|
patterns.push(prop);
|
|
299
299
|
continue;
|
|
300
300
|
}
|
|
301
|
-
|
|
302
|
-
|
|
301
|
+
const phantomType = this.phantomPropType(prop.getDefinition());
|
|
302
|
+
if (phantomType) {
|
|
303
|
+
this.writeln(`// ${prop.id}: ${phantomType}`);
|
|
303
304
|
continue;
|
|
304
305
|
}
|
|
305
306
|
const optional = !!prop.token("optional");
|
|
@@ -345,12 +346,6 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
345
346
|
if (struct?.entity === "structure") this.renderStructure(struct, node.id);
|
|
346
347
|
else this.writeln("{}");
|
|
347
348
|
this.writeln();
|
|
348
|
-
const nsPrefix = exported ? "export declare" : "declare";
|
|
349
|
-
this.write(`${nsPrefix} namespace ${node.id} `);
|
|
350
|
-
this.blockln("{}");
|
|
351
|
-
this.writeln(`type DataType = ${node.id}`);
|
|
352
|
-
this.popln();
|
|
353
|
-
this.writeln();
|
|
354
349
|
}
|
|
355
350
|
renderType(node) {
|
|
356
351
|
this.writeln();
|
|
@@ -377,12 +372,6 @@ else this.writeln("{}");
|
|
|
377
372
|
} else {
|
|
378
373
|
this.write(exported ? "export declare " : "declare ");
|
|
379
374
|
this.writeln(`class ${node.id} extends ${targetName} {}`);
|
|
380
|
-
const nsPrefix = exported ? "export declare" : "declare";
|
|
381
|
-
this.write(`${nsPrefix} namespace ${node.id} `);
|
|
382
|
-
this.blockln("{}");
|
|
383
|
-
this.writeln(`type DataType = ${node.id}`);
|
|
384
|
-
this.popln();
|
|
385
|
-
this.writeln();
|
|
386
375
|
}
|
|
387
376
|
}
|
|
388
377
|
renderTypeNamespace(node) {
|
|
@@ -401,23 +390,23 @@ else if ((0, __atscript_core.isGroup)(realDef)) typeDef = `TAtscriptTypeComplex<
|
|
|
401
390
|
else if ((0, __atscript_core.isArray)(realDef)) typeDef = `TAtscriptTypeArray<${name}>`;
|
|
402
391
|
else if ((0, __atscript_core.isPrimitive)(realDef)) typeDef = `TAtscriptTypeFinal<${name}>`;
|
|
403
392
|
}
|
|
404
|
-
this.writeln(`type DataType = ${name}`);
|
|
405
393
|
this.writeln(`const __is_atscript_annotated_type: true`);
|
|
406
394
|
this.writeln(`const type: ${typeDef}`);
|
|
407
395
|
this.writeln(`const metadata: TMetadataMap<AtscriptMetadata>`);
|
|
408
|
-
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}
|
|
396
|
+
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}>`);
|
|
409
397
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
410
398
|
this.writeln("const toJsonSchema: () => any");
|
|
411
399
|
this.popln();
|
|
412
400
|
}
|
|
413
|
-
|
|
414
|
-
if (!def) return
|
|
415
|
-
if ((0, __atscript_core.isPrimitive)(def) && def.
|
|
401
|
+
phantomPropType(def) {
|
|
402
|
+
if (!def) return undefined;
|
|
403
|
+
if ((0, __atscript_core.isPrimitive)(def) && def.config.type === "phantom") return def.id;
|
|
416
404
|
if ((0, __atscript_core.isRef)(def)) {
|
|
417
|
-
const
|
|
418
|
-
|
|
405
|
+
const ref = def;
|
|
406
|
+
const unwound = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
407
|
+
if ((0, __atscript_core.isPrimitive)(unwound) && unwound.config.type === "phantom") return ref.hasChain ? `${ref.id}.${ref.chain.map((c) => c.text).join(".")}` : ref.id;
|
|
419
408
|
}
|
|
420
|
-
return
|
|
409
|
+
return undefined;
|
|
421
410
|
}
|
|
422
411
|
isTypeTarget(name, doc) {
|
|
423
412
|
const d = doc || this.doc;
|
|
@@ -612,14 +601,22 @@ var Validator = class {
|
|
|
612
601
|
return false;
|
|
613
602
|
}
|
|
614
603
|
const minLength = def.metadata.get("expect.minLength");
|
|
615
|
-
if (
|
|
616
|
-
|
|
617
|
-
|
|
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
|
+
}
|
|
618
611
|
}
|
|
619
612
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
620
|
-
if (
|
|
621
|
-
|
|
622
|
-
|
|
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
|
+
}
|
|
623
620
|
}
|
|
624
621
|
let i = 0;
|
|
625
622
|
let passed = true;
|
|
@@ -751,14 +748,22 @@ else {
|
|
|
751
748
|
}
|
|
752
749
|
validateString(def, value) {
|
|
753
750
|
const minLength = def.metadata.get("expect.minLength");
|
|
754
|
-
if (
|
|
755
|
-
|
|
756
|
-
|
|
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
|
+
}
|
|
757
758
|
}
|
|
758
759
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
759
|
-
if (
|
|
760
|
-
|
|
761
|
-
|
|
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
|
+
}
|
|
762
767
|
}
|
|
763
768
|
const patterns = def.metadata.get("expect.pattern");
|
|
764
769
|
for (const { pattern, flags, message } of patterns || []) {
|
|
@@ -778,19 +783,28 @@ else {
|
|
|
778
783
|
}
|
|
779
784
|
validateNumber(def, value) {
|
|
780
785
|
const int = def.metadata.get("expect.int");
|
|
781
|
-
if (
|
|
782
|
-
|
|
786
|
+
if (int && value % 1 !== 0) {
|
|
787
|
+
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
788
|
+
this.error(message);
|
|
783
789
|
return false;
|
|
784
790
|
}
|
|
785
791
|
const min = def.metadata.get("expect.min");
|
|
786
|
-
if (
|
|
787
|
-
|
|
788
|
-
|
|
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
|
+
}
|
|
789
799
|
}
|
|
790
800
|
const max = def.metadata.get("expect.max");
|
|
791
|
-
if (
|
|
792
|
-
|
|
793
|
-
|
|
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
|
+
}
|
|
794
808
|
}
|
|
795
809
|
return true;
|
|
796
810
|
}
|
|
@@ -972,9 +986,9 @@ function buildJsonSchema(type) {
|
|
|
972
986
|
items: build$1(d.type.of)
|
|
973
987
|
};
|
|
974
988
|
const minLength = meta.get("expect.minLength");
|
|
975
|
-
if (typeof minLength === "number"
|
|
989
|
+
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
976
990
|
const maxLength = meta.get("expect.maxLength");
|
|
977
|
-
if (typeof maxLength === "number"
|
|
991
|
+
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
978
992
|
return schema;
|
|
979
993
|
},
|
|
980
994
|
union(d) {
|
|
@@ -999,18 +1013,18 @@ function buildJsonSchema(type) {
|
|
|
999
1013
|
}
|
|
1000
1014
|
if (schema.type === "string") {
|
|
1001
1015
|
const minLength = meta.get("expect.minLength");
|
|
1002
|
-
if (typeof minLength === "number"
|
|
1016
|
+
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1003
1017
|
const maxLength = meta.get("expect.maxLength");
|
|
1004
|
-
if (typeof maxLength === "number"
|
|
1018
|
+
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1005
1019
|
const patterns = meta.get("expect.pattern");
|
|
1006
1020
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1007
1021
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1008
1022
|
}
|
|
1009
1023
|
if (schema.type === "number" || schema.type === "integer") {
|
|
1010
1024
|
const min = meta.get("expect.min");
|
|
1011
|
-
if (typeof min === "number"
|
|
1025
|
+
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1012
1026
|
const max = meta.get("expect.max");
|
|
1013
|
-
if (typeof max === "number"
|
|
1027
|
+
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1014
1028
|
}
|
|
1015
1029
|
return schema;
|
|
1016
1030
|
}
|
|
@@ -1225,9 +1239,22 @@ else handle.prop(prop.id, propHandle.$type);
|
|
|
1225
1239
|
switch (a.name) {
|
|
1226
1240
|
case "expect.minLength":
|
|
1227
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;
|
|
1228
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;
|
|
1229
1253
|
case "expect.max":
|
|
1230
|
-
if (a.args[0]) handle.annotate(a.name,
|
|
1254
|
+
if (a.args[0]) handle.annotate(a.name, {
|
|
1255
|
+
maxValue: Number(a.args[0].text),
|
|
1256
|
+
message: a.args[1]?.text
|
|
1257
|
+
});
|
|
1231
1258
|
break;
|
|
1232
1259
|
case "expect.pattern":
|
|
1233
1260
|
handle.annotate(a.name, {
|
package/dist/index.cjs
CHANGED
|
@@ -295,8 +295,9 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
295
295
|
patterns.push(prop);
|
|
296
296
|
continue;
|
|
297
297
|
}
|
|
298
|
-
|
|
299
|
-
|
|
298
|
+
const phantomType = this.phantomPropType(prop.getDefinition());
|
|
299
|
+
if (phantomType) {
|
|
300
|
+
this.writeln(`// ${prop.id}: ${phantomType}`);
|
|
300
301
|
continue;
|
|
301
302
|
}
|
|
302
303
|
const optional = !!prop.token("optional");
|
|
@@ -342,12 +343,6 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
342
343
|
if (struct?.entity === "structure") this.renderStructure(struct, node.id);
|
|
343
344
|
else this.writeln("{}");
|
|
344
345
|
this.writeln();
|
|
345
|
-
const nsPrefix = exported ? "export declare" : "declare";
|
|
346
|
-
this.write(`${nsPrefix} namespace ${node.id} `);
|
|
347
|
-
this.blockln("{}");
|
|
348
|
-
this.writeln(`type DataType = ${node.id}`);
|
|
349
|
-
this.popln();
|
|
350
|
-
this.writeln();
|
|
351
346
|
}
|
|
352
347
|
renderType(node) {
|
|
353
348
|
this.writeln();
|
|
@@ -374,12 +369,6 @@ else this.writeln("{}");
|
|
|
374
369
|
} else {
|
|
375
370
|
this.write(exported ? "export declare " : "declare ");
|
|
376
371
|
this.writeln(`class ${node.id} extends ${targetName} {}`);
|
|
377
|
-
const nsPrefix = exported ? "export declare" : "declare";
|
|
378
|
-
this.write(`${nsPrefix} namespace ${node.id} `);
|
|
379
|
-
this.blockln("{}");
|
|
380
|
-
this.writeln(`type DataType = ${node.id}`);
|
|
381
|
-
this.popln();
|
|
382
|
-
this.writeln();
|
|
383
372
|
}
|
|
384
373
|
}
|
|
385
374
|
renderTypeNamespace(node) {
|
|
@@ -398,23 +387,23 @@ else if ((0, __atscript_core.isGroup)(realDef)) typeDef = `TAtscriptTypeComplex<
|
|
|
398
387
|
else if ((0, __atscript_core.isArray)(realDef)) typeDef = `TAtscriptTypeArray<${name}>`;
|
|
399
388
|
else if ((0, __atscript_core.isPrimitive)(realDef)) typeDef = `TAtscriptTypeFinal<${name}>`;
|
|
400
389
|
}
|
|
401
|
-
this.writeln(`type DataType = ${name}`);
|
|
402
390
|
this.writeln(`const __is_atscript_annotated_type: true`);
|
|
403
391
|
this.writeln(`const type: ${typeDef}`);
|
|
404
392
|
this.writeln(`const metadata: TMetadataMap<AtscriptMetadata>`);
|
|
405
|
-
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}
|
|
393
|
+
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}>`);
|
|
406
394
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
407
395
|
this.writeln("const toJsonSchema: () => any");
|
|
408
396
|
this.popln();
|
|
409
397
|
}
|
|
410
|
-
|
|
411
|
-
if (!def) return
|
|
412
|
-
if ((0, __atscript_core.isPrimitive)(def) && def.
|
|
398
|
+
phantomPropType(def) {
|
|
399
|
+
if (!def) return undefined;
|
|
400
|
+
if ((0, __atscript_core.isPrimitive)(def) && def.config.type === "phantom") return def.id;
|
|
413
401
|
if ((0, __atscript_core.isRef)(def)) {
|
|
414
|
-
const
|
|
415
|
-
|
|
402
|
+
const ref = def;
|
|
403
|
+
const unwound = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
404
|
+
if ((0, __atscript_core.isPrimitive)(unwound) && unwound.config.type === "phantom") return ref.hasChain ? `${ref.id}.${ref.chain.map((c) => c.text).join(".")}` : ref.id;
|
|
416
405
|
}
|
|
417
|
-
return
|
|
406
|
+
return undefined;
|
|
418
407
|
}
|
|
419
408
|
isTypeTarget(name, doc) {
|
|
420
409
|
const d = doc || this.doc;
|
|
@@ -609,14 +598,22 @@ var Validator = class {
|
|
|
609
598
|
return false;
|
|
610
599
|
}
|
|
611
600
|
const minLength = def.metadata.get("expect.minLength");
|
|
612
|
-
if (
|
|
613
|
-
|
|
614
|
-
|
|
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
|
+
}
|
|
615
608
|
}
|
|
616
609
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
617
|
-
if (
|
|
618
|
-
|
|
619
|
-
|
|
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
|
+
}
|
|
620
617
|
}
|
|
621
618
|
let i = 0;
|
|
622
619
|
let passed = true;
|
|
@@ -748,14 +745,22 @@ else {
|
|
|
748
745
|
}
|
|
749
746
|
validateString(def, value) {
|
|
750
747
|
const minLength = def.metadata.get("expect.minLength");
|
|
751
|
-
if (
|
|
752
|
-
|
|
753
|
-
|
|
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
|
+
}
|
|
754
755
|
}
|
|
755
756
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
756
|
-
if (
|
|
757
|
-
|
|
758
|
-
|
|
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
|
+
}
|
|
759
764
|
}
|
|
760
765
|
const patterns = def.metadata.get("expect.pattern");
|
|
761
766
|
for (const { pattern, flags, message } of patterns || []) {
|
|
@@ -775,19 +780,28 @@ else {
|
|
|
775
780
|
}
|
|
776
781
|
validateNumber(def, value) {
|
|
777
782
|
const int = def.metadata.get("expect.int");
|
|
778
|
-
if (
|
|
779
|
-
|
|
783
|
+
if (int && value % 1 !== 0) {
|
|
784
|
+
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
785
|
+
this.error(message);
|
|
780
786
|
return false;
|
|
781
787
|
}
|
|
782
788
|
const min = def.metadata.get("expect.min");
|
|
783
|
-
if (
|
|
784
|
-
|
|
785
|
-
|
|
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
|
+
}
|
|
786
796
|
}
|
|
787
797
|
const max = def.metadata.get("expect.max");
|
|
788
|
-
if (
|
|
789
|
-
|
|
790
|
-
|
|
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
|
+
}
|
|
791
805
|
}
|
|
792
806
|
return true;
|
|
793
807
|
}
|
|
@@ -969,9 +983,9 @@ function buildJsonSchema(type) {
|
|
|
969
983
|
items: build(d.type.of)
|
|
970
984
|
};
|
|
971
985
|
const minLength = meta.get("expect.minLength");
|
|
972
|
-
if (typeof minLength === "number"
|
|
986
|
+
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
973
987
|
const maxLength = meta.get("expect.maxLength");
|
|
974
|
-
if (typeof maxLength === "number"
|
|
988
|
+
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
975
989
|
return schema;
|
|
976
990
|
},
|
|
977
991
|
union(d) {
|
|
@@ -996,18 +1010,18 @@ function buildJsonSchema(type) {
|
|
|
996
1010
|
}
|
|
997
1011
|
if (schema.type === "string") {
|
|
998
1012
|
const minLength = meta.get("expect.minLength");
|
|
999
|
-
if (typeof minLength === "number"
|
|
1013
|
+
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1000
1014
|
const maxLength = meta.get("expect.maxLength");
|
|
1001
|
-
if (typeof maxLength === "number"
|
|
1015
|
+
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1002
1016
|
const patterns = meta.get("expect.pattern");
|
|
1003
1017
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1004
1018
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1005
1019
|
}
|
|
1006
1020
|
if (schema.type === "number" || schema.type === "integer") {
|
|
1007
1021
|
const min = meta.get("expect.min");
|
|
1008
|
-
if (typeof min === "number"
|
|
1022
|
+
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1009
1023
|
const max = meta.get("expect.max");
|
|
1010
|
-
if (typeof max === "number"
|
|
1024
|
+
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1011
1025
|
}
|
|
1012
1026
|
return schema;
|
|
1013
1027
|
}
|
|
@@ -1222,9 +1236,22 @@ else handle.prop(prop.id, propHandle.$type);
|
|
|
1222
1236
|
switch (a.name) {
|
|
1223
1237
|
case "expect.minLength":
|
|
1224
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;
|
|
1225
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;
|
|
1226
1250
|
case "expect.max":
|
|
1227
|
-
if (a.args[0]) handle.annotate(a.name,
|
|
1251
|
+
if (a.args[0]) handle.annotate(a.name, {
|
|
1252
|
+
maxValue: Number(a.args[0].text),
|
|
1253
|
+
message: a.args[1]?.text
|
|
1254
|
+
});
|
|
1228
1255
|
break;
|
|
1229
1256
|
case "expect.pattern":
|
|
1230
1257
|
handle.annotate(a.name, {
|
package/dist/index.mjs
CHANGED
|
@@ -271,8 +271,9 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
271
271
|
patterns.push(prop);
|
|
272
272
|
continue;
|
|
273
273
|
}
|
|
274
|
-
|
|
275
|
-
|
|
274
|
+
const phantomType = this.phantomPropType(prop.getDefinition());
|
|
275
|
+
if (phantomType) {
|
|
276
|
+
this.writeln(`// ${prop.id}: ${phantomType}`);
|
|
276
277
|
continue;
|
|
277
278
|
}
|
|
278
279
|
const optional = !!prop.token("optional");
|
|
@@ -318,12 +319,6 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
318
319
|
if (struct?.entity === "structure") this.renderStructure(struct, node.id);
|
|
319
320
|
else this.writeln("{}");
|
|
320
321
|
this.writeln();
|
|
321
|
-
const nsPrefix = exported ? "export declare" : "declare";
|
|
322
|
-
this.write(`${nsPrefix} namespace ${node.id} `);
|
|
323
|
-
this.blockln("{}");
|
|
324
|
-
this.writeln(`type DataType = ${node.id}`);
|
|
325
|
-
this.popln();
|
|
326
|
-
this.writeln();
|
|
327
322
|
}
|
|
328
323
|
renderType(node) {
|
|
329
324
|
this.writeln();
|
|
@@ -350,12 +345,6 @@ else this.writeln("{}");
|
|
|
350
345
|
} else {
|
|
351
346
|
this.write(exported ? "export declare " : "declare ");
|
|
352
347
|
this.writeln(`class ${node.id} extends ${targetName} {}`);
|
|
353
|
-
const nsPrefix = exported ? "export declare" : "declare";
|
|
354
|
-
this.write(`${nsPrefix} namespace ${node.id} `);
|
|
355
|
-
this.blockln("{}");
|
|
356
|
-
this.writeln(`type DataType = ${node.id}`);
|
|
357
|
-
this.popln();
|
|
358
|
-
this.writeln();
|
|
359
348
|
}
|
|
360
349
|
}
|
|
361
350
|
renderTypeNamespace(node) {
|
|
@@ -374,23 +363,23 @@ else if (isGroup(realDef)) typeDef = `TAtscriptTypeComplex<${name}>`;
|
|
|
374
363
|
else if (isArray(realDef)) typeDef = `TAtscriptTypeArray<${name}>`;
|
|
375
364
|
else if (isPrimitive(realDef)) typeDef = `TAtscriptTypeFinal<${name}>`;
|
|
376
365
|
}
|
|
377
|
-
this.writeln(`type DataType = ${name}`);
|
|
378
366
|
this.writeln(`const __is_atscript_annotated_type: true`);
|
|
379
367
|
this.writeln(`const type: ${typeDef}`);
|
|
380
368
|
this.writeln(`const metadata: TMetadataMap<AtscriptMetadata>`);
|
|
381
|
-
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}
|
|
369
|
+
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}>`);
|
|
382
370
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
383
371
|
this.writeln("const toJsonSchema: () => any");
|
|
384
372
|
this.popln();
|
|
385
373
|
}
|
|
386
|
-
|
|
387
|
-
if (!def) return
|
|
388
|
-
if (isPrimitive(def) && def.
|
|
374
|
+
phantomPropType(def) {
|
|
375
|
+
if (!def) return undefined;
|
|
376
|
+
if (isPrimitive(def) && def.config.type === "phantom") return def.id;
|
|
389
377
|
if (isRef(def)) {
|
|
390
|
-
const
|
|
391
|
-
|
|
378
|
+
const ref = def;
|
|
379
|
+
const unwound = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
380
|
+
if (isPrimitive(unwound) && unwound.config.type === "phantom") return ref.hasChain ? `${ref.id}.${ref.chain.map((c) => c.text).join(".")}` : ref.id;
|
|
392
381
|
}
|
|
393
|
-
return
|
|
382
|
+
return undefined;
|
|
394
383
|
}
|
|
395
384
|
isTypeTarget(name, doc) {
|
|
396
385
|
const d = doc || this.doc;
|
|
@@ -585,14 +574,22 @@ var Validator = class {
|
|
|
585
574
|
return false;
|
|
586
575
|
}
|
|
587
576
|
const minLength = def.metadata.get("expect.minLength");
|
|
588
|
-
if (
|
|
589
|
-
|
|
590
|
-
|
|
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
|
+
}
|
|
591
584
|
}
|
|
592
585
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
593
|
-
if (
|
|
594
|
-
|
|
595
|
-
|
|
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
|
+
}
|
|
596
593
|
}
|
|
597
594
|
let i = 0;
|
|
598
595
|
let passed = true;
|
|
@@ -724,14 +721,22 @@ else {
|
|
|
724
721
|
}
|
|
725
722
|
validateString(def, value) {
|
|
726
723
|
const minLength = def.metadata.get("expect.minLength");
|
|
727
|
-
if (
|
|
728
|
-
|
|
729
|
-
|
|
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
|
+
}
|
|
730
731
|
}
|
|
731
732
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
732
|
-
if (
|
|
733
|
-
|
|
734
|
-
|
|
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
|
+
}
|
|
735
740
|
}
|
|
736
741
|
const patterns = def.metadata.get("expect.pattern");
|
|
737
742
|
for (const { pattern, flags, message } of patterns || []) {
|
|
@@ -751,19 +756,28 @@ else {
|
|
|
751
756
|
}
|
|
752
757
|
validateNumber(def, value) {
|
|
753
758
|
const int = def.metadata.get("expect.int");
|
|
754
|
-
if (
|
|
755
|
-
|
|
759
|
+
if (int && value % 1 !== 0) {
|
|
760
|
+
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
761
|
+
this.error(message);
|
|
756
762
|
return false;
|
|
757
763
|
}
|
|
758
764
|
const min = def.metadata.get("expect.min");
|
|
759
|
-
if (
|
|
760
|
-
|
|
761
|
-
|
|
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
|
+
}
|
|
762
772
|
}
|
|
763
773
|
const max = def.metadata.get("expect.max");
|
|
764
|
-
if (
|
|
765
|
-
|
|
766
|
-
|
|
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
|
+
}
|
|
767
781
|
}
|
|
768
782
|
return true;
|
|
769
783
|
}
|
|
@@ -945,9 +959,9 @@ function buildJsonSchema(type) {
|
|
|
945
959
|
items: build(d.type.of)
|
|
946
960
|
};
|
|
947
961
|
const minLength = meta.get("expect.minLength");
|
|
948
|
-
if (typeof minLength === "number"
|
|
962
|
+
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
949
963
|
const maxLength = meta.get("expect.maxLength");
|
|
950
|
-
if (typeof maxLength === "number"
|
|
964
|
+
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
951
965
|
return schema;
|
|
952
966
|
},
|
|
953
967
|
union(d) {
|
|
@@ -972,18 +986,18 @@ function buildJsonSchema(type) {
|
|
|
972
986
|
}
|
|
973
987
|
if (schema.type === "string") {
|
|
974
988
|
const minLength = meta.get("expect.minLength");
|
|
975
|
-
if (typeof minLength === "number"
|
|
989
|
+
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
976
990
|
const maxLength = meta.get("expect.maxLength");
|
|
977
|
-
if (typeof maxLength === "number"
|
|
991
|
+
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
978
992
|
const patterns = meta.get("expect.pattern");
|
|
979
993
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
980
994
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
981
995
|
}
|
|
982
996
|
if (schema.type === "number" || schema.type === "integer") {
|
|
983
997
|
const min = meta.get("expect.min");
|
|
984
|
-
if (typeof min === "number"
|
|
998
|
+
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
985
999
|
const max = meta.get("expect.max");
|
|
986
|
-
if (typeof max === "number"
|
|
1000
|
+
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
987
1001
|
}
|
|
988
1002
|
return schema;
|
|
989
1003
|
}
|
|
@@ -1198,9 +1212,22 @@ else handle.prop(prop.id, propHandle.$type);
|
|
|
1198
1212
|
switch (a.name) {
|
|
1199
1213
|
case "expect.minLength":
|
|
1200
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;
|
|
1201
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;
|
|
1202
1226
|
case "expect.max":
|
|
1203
|
-
if (a.args[0]) handle.annotate(a.name,
|
|
1227
|
+
if (a.args[0]) handle.annotate(a.name, {
|
|
1228
|
+
maxValue: Number(a.args[0].text),
|
|
1229
|
+
message: a.args[1]?.text
|
|
1230
|
+
});
|
|
1204
1231
|
break;
|
|
1205
1232
|
case "expect.pattern":
|
|
1206
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 (
|
|
158
|
-
|
|
159
|
-
|
|
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 (
|
|
163
|
-
|
|
164
|
-
|
|
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 (
|
|
297
|
-
|
|
298
|
-
|
|
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 (
|
|
302
|
-
|
|
303
|
-
|
|
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 (
|
|
324
|
-
|
|
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 (
|
|
329
|
-
|
|
330
|
-
|
|
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 (
|
|
334
|
-
|
|
335
|
-
|
|
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"
|
|
555
|
+
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
531
556
|
const maxLength = meta.get("expect.maxLength");
|
|
532
|
-
if (typeof maxLength === "number"
|
|
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"
|
|
582
|
+
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
558
583
|
const maxLength = meta.get("expect.maxLength");
|
|
559
|
-
if (typeof maxLength === "number"
|
|
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"
|
|
591
|
+
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
567
592
|
const max = meta.get("expect.max");
|
|
568
|
-
if (typeof max === "number"
|
|
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;
|
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 (
|
|
157
|
-
|
|
158
|
-
|
|
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 (
|
|
162
|
-
|
|
163
|
-
|
|
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 (
|
|
296
|
-
|
|
297
|
-
|
|
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 (
|
|
301
|
-
|
|
302
|
-
|
|
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 (
|
|
323
|
-
|
|
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 (
|
|
328
|
-
|
|
329
|
-
|
|
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 (
|
|
333
|
-
|
|
334
|
-
|
|
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"
|
|
554
|
+
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
530
555
|
const maxLength = meta.get("expect.maxLength");
|
|
531
|
-
if (typeof maxLength === "number"
|
|
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"
|
|
581
|
+
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
557
582
|
const maxLength = meta.get("expect.maxLength");
|
|
558
|
-
if (typeof maxLength === "number"
|
|
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"
|
|
590
|
+
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
566
591
|
const max = meta.get("expect.max");
|
|
567
|
-
if (typeof max === "number"
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/typescript",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
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.
|
|
73
|
+
"@atscript/core": "^0.1.7"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
76
|
"@moostjs/event-cli": "^0.5.32",
|