@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.
@@ -1187,11 +1187,15 @@ class Compiler {
1187
1187
  else {
1188
1188
  comparisonTarget = schema.multipleOf;
1189
1189
  }
1190
- src.push(`const multipleOf = ${comparisonTarget};
1191
- const quotient = ${varName} / multipleOf;
1192
- const rounded = Math.round(quotient);
1193
- const tolerance = Math.abs(quotient) * Number.EPSILON;
1194
- if (${extra.before}multipleOf === 0 || !isFinite(quotient) || Math.abs(quotient - rounded) > tolerance) {${this.buildErrorReturn(pathContext, {
1190
+ const multipleOfVar = "multipleOf" + counter++;
1191
+ const quotientVar = "quotient" + counter++;
1192
+ const roundedVar = "rounded" + counter++;
1193
+ const toleranceVar = "tolerance" + counter++;
1194
+ src.push(`const ${multipleOfVar} = ${comparisonTarget};
1195
+ const ${quotientVar} = ${varName} / ${multipleOfVar};
1196
+ const ${roundedVar} = Math.round(${quotientVar});
1197
+ const ${toleranceVar} = Math.abs(${quotientVar}) * Number.EPSILON;
1198
+ if (${extra.before}${multipleOfVar} === 0 || !isFinite(${quotientVar}) || Math.abs(${quotientVar} - ${roundedVar}) > ${toleranceVar}) {${this.buildErrorReturn(pathContext, {
1195
1199
  keyword: "multipleOf",
1196
1200
  value: varName,
1197
1201
  message: `"Value must be a multiple of " + ${comparisonTarget}`,
@@ -1294,7 +1298,8 @@ class Compiler {
1294
1298
  else {
1295
1299
  src.push(`const formatValidator = formatValidators[${formatKeyVar}].validate ?? formatValidators[${formatKeyVar}];`);
1296
1300
  }
1297
- 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, {
1301
+ const isValid = "isValid" + counter++;
1302
+ 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, {
1298
1303
  keyword: "format",
1299
1304
  value: varName,
1300
1305
  message: `"Failed to validate value against format "+${formatKeyVar}`,
@@ -1403,14 +1408,16 @@ class Compiler {
1403
1408
  const resolvedPath = resolveDataPointerAtCompileTime(pointer, pathContext.$data, this.refCall);
1404
1409
  const requiredArrayExpr = generateArrayDataRef(src, resolvedPath, extra);
1405
1410
  src.push(`if (${extra.before}${requiredArrayExpr}) {`);
1406
- src.push(`for (let i = 0; i < ${requiredArrayExpr}.length; i++) {`);
1407
- src.push(`const prop = ${requiredArrayExpr}[i];`);
1408
- addEvaluatedProperty(src, "prop", trackingState);
1409
- src.push(`if (${extra.before}${varName}[prop] === undefined) {${this.buildErrorReturn(pathContext, {
1411
+ const i = "i" + counter++;
1412
+ src.push(`for (let ${i} = 0; ${i} < ${requiredArrayExpr}.length; ${i}++) {`);
1413
+ const prop = "prop" + counter++;
1414
+ src.push(`const ${prop} = ${requiredArrayExpr}[${i}];`);
1415
+ addEvaluatedProperty(src, prop, trackingState);
1416
+ src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1410
1417
  keyword: "required",
1411
1418
  value: varName,
1412
- message: `"Missing required field: " + prop + " in data."`,
1413
- expected: "prop",
1419
+ message: `"Missing required field: " + ${prop} + " in data."`,
1420
+ expected: `${prop}`,
1414
1421
  schemaPath: `${pathContext.schema}`,
1415
1422
  })}${extra.after}}`);
1416
1423
  src.push(`}`);
@@ -1431,13 +1438,14 @@ class Compiler {
1431
1438
  if (extra.before != "")
1432
1439
  src.push(`if(${extra.before} true){`);
1433
1440
  src.push(`for (let ${iVar} = 0; ${iVar} < ${arrVar}.length; ${iVar}++) {`);
1434
- src.push(`const prop = ${arrVar}[${iVar}];`);
1435
- addEvaluatedProperty(src, "prop", trackingState);
1436
- src.push(`if (${extra.before}${varName}[prop] === undefined) {${this.buildErrorReturn(pathContext, {
1441
+ const prop = "prop" + counter++;
1442
+ src.push(`const ${prop} = ${arrVar}[${iVar}];`);
1443
+ addEvaluatedProperty(src, prop, trackingState);
1444
+ src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
1437
1445
  keyword: "required",
1438
1446
  value: varName,
1439
- message: `"Missing required field: " + prop + " in data."`,
1440
- expected: "prop",
1447
+ message: `"Missing required field: " + ${prop} + " in data."`,
1448
+ expected: `${prop}`,
1441
1449
  schemaPath: `${pathContext.schema}`,
1442
1450
  })}${extra.after}}`);
1443
1451
  src.push(`}`);
@@ -1493,21 +1501,22 @@ class Compiler {
1493
1501
  handlePatternProperties(src, schema, varName, pathContext, trackingState, extra) {
1494
1502
  if (extra.before != "")
1495
1503
  src.push(`if(${extra.before} true){`);
1496
- src.push(`for (const key in ${varName}) {`);
1504
+ const key = "key" + counter++;
1505
+ src.push(`for (const ${key} in ${varName}) {`);
1497
1506
  Object.getOwnPropertyNames(schema.patternProperties).forEach((pattern) => {
1498
1507
  let pname = this.regexCache.get(pattern);
1499
1508
  if (!pname) {
1500
1509
  pname = "patternProp" + counter++;
1501
1510
  this.regexCache.set(pattern, pname);
1502
1511
  }
1503
- src.push(`if (${pname}.test(key)) {`);
1504
- addEvaluatedProperty(src, "key", trackingState);
1512
+ src.push(`if (${pname}.test(${key})) {`);
1513
+ addEvaluatedProperty(src, key, trackingState);
1505
1514
  const parent = trackingState.parentHasUnevaluatedProperties ||
1506
1515
  trackingState.hasOwnUnevaluatedProperties;
1507
1516
  const patternValidation = this.compileSchema(schema.patternProperties[pattern], {
1508
1517
  schema: `${pathContext.schema}/patternProperties/${JSON.stringify(pattern)}`,
1509
- data: `${pathContext.data}/\${key}`,
1510
- $data: `${pathContext.$data}/\${key}`,
1518
+ data: `${pathContext.data}/\${${key}}`,
1519
+ $data: `${pathContext.$data}/\${${key}}`,
1511
1520
  alt: pathContext.alt,
1512
1521
  alt2: pathContext.alt2,
1513
1522
  }, {
@@ -1516,7 +1525,7 @@ class Compiler {
1516
1525
  parentUnevaluatedPropVar: parent
1517
1526
  ? trackingState.unevaluatedPropVar
1518
1527
  : undefined,
1519
- }, `${varName}[key]`, extra);
1528
+ }, `${varName}[${key}]`, extra);
1520
1529
  src.push(patternValidation, "}");
1521
1530
  });
1522
1531
  src.push("}");
@@ -1528,12 +1537,13 @@ class Compiler {
1528
1537
  const explicitProps = Array.from(allowedProperties);
1529
1538
  if (extra.before != "")
1530
1539
  src.push(`if(${extra.before} true){`);
1531
- src.push(`for (const key in ${varName}) {`);
1532
- addEvaluatedProperty(src, "key", trackingState);
1540
+ const key = "key" + counter++;
1541
+ src.push(`for (const ${key} in ${varName}) {`);
1542
+ addEvaluatedProperty(src, key, trackingState);
1533
1543
  let checks = [];
1534
1544
  if (explicitProps.length > 0) {
1535
1545
  const allowedCheck = explicitProps
1536
- .map((key) => `key === ${JSON.stringify(key)}`)
1546
+ .map((keyItem) => `${key} === ${JSON.stringify(keyItem)}`)
1537
1547
  .join(" || ");
1538
1548
  checks.push(allowedCheck);
1539
1549
  }
@@ -1545,7 +1555,7 @@ class Compiler {
1545
1555
  pname = "patternProp" + counter++;
1546
1556
  this.regexCache.set(pattern, pname);
1547
1557
  }
1548
- return `${pname}.test(key)`;
1558
+ return `${pname}.test(${key})`;
1549
1559
  })
1550
1560
  .join(" || ");
1551
1561
  checks.push(patternCheck);
@@ -1556,17 +1566,18 @@ class Compiler {
1556
1566
  }
1557
1567
  const additionalPropValidation = this.compileSchema(schema.additionalProperties, {
1558
1568
  schema: `${pathContext.schema}/additionalProperties`,
1559
- data: `${pathContext.data}/\${key}`,
1560
- $data: `${pathContext.$data}/\${key}`,
1569
+ data: `${pathContext.data}/\${${key}}`,
1570
+ $data: `${pathContext.$data}/\${${key}}`,
1561
1571
  alt: pathContext.alt,
1562
1572
  alt2: pathContext.alt2,
1563
- }, {}, `${varName}[key]`, extra);
1573
+ }, {}, `${varName}[${key}]`, extra);
1564
1574
  src.push(additionalPropValidation, "}");
1565
1575
  if (extra.before != "")
1566
1576
  src.push(`}`);
1567
1577
  }
1568
1578
  handlePropertyConstraints(src, schema, varName, pathContext, extra) {
1569
- src.push(`const objKeys = Object.keys(${varName});`);
1579
+ const objKeys = "objKeys" + counter++;
1580
+ src.push(`const ${objKeys} = Object.keys(${varName});`);
1570
1581
  if (schema.minProperties !== undefined) {
1571
1582
  const isDataRef = this.options.$data && isDataReference(schema.minProperties);
1572
1583
  let comparisonTarget;
@@ -1578,9 +1589,9 @@ class Compiler {
1578
1589
  else {
1579
1590
  comparisonTarget = schema.minProperties;
1580
1591
  }
1581
- src.push(`if (${extra.before}objKeys.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1592
+ src.push(`if (${extra.before}${objKeys}.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1582
1593
  keyword: "minProperties",
1583
- value: "objKeys.length",
1594
+ value: `${objKeys}.length`,
1584
1595
  message: `"Object must have at least " + ${comparisonTarget} + " properties."`,
1585
1596
  expected: comparisonTarget,
1586
1597
  })}${extra.after}}`);
@@ -1599,9 +1610,9 @@ class Compiler {
1599
1610
  else {
1600
1611
  comparisonTarget = schema.maxProperties;
1601
1612
  }
1602
- src.push(`if (${extra.before}objKeys.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1613
+ src.push(`if (${extra.before}${objKeys}.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
1603
1614
  keyword: "maxProperties",
1604
- value: "objKeys.length",
1615
+ value: `${objKeys}.length`,
1605
1616
  message: `"Object must have at most " + ${comparisonTarget} + " properties."`,
1606
1617
  expected: comparisonTarget,
1607
1618
  })}${extra.after}}`);
@@ -1613,14 +1624,15 @@ class Compiler {
1613
1624
  handlePropertyNames(src, schema, varName, pathContext, extra) {
1614
1625
  if (extra.before != "")
1615
1626
  src.push(`if(${extra.before} true){`);
1616
- src.push(`for (const key in ${varName}) {`);
1627
+ const key = "key" + counter++;
1628
+ src.push(`for (const ${key} in ${varName}) {`);
1617
1629
  const propertyNameValidation = this.compileSchema(schema.propertyNames, {
1618
1630
  schema: `${pathContext.schema}/propertyNames`,
1619
- data: `${pathContext.data}/\${key}`,
1620
- $data: `${pathContext.$data}/\${key}`,
1631
+ data: `${pathContext.data}/\${${key}}`,
1632
+ $data: `${pathContext.$data}/\${${key}}`,
1621
1633
  alt: pathContext.alt,
1622
1634
  alt2: pathContext.alt2,
1623
- }, {}, `key`, extra);
1635
+ }, {}, key, extra);
1624
1636
  src.push(propertyNameValidation, "}");
1625
1637
  if (extra.before != "")
1626
1638
  src.push(`}`);
@@ -1732,7 +1744,8 @@ class Compiler {
1732
1744
  const evalSet = trackingState.unevaluatedPropVar;
1733
1745
  if (extra.before != "")
1734
1746
  src.push(`if(${extra.before} true){`);
1735
- src.push(`const ${unName} = [];`, `for (const key in ${varName}) {`, `if (!${evalSet}.has(key)) {`, `${unName}.push(key);`, `}`, `}`);
1747
+ const key = "key" + counter++;
1748
+ src.push(`const ${unName} = [];`, `for (const ${key} in ${varName}) {`, `if (!${evalSet}.has(${key})) {`, `${unName}.push(${key});`, `}`, `}`);
1736
1749
  if (schema.unevaluatedProperties === false) {
1737
1750
  src.push(`if (${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
1738
1751
  keyword: "unevaluatedProperties",
@@ -1745,7 +1758,8 @@ class Compiler {
1745
1758
  }
1746
1759
  else if (schema.unevaluatedProperties === true) {
1747
1760
  if (trackingState.parentHasUnevaluatedProperties) {
1748
- src.push(`for(const key in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(key)}`);
1761
+ const key = "key" + counter++;
1762
+ src.push(`for(const ${key} in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(${key})}`);
1749
1763
  }
1750
1764
  }
1751
1765
  else {
@@ -2063,7 +2077,8 @@ class Compiler {
2063
2077
  const evalSet = trackingState.unevaluatedItemVar;
2064
2078
  if (extra.before != "")
2065
2079
  src.push(`if(${extra.before} true){`);
2066
- src.push(`const ${unName} = [];`, `for (let i = 0; i < ${varName}.length; i++) {`, `if (!${evalSet}.has(i)) {`, `${unName}.push(i);`, `}`, `}`);
2080
+ const i = "i" + counter++;
2081
+ src.push(`const ${unName} = [];`, `for (let ${i} = 0; ${i} < ${varName}.length; ${i}++) {`, `if (!${evalSet}.has(${i})) {`, `${unName}.push(${i});`, `}`, `}`);
2067
2082
  if (schema.unevaluatedItems === false) {
2068
2083
  src.push(`if (${extra.before}${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
2069
2084
  keyword: "unevaluatedItems",