@jetio/validator 1.0.1 → 1.0.2

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.cjs.js CHANGED
@@ -1323,11 +1323,15 @@ class Compiler {
1323
1323
  else {
1324
1324
  comparisonTarget = schema.multipleOf;
1325
1325
  }
1326
- src.push(`const multipleOf = ${comparisonTarget};
1327
- const quotient = ${varName} / multipleOf;
1328
- const rounded = Math.round(quotient);
1329
- const tolerance = Math.abs(quotient) * Number.EPSILON;
1330
- if (${extra.before}multipleOf === 0 || !isFinite(quotient) || Math.abs(quotient - rounded) > tolerance) {${this.buildErrorReturn(pathContext, {
1326
+ const multipleOfVar = "multipleOf" + counter++;
1327
+ const quotientVar = "quotient" + counter++;
1328
+ const roundedVar = "rounded" + counter++;
1329
+ const toleranceVar = "tolerance" + counter++;
1330
+ src.push(`const ${multipleOfVar} = ${comparisonTarget};
1331
+ const ${quotientVar} = ${varName} / ${multipleOfVar};
1332
+ const ${roundedVar} = Math.round(${quotientVar});
1333
+ const ${toleranceVar} = Math.abs(${quotientVar}) * Number.EPSILON;
1334
+ if (${extra.before}${multipleOfVar} === 0 || !isFinite(${quotientVar}) || Math.abs(${quotientVar} - ${roundedVar}) > ${toleranceVar}) {${this.buildErrorReturn(pathContext, {
1331
1335
  keyword: "multipleOf",
1332
1336
  value: varName,
1333
1337
  message: `"Value must be a multiple of " + ${comparisonTarget}`,
@@ -1430,7 +1434,8 @@ class Compiler {
1430
1434
  else {
1431
1435
  src.push(`const formatValidator = formatValidators[${formatKeyVar}].validate ?? formatValidators[${formatKeyVar}];`);
1432
1436
  }
1433
- src.push(`if (${extra.before}formatValidator && typeof ${varName} === 'string') {`, ` const isValid = typeof formatValidator === 'function' ? ${this.options.async ? "async" : ""}formatValidator(${varName}) : formatValidator.test(${varName});`, ` if (!isValid) {${this.buildErrorReturn(pathContext, {
1437
+ const isValid = "isValid" + counter++;
1438
+ src.push(`if (${extra.before}formatValidator && typeof ${varName} === 'string') {`, ` const ${isValid} = typeof formatValidator === 'function' ? ${this.options.async ? "async" : ""}formatValidator(${varName}) : formatValidator.test(${varName});`, ` if (!${isValid}) {${this.buildErrorReturn(pathContext, {
1434
1439
  keyword: "format",
1435
1440
  value: varName,
1436
1441
  message: `"Failed to validate value against format "+${formatKeyVar}`,
@@ -1539,14 +1544,16 @@ class Compiler {
1539
1544
  const resolvedPath = resolveDataPointerAtCompileTime(pointer, pathContext.$data, this.refCall);
1540
1545
  const requiredArrayExpr = generateArrayDataRef(src, resolvedPath, extra);
1541
1546
  src.push(`if (${extra.before}${requiredArrayExpr}) {`);
1542
- src.push(`for (let i = 0; i < ${requiredArrayExpr}.length; i++) {`);
1543
- src.push(`const prop = ${requiredArrayExpr}[i];`);
1544
- addEvaluatedProperty(src, "prop", trackingState);
1545
- src.push(`if (${extra.before}${varName}[prop] === undefined) {${this.buildErrorReturn(pathContext, {
1547
+ const i = "i" + counter++;
1548
+ src.push(`for (let ${i} = 0; ${i} < ${requiredArrayExpr}.length; ${i}++) {`);
1549
+ const prop = "prop" + counter++;
1550
+ src.push(`const ${prop} = ${requiredArrayExpr}[${i}];`);
1551
+ addEvaluatedProperty(src, prop, trackingState);
1552
+ src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1546
1553
  keyword: "required",
1547
1554
  value: varName,
1548
- message: `"Missing required field: " + prop + " in data."`,
1549
- expected: "prop",
1555
+ message: `"Missing required field: " + ${prop} + " in data."`,
1556
+ expected: `${prop}`,
1550
1557
  schemaPath: `${pathContext.schema}`,
1551
1558
  })}${extra.after}}`);
1552
1559
  src.push(`}`);
@@ -1567,13 +1574,14 @@ class Compiler {
1567
1574
  if (extra.before != "")
1568
1575
  src.push(`if(${extra.before} true){`);
1569
1576
  src.push(`for (let ${iVar} = 0; ${iVar} < ${arrVar}.length; ${iVar}++) {`);
1570
- src.push(`const prop = ${arrVar}[${iVar}];`);
1571
- addEvaluatedProperty(src, "prop", trackingState);
1572
- src.push(`if (${extra.before}${varName}[prop] === undefined) {${this.buildErrorReturn(pathContext, {
1577
+ const prop = "prop" + counter++;
1578
+ src.push(`const ${prop} = ${arrVar}[${iVar}];`);
1579
+ addEvaluatedProperty(src, prop, trackingState);
1580
+ src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1573
1581
  keyword: "required",
1574
1582
  value: varName,
1575
- message: `"Missing required field: " + prop + " in data."`,
1576
- expected: "prop",
1583
+ message: `"Missing required field: " + ${prop} + " in data."`,
1584
+ expected: `${prop}`,
1577
1585
  schemaPath: `${pathContext.schema}`,
1578
1586
  })}${extra.after}}`);
1579
1587
  src.push(`}`);
@@ -1629,21 +1637,22 @@ class Compiler {
1629
1637
  handlePatternProperties(src, schema, varName, pathContext, trackingState, extra) {
1630
1638
  if (extra.before != "")
1631
1639
  src.push(`if(${extra.before} true){`);
1632
- src.push(`for (const key in ${varName}) {`);
1640
+ const key = "key" + counter++;
1641
+ src.push(`for (const ${key} in ${varName}) {`);
1633
1642
  Object.getOwnPropertyNames(schema.patternProperties).forEach((pattern) => {
1634
1643
  let pname = this.regexCache.get(pattern);
1635
1644
  if (!pname) {
1636
1645
  pname = "patternProp" + counter++;
1637
1646
  this.regexCache.set(pattern, pname);
1638
1647
  }
1639
- src.push(`if (${pname}.test(key)) {`);
1640
- addEvaluatedProperty(src, "key", trackingState);
1648
+ src.push(`if (${pname}.test(${key})) {`);
1649
+ addEvaluatedProperty(src, key, trackingState);
1641
1650
  const parent = trackingState.parentHasUnevaluatedProperties ||
1642
1651
  trackingState.hasOwnUnevaluatedProperties;
1643
1652
  const patternValidation = this.compileSchema(schema.patternProperties[pattern], {
1644
1653
  schema: `${pathContext.schema}/patternProperties/${JSON.stringify(pattern)}`,
1645
- data: `${pathContext.data}/\${key}`,
1646
- $data: `${pathContext.$data}/\${key}`,
1654
+ data: `${pathContext.data}/\${${key}}`,
1655
+ $data: `${pathContext.$data}/\${${key}}`,
1647
1656
  alt: pathContext.alt,
1648
1657
  alt2: pathContext.alt2,
1649
1658
  }, {
@@ -1652,7 +1661,7 @@ class Compiler {
1652
1661
  parentUnevaluatedPropVar: parent
1653
1662
  ? trackingState.unevaluatedPropVar
1654
1663
  : undefined,
1655
- }, `${varName}[key]`, extra);
1664
+ }, `${varName}[${key}]`, extra);
1656
1665
  src.push(patternValidation, "}");
1657
1666
  });
1658
1667
  src.push("}");
@@ -1664,12 +1673,13 @@ class Compiler {
1664
1673
  const explicitProps = Array.from(allowedProperties);
1665
1674
  if (extra.before != "")
1666
1675
  src.push(`if(${extra.before} true){`);
1667
- src.push(`for (const key in ${varName}) {`);
1668
- addEvaluatedProperty(src, "key", trackingState);
1676
+ const key = "key" + counter++;
1677
+ src.push(`for (const ${key} in ${varName}) {`);
1678
+ addEvaluatedProperty(src, key, trackingState);
1669
1679
  let checks = [];
1670
1680
  if (explicitProps.length > 0) {
1671
1681
  const allowedCheck = explicitProps
1672
- .map((key) => `key === ${JSON.stringify(key)}`)
1682
+ .map((keyItem) => `${key} === ${JSON.stringify(keyItem)}`)
1673
1683
  .join(" || ");
1674
1684
  checks.push(allowedCheck);
1675
1685
  }
@@ -1681,7 +1691,7 @@ class Compiler {
1681
1691
  pname = "patternProp" + counter++;
1682
1692
  this.regexCache.set(pattern, pname);
1683
1693
  }
1684
- return `${pname}.test(key)`;
1694
+ return `${pname}.test(${key})`;
1685
1695
  })
1686
1696
  .join(" || ");
1687
1697
  checks.push(patternCheck);
@@ -1692,17 +1702,18 @@ class Compiler {
1692
1702
  }
1693
1703
  const additionalPropValidation = this.compileSchema(schema.additionalProperties, {
1694
1704
  schema: `${pathContext.schema}/additionalProperties`,
1695
- data: `${pathContext.data}/\${key}`,
1696
- $data: `${pathContext.$data}/\${key}`,
1705
+ data: `${pathContext.data}/\${${key}}`,
1706
+ $data: `${pathContext.$data}/\${${key}}`,
1697
1707
  alt: pathContext.alt,
1698
1708
  alt2: pathContext.alt2,
1699
- }, {}, `${varName}[key]`, extra);
1709
+ }, {}, `${varName}[${key}]`, extra);
1700
1710
  src.push(additionalPropValidation, "}");
1701
1711
  if (extra.before != "")
1702
1712
  src.push(`}`);
1703
1713
  }
1704
1714
  handlePropertyConstraints(src, schema, varName, pathContext, extra) {
1705
- src.push(`const objKeys = Object.keys(${varName});`);
1715
+ const objKeys = "objKeys" + counter++;
1716
+ src.push(`const ${objKeys} = Object.keys(${varName});`);
1706
1717
  if (schema.minProperties !== undefined) {
1707
1718
  const isDataRef = this.options.$data && isDataReference(schema.minProperties);
1708
1719
  let comparisonTarget;
@@ -1714,9 +1725,9 @@ class Compiler {
1714
1725
  else {
1715
1726
  comparisonTarget = schema.minProperties;
1716
1727
  }
1717
- src.push(`if (${extra.before}objKeys.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1728
+ src.push(`if (${extra.before}${objKeys}.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1718
1729
  keyword: "minProperties",
1719
- value: "objKeys.length",
1730
+ value: `${objKeys}.length`,
1720
1731
  message: `"Object must have at least " + ${comparisonTarget} + " properties."`,
1721
1732
  expected: comparisonTarget,
1722
1733
  })}${extra.after}}`);
@@ -1735,9 +1746,9 @@ class Compiler {
1735
1746
  else {
1736
1747
  comparisonTarget = schema.maxProperties;
1737
1748
  }
1738
- src.push(`if (${extra.before}objKeys.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1749
+ src.push(`if (${extra.before}${objKeys}.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1739
1750
  keyword: "maxProperties",
1740
- value: "objKeys.length",
1751
+ value: `${objKeys}.length`,
1741
1752
  message: `"Object must have at most " + ${comparisonTarget} + " properties."`,
1742
1753
  expected: comparisonTarget,
1743
1754
  })}${extra.after}}`);
@@ -1749,14 +1760,15 @@ class Compiler {
1749
1760
  handlePropertyNames(src, schema, varName, pathContext, extra) {
1750
1761
  if (extra.before != "")
1751
1762
  src.push(`if(${extra.before} true){`);
1752
- src.push(`for (const key in ${varName}) {`);
1763
+ const key = "key" + counter++;
1764
+ src.push(`for (const ${key} in ${varName}) {`);
1753
1765
  const propertyNameValidation = this.compileSchema(schema.propertyNames, {
1754
1766
  schema: `${pathContext.schema}/propertyNames`,
1755
- data: `${pathContext.data}/\${key}`,
1756
- $data: `${pathContext.$data}/\${key}`,
1767
+ data: `${pathContext.data}/\${${key}}`,
1768
+ $data: `${pathContext.$data}/\${${key}}`,
1757
1769
  alt: pathContext.alt,
1758
1770
  alt2: pathContext.alt2,
1759
- }, {}, `key`, extra);
1771
+ }, {}, key, extra);
1760
1772
  src.push(propertyNameValidation, "}");
1761
1773
  if (extra.before != "")
1762
1774
  src.push(`}`);
@@ -1868,7 +1880,8 @@ class Compiler {
1868
1880
  const evalSet = trackingState.unevaluatedPropVar;
1869
1881
  if (extra.before != "")
1870
1882
  src.push(`if(${extra.before} true){`);
1871
- src.push(`const ${unName} = [];`, `for (const key in ${varName}) {`, `if (!${evalSet}.has(key)) {`, `${unName}.push(key);`, `}`, `}`);
1883
+ const key = "key" + counter++;
1884
+ src.push(`const ${unName} = [];`, `for (const ${key} in ${varName}) {`, `if (!${evalSet}.has(${key})) {`, `${unName}.push(${key});`, `}`, `}`);
1872
1885
  if (schema.unevaluatedProperties === false) {
1873
1886
  src.push(`if (${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
1874
1887
  keyword: "unevaluatedProperties",
@@ -1881,7 +1894,8 @@ class Compiler {
1881
1894
  }
1882
1895
  else if (schema.unevaluatedProperties === true) {
1883
1896
  if (trackingState.parentHasUnevaluatedProperties) {
1884
- src.push(`for(const key in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(key)}`);
1897
+ const key = "key" + counter++;
1898
+ src.push(`for(const ${key} in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(${key})}`);
1885
1899
  }
1886
1900
  }
1887
1901
  else {
@@ -2199,7 +2213,8 @@ class Compiler {
2199
2213
  const evalSet = trackingState.unevaluatedItemVar;
2200
2214
  if (extra.before != "")
2201
2215
  src.push(`if(${extra.before} true){`);
2202
- src.push(`const ${unName} = [];`, `for (let i = 0; i < ${varName}.length; i++) {`, `if (!${evalSet}.has(i)) {`, `${unName}.push(i);`, `}`, `}`);
2216
+ const i = "i" + counter++;
2217
+ src.push(`const ${unName} = [];`, `for (let ${i} = 0; ${i} < ${varName}.length; ${i}++) {`, `if (!${evalSet}.has(${i})) {`, `${unName}.push(${i});`, `}`, `}`);
2203
2218
  if (schema.unevaluatedItems === false) {
2204
2219
  src.push(`if (${extra.before}${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
2205
2220
  keyword: "unevaluatedItems",
package/dist/index.esm.js CHANGED
@@ -1319,11 +1319,15 @@ class Compiler {
1319
1319
  else {
1320
1320
  comparisonTarget = schema.multipleOf;
1321
1321
  }
1322
- src.push(`const multipleOf = ${comparisonTarget};
1323
- const quotient = ${varName} / multipleOf;
1324
- const rounded = Math.round(quotient);
1325
- const tolerance = Math.abs(quotient) * Number.EPSILON;
1326
- if (${extra.before}multipleOf === 0 || !isFinite(quotient) || Math.abs(quotient - rounded) > tolerance) {${this.buildErrorReturn(pathContext, {
1322
+ const multipleOfVar = "multipleOf" + counter++;
1323
+ const quotientVar = "quotient" + counter++;
1324
+ const roundedVar = "rounded" + counter++;
1325
+ const toleranceVar = "tolerance" + counter++;
1326
+ src.push(`const ${multipleOfVar} = ${comparisonTarget};
1327
+ const ${quotientVar} = ${varName} / ${multipleOfVar};
1328
+ const ${roundedVar} = Math.round(${quotientVar});
1329
+ const ${toleranceVar} = Math.abs(${quotientVar}) * Number.EPSILON;
1330
+ if (${extra.before}${multipleOfVar} === 0 || !isFinite(${quotientVar}) || Math.abs(${quotientVar} - ${roundedVar}) > ${toleranceVar}) {${this.buildErrorReturn(pathContext, {
1327
1331
  keyword: "multipleOf",
1328
1332
  value: varName,
1329
1333
  message: `"Value must be a multiple of " + ${comparisonTarget}`,
@@ -1426,7 +1430,8 @@ class Compiler {
1426
1430
  else {
1427
1431
  src.push(`const formatValidator = formatValidators[${formatKeyVar}].validate ?? formatValidators[${formatKeyVar}];`);
1428
1432
  }
1429
- src.push(`if (${extra.before}formatValidator && typeof ${varName} === 'string') {`, ` const isValid = typeof formatValidator === 'function' ? ${this.options.async ? "async" : ""}formatValidator(${varName}) : formatValidator.test(${varName});`, ` if (!isValid) {${this.buildErrorReturn(pathContext, {
1433
+ const isValid = "isValid" + counter++;
1434
+ src.push(`if (${extra.before}formatValidator && typeof ${varName} === 'string') {`, ` const ${isValid} = typeof formatValidator === 'function' ? ${this.options.async ? "async" : ""}formatValidator(${varName}) : formatValidator.test(${varName});`, ` if (!${isValid}) {${this.buildErrorReturn(pathContext, {
1430
1435
  keyword: "format",
1431
1436
  value: varName,
1432
1437
  message: `"Failed to validate value against format "+${formatKeyVar}`,
@@ -1535,14 +1540,16 @@ class Compiler {
1535
1540
  const resolvedPath = resolveDataPointerAtCompileTime(pointer, pathContext.$data, this.refCall);
1536
1541
  const requiredArrayExpr = generateArrayDataRef(src, resolvedPath, extra);
1537
1542
  src.push(`if (${extra.before}${requiredArrayExpr}) {`);
1538
- src.push(`for (let i = 0; i < ${requiredArrayExpr}.length; i++) {`);
1539
- src.push(`const prop = ${requiredArrayExpr}[i];`);
1540
- addEvaluatedProperty(src, "prop", trackingState);
1541
- src.push(`if (${extra.before}${varName}[prop] === undefined) {${this.buildErrorReturn(pathContext, {
1543
+ const i = "i" + counter++;
1544
+ src.push(`for (let ${i} = 0; ${i} < ${requiredArrayExpr}.length; ${i}++) {`);
1545
+ const prop = "prop" + counter++;
1546
+ src.push(`const ${prop} = ${requiredArrayExpr}[${i}];`);
1547
+ addEvaluatedProperty(src, prop, trackingState);
1548
+ src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1542
1549
  keyword: "required",
1543
1550
  value: varName,
1544
- message: `"Missing required field: " + prop + " in data."`,
1545
- expected: "prop",
1551
+ message: `"Missing required field: " + ${prop} + " in data."`,
1552
+ expected: `${prop}`,
1546
1553
  schemaPath: `${pathContext.schema}`,
1547
1554
  })}${extra.after}}`);
1548
1555
  src.push(`}`);
@@ -1563,13 +1570,14 @@ class Compiler {
1563
1570
  if (extra.before != "")
1564
1571
  src.push(`if(${extra.before} true){`);
1565
1572
  src.push(`for (let ${iVar} = 0; ${iVar} < ${arrVar}.length; ${iVar}++) {`);
1566
- src.push(`const prop = ${arrVar}[${iVar}];`);
1567
- addEvaluatedProperty(src, "prop", trackingState);
1568
- src.push(`if (${extra.before}${varName}[prop] === undefined) {${this.buildErrorReturn(pathContext, {
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, {
1569
1577
  keyword: "required",
1570
1578
  value: varName,
1571
- message: `"Missing required field: " + prop + " in data."`,
1572
- expected: "prop",
1579
+ message: `"Missing required field: " + ${prop} + " in data."`,
1580
+ expected: `${prop}`,
1573
1581
  schemaPath: `${pathContext.schema}`,
1574
1582
  })}${extra.after}}`);
1575
1583
  src.push(`}`);
@@ -1625,21 +1633,22 @@ class Compiler {
1625
1633
  handlePatternProperties(src, schema, varName, pathContext, trackingState, extra) {
1626
1634
  if (extra.before != "")
1627
1635
  src.push(`if(${extra.before} true){`);
1628
- src.push(`for (const key in ${varName}) {`);
1636
+ const key = "key" + counter++;
1637
+ src.push(`for (const ${key} in ${varName}) {`);
1629
1638
  Object.getOwnPropertyNames(schema.patternProperties).forEach((pattern) => {
1630
1639
  let pname = this.regexCache.get(pattern);
1631
1640
  if (!pname) {
1632
1641
  pname = "patternProp" + counter++;
1633
1642
  this.regexCache.set(pattern, pname);
1634
1643
  }
1635
- src.push(`if (${pname}.test(key)) {`);
1636
- addEvaluatedProperty(src, "key", trackingState);
1644
+ src.push(`if (${pname}.test(${key})) {`);
1645
+ addEvaluatedProperty(src, key, trackingState);
1637
1646
  const parent = trackingState.parentHasUnevaluatedProperties ||
1638
1647
  trackingState.hasOwnUnevaluatedProperties;
1639
1648
  const patternValidation = this.compileSchema(schema.patternProperties[pattern], {
1640
1649
  schema: `${pathContext.schema}/patternProperties/${JSON.stringify(pattern)}`,
1641
- data: `${pathContext.data}/\${key}`,
1642
- $data: `${pathContext.$data}/\${key}`,
1650
+ data: `${pathContext.data}/\${${key}}`,
1651
+ $data: `${pathContext.$data}/\${${key}}`,
1643
1652
  alt: pathContext.alt,
1644
1653
  alt2: pathContext.alt2,
1645
1654
  }, {
@@ -1648,7 +1657,7 @@ class Compiler {
1648
1657
  parentUnevaluatedPropVar: parent
1649
1658
  ? trackingState.unevaluatedPropVar
1650
1659
  : undefined,
1651
- }, `${varName}[key]`, extra);
1660
+ }, `${varName}[${key}]`, extra);
1652
1661
  src.push(patternValidation, "}");
1653
1662
  });
1654
1663
  src.push("}");
@@ -1660,12 +1669,13 @@ class Compiler {
1660
1669
  const explicitProps = Array.from(allowedProperties);
1661
1670
  if (extra.before != "")
1662
1671
  src.push(`if(${extra.before} true){`);
1663
- src.push(`for (const key in ${varName}) {`);
1664
- addEvaluatedProperty(src, "key", trackingState);
1672
+ const key = "key" + counter++;
1673
+ src.push(`for (const ${key} in ${varName}) {`);
1674
+ addEvaluatedProperty(src, key, trackingState);
1665
1675
  let checks = [];
1666
1676
  if (explicitProps.length > 0) {
1667
1677
  const allowedCheck = explicitProps
1668
- .map((key) => `key === ${JSON.stringify(key)}`)
1678
+ .map((keyItem) => `${key} === ${JSON.stringify(keyItem)}`)
1669
1679
  .join(" || ");
1670
1680
  checks.push(allowedCheck);
1671
1681
  }
@@ -1677,7 +1687,7 @@ class Compiler {
1677
1687
  pname = "patternProp" + counter++;
1678
1688
  this.regexCache.set(pattern, pname);
1679
1689
  }
1680
- return `${pname}.test(key)`;
1690
+ return `${pname}.test(${key})`;
1681
1691
  })
1682
1692
  .join(" || ");
1683
1693
  checks.push(patternCheck);
@@ -1688,17 +1698,18 @@ class Compiler {
1688
1698
  }
1689
1699
  const additionalPropValidation = this.compileSchema(schema.additionalProperties, {
1690
1700
  schema: `${pathContext.schema}/additionalProperties`,
1691
- data: `${pathContext.data}/\${key}`,
1692
- $data: `${pathContext.$data}/\${key}`,
1701
+ data: `${pathContext.data}/\${${key}}`,
1702
+ $data: `${pathContext.$data}/\${${key}}`,
1693
1703
  alt: pathContext.alt,
1694
1704
  alt2: pathContext.alt2,
1695
- }, {}, `${varName}[key]`, extra);
1705
+ }, {}, `${varName}[${key}]`, extra);
1696
1706
  src.push(additionalPropValidation, "}");
1697
1707
  if (extra.before != "")
1698
1708
  src.push(`}`);
1699
1709
  }
1700
1710
  handlePropertyConstraints(src, schema, varName, pathContext, extra) {
1701
- src.push(`const objKeys = Object.keys(${varName});`);
1711
+ const objKeys = "objKeys" + counter++;
1712
+ src.push(`const ${objKeys} = Object.keys(${varName});`);
1702
1713
  if (schema.minProperties !== undefined) {
1703
1714
  const isDataRef = this.options.$data && isDataReference(schema.minProperties);
1704
1715
  let comparisonTarget;
@@ -1710,9 +1721,9 @@ class Compiler {
1710
1721
  else {
1711
1722
  comparisonTarget = schema.minProperties;
1712
1723
  }
1713
- src.push(`if (${extra.before}objKeys.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1724
+ src.push(`if (${extra.before}${objKeys}.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1714
1725
  keyword: "minProperties",
1715
- value: "objKeys.length",
1726
+ value: `${objKeys}.length`,
1716
1727
  message: `"Object must have at least " + ${comparisonTarget} + " properties."`,
1717
1728
  expected: comparisonTarget,
1718
1729
  })}${extra.after}}`);
@@ -1731,9 +1742,9 @@ class Compiler {
1731
1742
  else {
1732
1743
  comparisonTarget = schema.maxProperties;
1733
1744
  }
1734
- src.push(`if (${extra.before}objKeys.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1745
+ src.push(`if (${extra.before}${objKeys}.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1735
1746
  keyword: "maxProperties",
1736
- value: "objKeys.length",
1747
+ value: `${objKeys}.length`,
1737
1748
  message: `"Object must have at most " + ${comparisonTarget} + " properties."`,
1738
1749
  expected: comparisonTarget,
1739
1750
  })}${extra.after}}`);
@@ -1745,14 +1756,15 @@ class Compiler {
1745
1756
  handlePropertyNames(src, schema, varName, pathContext, extra) {
1746
1757
  if (extra.before != "")
1747
1758
  src.push(`if(${extra.before} true){`);
1748
- src.push(`for (const key in ${varName}) {`);
1759
+ const key = "key" + counter++;
1760
+ src.push(`for (const ${key} in ${varName}) {`);
1749
1761
  const propertyNameValidation = this.compileSchema(schema.propertyNames, {
1750
1762
  schema: `${pathContext.schema}/propertyNames`,
1751
- data: `${pathContext.data}/\${key}`,
1752
- $data: `${pathContext.$data}/\${key}`,
1763
+ data: `${pathContext.data}/\${${key}}`,
1764
+ $data: `${pathContext.$data}/\${${key}}`,
1753
1765
  alt: pathContext.alt,
1754
1766
  alt2: pathContext.alt2,
1755
- }, {}, `key`, extra);
1767
+ }, {}, key, extra);
1756
1768
  src.push(propertyNameValidation, "}");
1757
1769
  if (extra.before != "")
1758
1770
  src.push(`}`);
@@ -1864,7 +1876,8 @@ class Compiler {
1864
1876
  const evalSet = trackingState.unevaluatedPropVar;
1865
1877
  if (extra.before != "")
1866
1878
  src.push(`if(${extra.before} true){`);
1867
- src.push(`const ${unName} = [];`, `for (const key in ${varName}) {`, `if (!${evalSet}.has(key)) {`, `${unName}.push(key);`, `}`, `}`);
1879
+ const key = "key" + counter++;
1880
+ src.push(`const ${unName} = [];`, `for (const ${key} in ${varName}) {`, `if (!${evalSet}.has(${key})) {`, `${unName}.push(${key});`, `}`, `}`);
1868
1881
  if (schema.unevaluatedProperties === false) {
1869
1882
  src.push(`if (${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
1870
1883
  keyword: "unevaluatedProperties",
@@ -1877,7 +1890,8 @@ class Compiler {
1877
1890
  }
1878
1891
  else if (schema.unevaluatedProperties === true) {
1879
1892
  if (trackingState.parentHasUnevaluatedProperties) {
1880
- src.push(`for(const key in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(key)}`);
1893
+ const key = "key" + counter++;
1894
+ src.push(`for(const ${key} in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(${key})}`);
1881
1895
  }
1882
1896
  }
1883
1897
  else {
@@ -2195,7 +2209,8 @@ class Compiler {
2195
2209
  const evalSet = trackingState.unevaluatedItemVar;
2196
2210
  if (extra.before != "")
2197
2211
  src.push(`if(${extra.before} true){`);
2198
- src.push(`const ${unName} = [];`, `for (let i = 0; i < ${varName}.length; i++) {`, `if (!${evalSet}.has(i)) {`, `${unName}.push(i);`, `}`, `}`);
2212
+ const i = "i" + counter++;
2213
+ src.push(`const ${unName} = [];`, `for (let ${i} = 0; ${i} < ${varName}.length; ${i}++) {`, `if (!${evalSet}.has(${i})) {`, `${unName}.push(${i});`, `}`, `}`);
2199
2214
  if (schema.unevaluatedItems === false) {
2200
2215
  src.push(`if (${extra.before}${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
2201
2216
  keyword: "unevaluatedItems",