@jetio/validator 1.0.2 → 1.0.4

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/index.esm.js CHANGED
@@ -245,7 +245,6 @@ class Compiler {
245
245
  this.validateKeywords = new Set();
246
246
  this.notLogic = false;
247
247
  this.noreturn = false;
248
- this.neutralError = false;
249
248
  this.standAlone = false;
250
249
  this.hasCompileKeyword = false;
251
250
  this.needslen_of = false;
@@ -438,11 +437,10 @@ class Compiler {
438
437
  return src.join("");
439
438
  }
440
439
  const schema = rootSchema;
441
- const varName = (schema.default === undefined || !this.options.useDefaults) &&
442
- !this.options.removeAdditional &&
443
- !this.options.coerceTypes
440
+ const varName = accessPattern === "rootData" ||
441
+ (inlined && (schema.default === undefined || !this.options.useDefaults))
444
442
  ? accessPattern
445
- : "var" + counter++;
443
+ : "jv" + counter++;
446
444
  this.initializeDefault(src, schema, varName, accessPattern, inlined);
447
445
  let shouldTrackProps;
448
446
  let shouldTrackItems;
@@ -486,11 +484,16 @@ class Compiler {
486
484
  return src.join("");
487
485
  }
488
486
  initializeDefault(src, schema, varName, accessPattern, inlined) {
489
- if (schema.default !== undefined && this.options.useDefaults) {
490
- src.push(`let ${varName} = ${accessPattern};`, `if (${varName} === undefined || ${varName} === null) {`, `${varName} = ${JSON.stringify(schema.default)};`, "}");
491
- }
492
- else if (this.options.removeAdditional || this.options.coerceTypes) {
493
- src.push(`let ${varName} = ${accessPattern};`);
487
+ if (accessPattern !== "rootData") {
488
+ if (schema.default !== undefined && this.options.useDefaults) {
489
+ src.push(`let ${varName} = ${accessPattern};`, `if (${varName} === undefined || ${varName} === null) {`, `${varName} = ${JSON.stringify(schema.default)};`, "}");
490
+ }
491
+ else if (!inlined) {
492
+ const keyword = this.options.removeAdditional || this.options.coerceTypes
493
+ ? "let"
494
+ : "const";
495
+ src.push(`${keyword} ${varName} = ${accessPattern};`);
496
+ }
494
497
  }
495
498
  if (this.options.coerceTypes) {
496
499
  if (schema.type === "number" || schema.type === "integer") {
@@ -606,10 +609,20 @@ class Compiler {
606
609
  src.push(`if(${extra.before} true){`);
607
610
  src.push(`const ${funcValidator}Result = ${awaitPrefix}${schema.__functionName}${callArgs};`);
608
611
  if (this.options.allErrors) {
609
- src.push(`if (!${funcValidator}Result) {${this.errorVariable} = ${this.errorVariable}.concat(${schema.__functionName}.errors);${extra.refAfter || extra.after}}`);
612
+ src.push(`if (!${funcValidator}Result){${extra.after}${this.notLogic ? "" : `${this.errorVariable} = ${this.errorVariable}.concat(${schema.__functionName}.errors);`}}`);
610
613
  }
611
614
  else {
612
- src.push(`if (!${funcValidator}Result){${this.mainFunctionName}.errors = ${schema.__functionName}.errors;${extra.refAfter ?? extra.after}${this.noreturn ? "" : "return false;"}}`);
615
+ if (this.noreturn) {
616
+ src.push(`if (!${funcValidator}Result){${extra.after}${this.notLogic ? "" : `${schema.__functionName}.errors.length > 1 ? (${this.errorVariable} = ${this.errorVariable}.concat(${schema.__functionName}.errors)) : ${this.errorVariable}.push(${schema.__functionName}.errors[0]);`}}`);
617
+ }
618
+ else {
619
+ if (this.mainFunctionName === schema.__functionName) {
620
+ src.push(`if (!${funcValidator}Result){${this.notLogic ? extra.after : `return false;`}}`);
621
+ }
622
+ else {
623
+ src.push(`if (!${funcValidator}Result){${this.notLogic ? extra.after : `${this.mainFunctionName}.errors = ${schema.__functionName}.errors;return false;`}}`);
624
+ }
625
+ }
613
626
  }
614
627
  if (extra.before != "")
615
628
  src.push(`}`);
@@ -635,10 +648,20 @@ class Compiler {
635
648
  src.push(`if(${extra.before} true){`);
636
649
  src.push(`const ${refValidator}Result = ${awaitPrefix}${functionName}${callArgs};`);
637
650
  if (this.options.allErrors) {
638
- src.push(`if (!${refValidator}Result) {${this.errorVariable} = ${this.errorVariable}.concat(${functionName}.errors);${extra.refAfter || extra.after}}`);
651
+ src.push(`if (!${refValidator}Result){${extra.after}${this.notLogic ? "" : `${this.errorVariable} = ${this.errorVariable}.concat(${functionName}.errors);`}}`);
639
652
  }
640
653
  else {
641
- src.push(`if (!${refValidator}Result){${this.mainFunctionName}.errors = ${functionName}.errors;${extra.refAfter ?? extra.after}${this.noreturn ? "" : "return false;"}}`);
654
+ if (this.noreturn) {
655
+ src.push(`if (!${refValidator}Result){${extra.after}${this.notLogic ? "" : `${functionName}.errors.length > 1 ? (${this.errorVariable} = ${this.errorVariable}.concat(${functionName}.errors)) : ${this.errorVariable}.push(${functionName}.errors[0]);`}}`);
656
+ }
657
+ else {
658
+ if (this.mainFunctionName === functionName) {
659
+ src.push(`if (!${refValidator}Result){${this.notLogic ? extra.after : `return false;`}}`);
660
+ }
661
+ else {
662
+ src.push(`if (!${refValidator}Result){${this.notLogic ? extra.after : `${this.mainFunctionName}.errors = ${functionName}.errors;return false;`}}`);
663
+ }
664
+ }
642
665
  }
643
666
  if (extra.before != "")
644
667
  src.push(`}`);
@@ -657,12 +680,12 @@ class Compiler {
657
680
  return `(${args.join(", ")})`;
658
681
  }
659
682
  handleLogicalOperators(src, schema, varName, pathContext, trackingState, extra) {
683
+ if (schema.allOf)
684
+ this.handleAllOfOperator(src, schema, varName, pathContext, trackingState, extra);
660
685
  if (schema.not)
661
686
  this.handleNotOperator(src, schema, varName, pathContext, extra);
662
687
  if (schema.anyOf)
663
688
  this.handleAnyOfOperator(src, schema, varName, pathContext, trackingState, extra);
664
- if (schema.allOf)
665
- this.handleAllOfOperator(src, schema, varName, pathContext, trackingState, extra);
666
689
  if (schema.oneOf)
667
690
  this.handleOneOfOperator(src, schema, varName, pathContext, trackingState, extra);
668
691
  }
@@ -696,9 +719,12 @@ class Compiler {
696
719
  let firstLength = "";
697
720
  const anyOfValid = "anyOfValid" + counter++;
698
721
  src.push(`let ${anyOfValid} = false;`);
699
- const anyOfError = "anyOfErr" + counter++;
700
- if (!this.options.allErrors && !this.notLogic)
722
+ const anyOfError = this.noreturn
723
+ ? this.errorVariable
724
+ : "anyOfErr" + counter++;
725
+ if (!this.options.allErrors && !this.noreturn)
701
726
  src.push(`let ${anyOfError} = [];`);
727
+ const error = this.errorVariable;
702
728
  schema.anyOf.forEach((subSchema, index) => {
703
729
  const branch = `branch${counter++}Valid`;
704
730
  if (!this.options.allErrors)
@@ -709,24 +735,27 @@ class Compiler {
709
735
  const configs = this.createSubschemaOptions(trackingState, pathContext, `anyOf/${index}`, schema);
710
736
  configs.pathContext.alt = `${pathContext.schema}/anyOf/${index}`;
711
737
  configs.pathContext.alt2 = `${pathContext.schema}/anyOf`;
738
+ let errorCountVar = "anyErrCnt" + counter++;
712
739
  if (this.options.allErrors) {
713
740
  validatorFn = this.compileSchema(subSchema, configs.pathContext, configs.trackingState, varName, extra, true);
714
741
  }
715
742
  else {
743
+ this.errorVariable = anyOfError;
716
744
  validatorFn = this.compileSchema(subSchema, configs.pathContext, configs.trackingState, varName, {
717
745
  before: `${branch} && `,
718
- after: `${branch} = false;${this.notLogic ? "" : `${anyOfError}.push(${this.mainFunctionName}.errors[0]);`}`,
719
- refAfter: `${branch} = false;${this.notLogic ? "" : `${anyOfError} = ${anyOfError}.concat(${this.mainFunctionName}.errors);`}`,
746
+ after: `${branch} = false;`,
720
747
  }, true);
721
748
  }
722
- this.noreturn = noreturn;
723
- let errorCountVar;
724
749
  if (this.options.allErrors) {
725
- errorCountVar = "anyErrCnt" + counter++;
726
750
  src.push(`const ${errorCountVar} = ${this.errorVariable}.length;`);
727
751
  if (index === 0)
728
752
  firstLength = errorCountVar;
729
753
  }
754
+ else if (index === 0) {
755
+ src.push(`const ${errorCountVar} = ${this.errorVariable}.length;`);
756
+ firstLength = errorCountVar;
757
+ }
758
+ this.noreturn = noreturn;
730
759
  if (index > 0 &&
731
760
  !trackingState.shouldTrackEvaluatedProperties &&
732
761
  !trackingState.shouldTrackEvaluatedItems) {
@@ -761,27 +790,36 @@ class Compiler {
761
790
  }
762
791
  });
763
792
  if (this.options.allErrors) {
764
- src.push(`if (${anyOfValid}) {${this.errorVariable}.length = ${firstLength};}${extra.after != "" ? `else{${extra.after}}` : ""}`);
793
+ src.push(`if (${anyOfValid} && ${this.errorVariable}.length != ${firstLength}) {${this.errorVariable}.length = ${firstLength};}${extra.after != "" ? `else if(!${anyOfValid}){${extra.after}}` : ""}`);
765
794
  }
766
795
  else {
767
- src.push(`if (${anyOfValid}){${this.mainFunctionName}.errors = undefined}else {${this.notLogic ? "" : `${this.mainFunctionName}.errors = ${anyOfError};`}${extra.refAfter ?? extra.after}${this.noreturn ? "" : "return false;"}}`);
796
+ if (this.noreturn) {
797
+ src.push(`if (${anyOfValid} && ${this.errorVariable}.length != ${firstLength}){${this.errorVariable}.length = ${firstLength};}else if(!${anyOfValid}){${extra.after}}`);
798
+ }
799
+ else {
800
+ src.push(`if (${anyOfValid} && ${this.errorVariable}.length != ${firstLength}){${this.errorVariable}.length = ${firstLength};}else if(!${anyOfValid}){${this.notLogic ? extra.after : `${this.mainFunctionName}.errors = ${this.errorVariable};return false;`}}`);
801
+ }
802
+ this.errorVariable = error;
768
803
  }
769
804
  }
770
805
  handleOneOfOperator(src, schema, varName, pathContext, trackingState, extra) {
771
806
  if (!schema.oneOf)
772
807
  return;
773
808
  let firstLength = "";
774
- const oneOfErrors = `oneOfErrors${counter++}`;
809
+ const oneOfErrors = this.noreturn
810
+ ? this.errorVariable
811
+ : `oneOfErrors${counter++}`;
775
812
  const validSchemaCount = "validSchemaCount" + counter++;
776
813
  src.push(`let ${validSchemaCount} = 0;`);
777
- if (!this.options.allErrors)
814
+ if (!this.options.allErrors && !this.noreturn)
778
815
  src.push(`let ${oneOfErrors} = [];`);
816
+ const noreturn = this.noreturn;
817
+ const error = this.errorVariable;
779
818
  schema.oneOf.forEach((subSchema, index) => {
780
819
  const branch = `branch${counter++}Valid`;
781
- if (!this.options.allErrors && !this.notLogic)
820
+ if (!this.options.allErrors)
782
821
  src.push(`let ${branch} = true;`);
783
822
  let validatorFn;
784
- const noreturn = this.noreturn;
785
823
  this.noreturn = true;
786
824
  const configs = this.createSubschemaOptions(trackingState, pathContext, `oneOf/${index}`, schema);
787
825
  configs.pathContext.alt = `${pathContext.schema}/oneOf/${index}`;
@@ -790,20 +828,22 @@ class Compiler {
790
828
  validatorFn = this.compileSchema(subSchema, configs.pathContext, configs.trackingState, varName, extra, true);
791
829
  }
792
830
  else {
831
+ this.errorVariable = oneOfErrors;
793
832
  validatorFn = this.compileSchema(subSchema, configs.pathContext, configs.trackingState, varName, {
794
833
  before: `${branch} && `,
795
- after: `${branch} = false;${this.notLogic ? "" : `${oneOfErrors}.push(${this.mainFunctionName}.errors[0]);`}`,
796
- refAfter: `${branch} = false;${this.notLogic ? "" : `${oneOfErrors} = ${oneOfErrors}.concat(${this.mainFunctionName}.errors);`}`,
834
+ after: `${branch} = false;`,
797
835
  }, true);
798
836
  }
799
- this.noreturn = noreturn;
800
- let errorCountVar;
837
+ let errorCountVar = "oneErrCnt" + counter++;
801
838
  if (this.options.allErrors) {
802
- errorCountVar = "oneErrCnt" + counter;
803
839
  src.push(`const ${errorCountVar} = ${this.errorVariable}.length;`);
804
840
  if (index === 0)
805
841
  firstLength = errorCountVar;
806
842
  }
843
+ else if (index === 0) {
844
+ src.push(`const ${errorCountVar} = ${this.errorVariable}.length;`);
845
+ firstLength = errorCountVar;
846
+ }
807
847
  if (index > 0 &&
808
848
  !trackingState.shouldTrackEvaluatedProperties &&
809
849
  !trackingState.shouldTrackEvaluatedItems) {
@@ -838,7 +878,7 @@ class Compiler {
838
878
  }
839
879
  });
840
880
  if (this.options.allErrors) {
841
- src.push(`if (${validSchemaCount} == 1) {${this.errorVariable}.length = ${firstLength};} else{${this.buildErrorReturn(pathContext, {
881
+ src.push(`if (${validSchemaCount} == 1 && ${this.errorVariable}.length != ${firstLength}) {${this.errorVariable}.length = ${firstLength};}else if(${validSchemaCount} != 1){${this.buildErrorReturn(pathContext, {
842
882
  keyword: "oneOf",
843
883
  value: varName,
844
884
  message: `"Data must validate against exactly one schema, but matched "+ ${validSchemaCount}`,
@@ -846,14 +886,23 @@ class Compiler {
846
886
  })}${extra.after}}`);
847
887
  }
848
888
  else {
849
- const noreturn = this.noreturn;
850
- this.noreturn = true;
851
- src.push(`if (${validSchemaCount} == 1){${this.mainFunctionName}.errors = undefined}else {${this.buildErrorReturn(pathContext, {
852
- keyword: "oneOf",
853
- value: varName,
854
- message: `"Data must validate against exactly one schema, but matched "+ ${validSchemaCount}`,
855
- expected: '"exactly one schema"',
856
- })}${this.notLogic ? "" : `${oneOfErrors}.push(${this.mainFunctionName}.errors[0]);${this.mainFunctionName}.errors = ${oneOfErrors};`}${extra.refAfter ?? extra.after}${noreturn ? "" : "return false;"}}`);
889
+ if (noreturn) {
890
+ src.push(`if (${validSchemaCount} == 1 && ${this.errorVariable}.length != ${firstLength}){${this.errorVariable}.length = ${firstLength};}else if(${validSchemaCount} != 1){${this.buildErrorReturn(pathContext, {
891
+ keyword: "oneOf",
892
+ value: varName,
893
+ message: `"Data must validate against exactly one schema, but matched "+ ${validSchemaCount}`,
894
+ expected: '"exactly one schema"',
895
+ })}${extra.after}}`);
896
+ }
897
+ else {
898
+ src.push(`if (${validSchemaCount} == 1 && ${this.errorVariable}.length != ${firstLength}){${this.errorVariable}.length = ${firstLength};}else if(${validSchemaCount} != 1){${this.buildErrorReturn(pathContext, {
899
+ keyword: "oneOf",
900
+ value: varName,
901
+ message: `"Data must validate against exactly one schema, but matched "+ ${validSchemaCount}`,
902
+ expected: '"exactly one schema"',
903
+ })}${this.notLogic ? extra.after : `${this.mainFunctionName}.errors = ${this.errorVariable};return false;`}}`);
904
+ }
905
+ this.errorVariable = error;
857
906
  this.noreturn = noreturn;
858
907
  }
859
908
  }
@@ -1441,7 +1490,7 @@ class Compiler {
1441
1490
  else {
1442
1491
  const data = this.jetValidator.getFormat(schema.format);
1443
1492
  if (!data) {
1444
- throw new Error(`Format '${schema.format}' not found`);
1493
+ console.warn(`Format '${schema.format}' not found will be ignored`);
1445
1494
  }
1446
1495
  const format = typeof data === "object" && "validate" in data ? data.validate : data;
1447
1496
  const formatType = typeof data === "object" &&
@@ -1563,24 +1612,39 @@ class Compiler {
1563
1612
  if (Array.isArray(schema.required)) {
1564
1613
  if (this.options.allErrors ||
1565
1614
  schema.required.length > this.options.loopRequired) {
1566
- const arr = JSON.stringify(schema.required);
1567
- const arrVar = `arr${src.length}${counter++}`;
1568
- const iVar = `i${src.length}${counter++}`;
1569
- src.push(`const ${arrVar} = ${arr};`);
1570
1615
  if (extra.before != "")
1571
1616
  src.push(`if(${extra.before} true){`);
1572
- src.push(`for (let ${iVar} = 0; ${iVar} < ${arrVar}.length; ${iVar}++) {`);
1573
- const prop = "prop" + counter++;
1574
- src.push(`const ${prop} = ${arrVar}[${iVar}];`);
1575
- addEvaluatedProperty(src, prop, trackingState);
1576
- src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1577
- keyword: "required",
1578
- value: varName,
1579
- message: `"Missing required field: " + ${prop} + " in data."`,
1580
- expected: `${prop}`,
1581
- schemaPath: `${pathContext.schema}`,
1582
- })}${extra.after}}`);
1583
- src.push(`}`);
1617
+ if (schema.required.length > this.options.loopRequired) {
1618
+ const arr = JSON.stringify(schema.required);
1619
+ const arrVar = `arr${src.length}${counter++}`;
1620
+ const iVar = `i${src.length}${counter++}`;
1621
+ src.push(`const ${arrVar} = ${arr};`);
1622
+ src.push(`for (let ${iVar} = 0; ${iVar} < ${arrVar}.length; ${iVar}++) {`);
1623
+ const prop = "prop" + counter++;
1624
+ src.push(`const ${prop} = ${arrVar}[${iVar}];`);
1625
+ addEvaluatedProperty(src, prop, trackingState);
1626
+ src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1627
+ keyword: "required",
1628
+ value: varName,
1629
+ message: `"Missing required field: " + ${prop} + " in data."`,
1630
+ expected: `${prop}`,
1631
+ schemaPath: `${pathContext.schema}`,
1632
+ })}${extra.after}}`);
1633
+ src.push(`}`);
1634
+ }
1635
+ else {
1636
+ for (const prop of schema.required) {
1637
+ const errorMessage = JSON.stringify(`Missing required field: ${prop} in data.`);
1638
+ const pstring = JSON.stringify(prop);
1639
+ src.push(`if (${extra.before}${varName}[${pstring}] === undefined) {${this.buildErrorReturn(pathContext, {
1640
+ keyword: "required",
1641
+ value: varName,
1642
+ message: errorMessage,
1643
+ expected: `${pstring}`,
1644
+ schemaPath: `${pathContext.schema}`,
1645
+ })}${extra.after}}`);
1646
+ }
1647
+ }
1584
1648
  if (extra.before != "")
1585
1649
  src.push(`}`);
1586
1650
  }
@@ -2248,9 +2312,7 @@ class Compiler {
2248
2312
  buildErrorReturn(pathContext, error, spreads) {
2249
2313
  if (this.notLogic)
2250
2314
  return "";
2251
- if (this.neutralError)
2252
- return "return false;";
2253
- let result = this.options.allErrors
2315
+ let result = this.options.allErrors || this.noreturn
2254
2316
  ? `${this.errorVariable}.push({`
2255
2317
  : `${this.mainFunctionName}.errors = [{`;
2256
2318
  const escapedDataPath = escapeTemplateString(error.dataPath || pathContext.data || "/");
@@ -2258,11 +2320,11 @@ class Compiler {
2258
2320
  result += `dataPath: \`${escapedDataPath}\`,`;
2259
2321
  result += `schemaPath: \`${escapedSchemaPath}\`,`;
2260
2322
  result += `keyword: "${error.keyword}",`;
2323
+ if (error.expected) {
2324
+ result += `expected: ${error.expected},`;
2325
+ }
2261
2326
  if (this.options.verbose) {
2262
2327
  result += `value: ${error.value},`;
2263
- if (error.expected) {
2264
- result += `expected: ${error.expected},`;
2265
- }
2266
2328
  }
2267
2329
  let errorMessage;
2268
2330
  if (this.options.errorMessage && typeof this.schema !== "boolean") {
@@ -2331,7 +2393,7 @@ class Compiler {
2331
2393
  result += `,${spreads}`;
2332
2394
  }
2333
2395
  result += "}";
2334
- if (this.options.allErrors) {
2396
+ if (this.options.allErrors || this.noreturn) {
2335
2397
  result += ");";
2336
2398
  }
2337
2399
  else {
@@ -5078,7 +5140,7 @@ class SchemaResolver {
5078
5140
  throw Error(`[${mode}] Unknown type ${type}`);
5079
5141
  }
5080
5142
  }
5081
- for (const keyword of allPossibleIncompatible) {
5143
+ for (const keyword of Array.from(allPossibleIncompatible)) {
5082
5144
  const incompatibleWithAll = types.every((type) => incompatibleKeywords[type]?.includes(keyword));
5083
5145
  if (incompatibleWithAll && schema[keyword] !== undefined) {
5084
5146
  throw Error(`[${mode}] Keyword "${keyword}" is incompatible with ${types.length > 1 ? "all types" : "type"} "${types.join(", ")}"`);
@@ -5640,6 +5702,7 @@ class JetValidator {
5640
5702
  const fconfig = {
5641
5703
  ...config,
5642
5704
  };
5705
+ const has$Data = compileContext.uses$Data;
5643
5706
  if (typeof resolvedSchema === "boolean")
5644
5707
  fconfig.allErrors = false;
5645
5708
  const compiler = new Compiler(refables, mainSchema, fconfig, this, allKeywords, compileContext, false);
@@ -5672,22 +5735,6 @@ class JetValidator {
5672
5735
  if (validate)
5673
5736
  customKeywords.set(keywordDef, validate);
5674
5737
  }
5675
- if (typeof resolvedSchema !== "boolean") {
5676
- if (allFormats.size > 0) {
5677
- for (const validatorKey of allFormats) {
5678
- const validator = this.formatValidators[validatorKey];
5679
- if (validator) {
5680
- if (typeof validator === "function" ||
5681
- validator instanceof RegExp) {
5682
- formatValidators[validatorKey] = validator;
5683
- }
5684
- else {
5685
- formatValidators[validatorKey] = validator.validate;
5686
- }
5687
- }
5688
- }
5689
- }
5690
- }
5691
5738
  const asyncPrefix = config.async ? "async " : "";
5692
5739
  let functionDeclaration = "validate(rootData";
5693
5740
  if (compileContext.hasRootReference) {
@@ -5720,6 +5767,40 @@ class JetValidator {
5720
5767
  regexArgs.push(new RegExp(key));
5721
5768
  }
5722
5769
  }
5770
+ if (typeof resolvedSchema !== "boolean") {
5771
+ if (has$Data) {
5772
+ const formatKeys = Array.isArray(fconfig.formats) && fconfig.formats.length > 0
5773
+ ? fconfig.formats
5774
+ : Object.keys(this.formatValidators);
5775
+ for (const validatorKey of formatKeys) {
5776
+ const validator = this.formatValidators[validatorKey];
5777
+ if (validator) {
5778
+ if (typeof validator === "function" ||
5779
+ validator instanceof RegExp) {
5780
+ formatValidators[validatorKey] = validator;
5781
+ }
5782
+ else {
5783
+ formatValidators[validatorKey] = validator.validate;
5784
+ }
5785
+ }
5786
+ }
5787
+ }
5788
+ else if (allFormats.size > 0) {
5789
+ for (const validatorKey of allFormats) {
5790
+ const validator = this.formatValidators[validatorKey];
5791
+ if (validator) {
5792
+ if (typeof validator === "function" ||
5793
+ validator instanceof RegExp) {
5794
+ formatValidators[validatorKey] = validator;
5795
+ }
5796
+ else {
5797
+ formatValidators[validatorKey] = validator.validate;
5798
+ }
5799
+ }
5800
+ }
5801
+ }
5802
+ }
5803
+ Object.values(formatValidators);
5723
5804
  return new Function("formatValidators", "deepEqual", "canonicalStringify", "customKeywords", "len_of", ...regexParams, finalSource)(formatValidators, deepEqual, canonicalStringify, customKeywords, len_of, ...regexArgs);
5724
5805
  }
5725
5806
  compile(fschema, config) {
@@ -460,6 +460,7 @@ class JetValidator {
460
460
  const fconfig = {
461
461
  ...config,
462
462
  };
463
+ const has$Data = compileContext.uses$Data;
463
464
  if (typeof resolvedSchema === "boolean")
464
465
  fconfig.allErrors = false;
465
466
  const compiler = new compileSchema_1.Compiler(refables, mainSchema, fconfig, this, allKeywords, compileContext, false);
@@ -492,22 +493,6 @@ class JetValidator {
492
493
  if (validate)
493
494
  customKeywords.set(keywordDef, validate);
494
495
  }
495
- if (typeof resolvedSchema !== "boolean") {
496
- if (allFormats.size > 0) {
497
- for (const validatorKey of allFormats) {
498
- const validator = this.formatValidators[validatorKey];
499
- if (validator) {
500
- if (typeof validator === "function" ||
501
- validator instanceof RegExp) {
502
- formatValidators[validatorKey] = validator;
503
- }
504
- else {
505
- formatValidators[validatorKey] = validator.validate;
506
- }
507
- }
508
- }
509
- }
510
- }
511
496
  const asyncPrefix = config.async ? "async " : "";
512
497
  let functionDeclaration = "validate(rootData";
513
498
  if (compileContext.hasRootReference) {
@@ -540,6 +525,41 @@ class JetValidator {
540
525
  regexArgs.push(new RegExp(key));
541
526
  }
542
527
  }
528
+ if (typeof resolvedSchema !== "boolean") {
529
+ if (has$Data) {
530
+ const formatKeys = Array.isArray(fconfig.formats) && fconfig.formats.length > 0
531
+ ? fconfig.formats
532
+ : Object.keys(this.formatValidators);
533
+ for (const validatorKey of formatKeys) {
534
+ const validator = this.formatValidators[validatorKey];
535
+ if (validator) {
536
+ if (typeof validator === "function" ||
537
+ validator instanceof RegExp) {
538
+ formatValidators[validatorKey] = validator;
539
+ }
540
+ else {
541
+ formatValidators[validatorKey] = validator.validate;
542
+ }
543
+ }
544
+ }
545
+ }
546
+ else if (allFormats.size > 0) {
547
+ for (const validatorKey of allFormats) {
548
+ const validator = this.formatValidators[validatorKey];
549
+ if (validator) {
550
+ if (typeof validator === "function" ||
551
+ validator instanceof RegExp) {
552
+ formatValidators[validatorKey] = validator;
553
+ }
554
+ else {
555
+ formatValidators[validatorKey] = validator.validate;
556
+ }
557
+ }
558
+ }
559
+ }
560
+ }
561
+ const validatorKeys = Object.keys(formatValidators);
562
+ const validatorValues = Object.values(formatValidators);
543
563
  return new Function("formatValidators", "deepEqual", "canonicalStringify", "customKeywords", "len_of", ...regexParams, finalSource)(formatValidators, utilities_1.deepEqual, utilities_1.canonicalStringify, customKeywords, utilities_1.len_of, ...regexArgs);
544
564
  }
545
565
  compile(fschema, config) {