@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/compileSchema.js +57 -42
- package/dist/compileSchema.js.map +1 -1
- package/dist/index.cjs.js +57 -42
- package/dist/index.esm.js +57 -42
- package/dist/validator.umd.js +57 -42
- package/package.json +9 -3
package/dist/validator.umd.js
CHANGED
|
@@ -1329,11 +1329,15 @@
|
|
|
1329
1329
|
else {
|
|
1330
1330
|
comparisonTarget = schema.multipleOf;
|
|
1331
1331
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1332
|
+
const multipleOfVar = "multipleOf" + counter++;
|
|
1333
|
+
const quotientVar = "quotient" + counter++;
|
|
1334
|
+
const roundedVar = "rounded" + counter++;
|
|
1335
|
+
const toleranceVar = "tolerance" + counter++;
|
|
1336
|
+
src.push(`const ${multipleOfVar} = ${comparisonTarget};
|
|
1337
|
+
const ${quotientVar} = ${varName} / ${multipleOfVar};
|
|
1338
|
+
const ${roundedVar} = Math.round(${quotientVar});
|
|
1339
|
+
const ${toleranceVar} = Math.abs(${quotientVar}) * Number.EPSILON;
|
|
1340
|
+
if (${extra.before}${multipleOfVar} === 0 || !isFinite(${quotientVar}) || Math.abs(${quotientVar} - ${roundedVar}) > ${toleranceVar}) {${this.buildErrorReturn(pathContext, {
|
|
1337
1341
|
keyword: "multipleOf",
|
|
1338
1342
|
value: varName,
|
|
1339
1343
|
message: `"Value must be a multiple of " + ${comparisonTarget}`,
|
|
@@ -1437,7 +1441,8 @@
|
|
|
1437
1441
|
else {
|
|
1438
1442
|
src.push(`const formatValidator = formatValidators[${formatKeyVar}].validate ?? formatValidators[${formatKeyVar}];`);
|
|
1439
1443
|
}
|
|
1440
|
-
|
|
1444
|
+
const isValid = "isValid" + counter++;
|
|
1445
|
+
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, {
|
|
1441
1446
|
keyword: "format",
|
|
1442
1447
|
value: varName,
|
|
1443
1448
|
message: `"Failed to validate value against format "+${formatKeyVar}`,
|
|
@@ -1546,14 +1551,16 @@
|
|
|
1546
1551
|
const resolvedPath = resolveDataPointerAtCompileTime(pointer, pathContext.$data, this.refCall);
|
|
1547
1552
|
const requiredArrayExpr = generateArrayDataRef(src, resolvedPath, extra);
|
|
1548
1553
|
src.push(`if (${extra.before}${requiredArrayExpr}) {`);
|
|
1549
|
-
|
|
1550
|
-
src.push(`
|
|
1551
|
-
|
|
1552
|
-
src.push(`
|
|
1554
|
+
const i = "i" + counter++;
|
|
1555
|
+
src.push(`for (let ${i} = 0; ${i} < ${requiredArrayExpr}.length; ${i}++) {`);
|
|
1556
|
+
const prop = "prop" + counter++;
|
|
1557
|
+
src.push(`const ${prop} = ${requiredArrayExpr}[${i}];`);
|
|
1558
|
+
addEvaluatedProperty(src, prop, trackingState);
|
|
1559
|
+
src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
|
|
1553
1560
|
keyword: "required",
|
|
1554
1561
|
value: varName,
|
|
1555
|
-
message: `"Missing required field: " + prop + " in data."`,
|
|
1556
|
-
expected:
|
|
1562
|
+
message: `"Missing required field: " + ${prop} + " in data."`,
|
|
1563
|
+
expected: `${prop}`,
|
|
1557
1564
|
schemaPath: `${pathContext.schema}`,
|
|
1558
1565
|
})}${extra.after}}`);
|
|
1559
1566
|
src.push(`}`);
|
|
@@ -1574,13 +1581,14 @@
|
|
|
1574
1581
|
if (extra.before != "")
|
|
1575
1582
|
src.push(`if(${extra.before} true){`);
|
|
1576
1583
|
src.push(`for (let ${iVar} = 0; ${iVar} < ${arrVar}.length; ${iVar}++) {`);
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
src
|
|
1584
|
+
const prop = "prop" + counter++;
|
|
1585
|
+
src.push(`const ${prop} = ${arrVar}[${iVar}];`);
|
|
1586
|
+
addEvaluatedProperty(src, prop, trackingState);
|
|
1587
|
+
src.push(`if (${extra.before}${varName}[${prop}] === undefined) {${this.buildErrorReturn(pathContext, {
|
|
1580
1588
|
keyword: "required",
|
|
1581
1589
|
value: varName,
|
|
1582
|
-
message: `"Missing required field: " + prop + " in data."`,
|
|
1583
|
-
expected:
|
|
1590
|
+
message: `"Missing required field: " + ${prop} + " in data."`,
|
|
1591
|
+
expected: `${prop}`,
|
|
1584
1592
|
schemaPath: `${pathContext.schema}`,
|
|
1585
1593
|
})}${extra.after}}`);
|
|
1586
1594
|
src.push(`}`);
|
|
@@ -1636,21 +1644,22 @@
|
|
|
1636
1644
|
handlePatternProperties(src, schema, varName, pathContext, trackingState, extra) {
|
|
1637
1645
|
if (extra.before != "")
|
|
1638
1646
|
src.push(`if(${extra.before} true){`);
|
|
1639
|
-
|
|
1647
|
+
const key = "key" + counter++;
|
|
1648
|
+
src.push(`for (const ${key} in ${varName}) {`);
|
|
1640
1649
|
Object.getOwnPropertyNames(schema.patternProperties).forEach((pattern) => {
|
|
1641
1650
|
let pname = this.regexCache.get(pattern);
|
|
1642
1651
|
if (!pname) {
|
|
1643
1652
|
pname = "patternProp" + counter++;
|
|
1644
1653
|
this.regexCache.set(pattern, pname);
|
|
1645
1654
|
}
|
|
1646
|
-
src.push(`if (${pname}.test(key)) {`);
|
|
1647
|
-
addEvaluatedProperty(src,
|
|
1655
|
+
src.push(`if (${pname}.test(${key})) {`);
|
|
1656
|
+
addEvaluatedProperty(src, key, trackingState);
|
|
1648
1657
|
const parent = trackingState.parentHasUnevaluatedProperties ||
|
|
1649
1658
|
trackingState.hasOwnUnevaluatedProperties;
|
|
1650
1659
|
const patternValidation = this.compileSchema(schema.patternProperties[pattern], {
|
|
1651
1660
|
schema: `${pathContext.schema}/patternProperties/${JSON.stringify(pattern)}`,
|
|
1652
|
-
data: `${pathContext.data}/\${key}`,
|
|
1653
|
-
$data: `${pathContext.$data}/\${key}`,
|
|
1661
|
+
data: `${pathContext.data}/\${${key}}`,
|
|
1662
|
+
$data: `${pathContext.$data}/\${${key}}`,
|
|
1654
1663
|
alt: pathContext.alt,
|
|
1655
1664
|
alt2: pathContext.alt2,
|
|
1656
1665
|
}, {
|
|
@@ -1659,7 +1668,7 @@
|
|
|
1659
1668
|
parentUnevaluatedPropVar: parent
|
|
1660
1669
|
? trackingState.unevaluatedPropVar
|
|
1661
1670
|
: undefined,
|
|
1662
|
-
}, `${varName}[key]`, extra);
|
|
1671
|
+
}, `${varName}[${key}]`, extra);
|
|
1663
1672
|
src.push(patternValidation, "}");
|
|
1664
1673
|
});
|
|
1665
1674
|
src.push("}");
|
|
@@ -1671,12 +1680,13 @@
|
|
|
1671
1680
|
const explicitProps = Array.from(allowedProperties);
|
|
1672
1681
|
if (extra.before != "")
|
|
1673
1682
|
src.push(`if(${extra.before} true){`);
|
|
1674
|
-
|
|
1675
|
-
|
|
1683
|
+
const key = "key" + counter++;
|
|
1684
|
+
src.push(`for (const ${key} in ${varName}) {`);
|
|
1685
|
+
addEvaluatedProperty(src, key, trackingState);
|
|
1676
1686
|
let checks = [];
|
|
1677
1687
|
if (explicitProps.length > 0) {
|
|
1678
1688
|
const allowedCheck = explicitProps
|
|
1679
|
-
.map((
|
|
1689
|
+
.map((keyItem) => `${key} === ${JSON.stringify(keyItem)}`)
|
|
1680
1690
|
.join(" || ");
|
|
1681
1691
|
checks.push(allowedCheck);
|
|
1682
1692
|
}
|
|
@@ -1688,7 +1698,7 @@
|
|
|
1688
1698
|
pname = "patternProp" + counter++;
|
|
1689
1699
|
this.regexCache.set(pattern, pname);
|
|
1690
1700
|
}
|
|
1691
|
-
return `${pname}.test(key)`;
|
|
1701
|
+
return `${pname}.test(${key})`;
|
|
1692
1702
|
})
|
|
1693
1703
|
.join(" || ");
|
|
1694
1704
|
checks.push(patternCheck);
|
|
@@ -1699,17 +1709,18 @@
|
|
|
1699
1709
|
}
|
|
1700
1710
|
const additionalPropValidation = this.compileSchema(schema.additionalProperties, {
|
|
1701
1711
|
schema: `${pathContext.schema}/additionalProperties`,
|
|
1702
|
-
data: `${pathContext.data}/\${key}`,
|
|
1703
|
-
$data: `${pathContext.$data}/\${key}`,
|
|
1712
|
+
data: `${pathContext.data}/\${${key}}`,
|
|
1713
|
+
$data: `${pathContext.$data}/\${${key}}`,
|
|
1704
1714
|
alt: pathContext.alt,
|
|
1705
1715
|
alt2: pathContext.alt2,
|
|
1706
|
-
}, {}, `${varName}[key]`, extra);
|
|
1716
|
+
}, {}, `${varName}[${key}]`, extra);
|
|
1707
1717
|
src.push(additionalPropValidation, "}");
|
|
1708
1718
|
if (extra.before != "")
|
|
1709
1719
|
src.push(`}`);
|
|
1710
1720
|
}
|
|
1711
1721
|
handlePropertyConstraints(src, schema, varName, pathContext, extra) {
|
|
1712
|
-
|
|
1722
|
+
const objKeys = "objKeys" + counter++;
|
|
1723
|
+
src.push(`const ${objKeys} = Object.keys(${varName});`);
|
|
1713
1724
|
if (schema.minProperties !== undefined) {
|
|
1714
1725
|
const isDataRef = this.options.$data && isDataReference(schema.minProperties);
|
|
1715
1726
|
let comparisonTarget;
|
|
@@ -1721,9 +1732,9 @@
|
|
|
1721
1732
|
else {
|
|
1722
1733
|
comparisonTarget = schema.minProperties;
|
|
1723
1734
|
}
|
|
1724
|
-
src.push(`if (${extra.before}objKeys.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
|
|
1735
|
+
src.push(`if (${extra.before}${objKeys}.length < ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
|
|
1725
1736
|
keyword: "minProperties",
|
|
1726
|
-
value:
|
|
1737
|
+
value: `${objKeys}.length`,
|
|
1727
1738
|
message: `"Object must have at least " + ${comparisonTarget} + " properties."`,
|
|
1728
1739
|
expected: comparisonTarget,
|
|
1729
1740
|
})}${extra.after}}`);
|
|
@@ -1742,9 +1753,9 @@
|
|
|
1742
1753
|
else {
|
|
1743
1754
|
comparisonTarget = schema.maxProperties;
|
|
1744
1755
|
}
|
|
1745
|
-
src.push(`if (${extra.before}objKeys.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
|
|
1756
|
+
src.push(`if (${extra.before}${objKeys}.length > ${comparisonTarget}) {${this.buildErrorReturn(pathContext, {
|
|
1746
1757
|
keyword: "maxProperties",
|
|
1747
|
-
value:
|
|
1758
|
+
value: `${objKeys}.length`,
|
|
1748
1759
|
message: `"Object must have at most " + ${comparisonTarget} + " properties."`,
|
|
1749
1760
|
expected: comparisonTarget,
|
|
1750
1761
|
})}${extra.after}}`);
|
|
@@ -1756,14 +1767,15 @@
|
|
|
1756
1767
|
handlePropertyNames(src, schema, varName, pathContext, extra) {
|
|
1757
1768
|
if (extra.before != "")
|
|
1758
1769
|
src.push(`if(${extra.before} true){`);
|
|
1759
|
-
|
|
1770
|
+
const key = "key" + counter++;
|
|
1771
|
+
src.push(`for (const ${key} in ${varName}) {`);
|
|
1760
1772
|
const propertyNameValidation = this.compileSchema(schema.propertyNames, {
|
|
1761
1773
|
schema: `${pathContext.schema}/propertyNames`,
|
|
1762
|
-
data: `${pathContext.data}/\${key}`,
|
|
1763
|
-
$data: `${pathContext.$data}/\${key}`,
|
|
1774
|
+
data: `${pathContext.data}/\${${key}}`,
|
|
1775
|
+
$data: `${pathContext.$data}/\${${key}}`,
|
|
1764
1776
|
alt: pathContext.alt,
|
|
1765
1777
|
alt2: pathContext.alt2,
|
|
1766
|
-
}, {},
|
|
1778
|
+
}, {}, key, extra);
|
|
1767
1779
|
src.push(propertyNameValidation, "}");
|
|
1768
1780
|
if (extra.before != "")
|
|
1769
1781
|
src.push(`}`);
|
|
@@ -1876,7 +1888,8 @@
|
|
|
1876
1888
|
const evalSet = trackingState.unevaluatedPropVar;
|
|
1877
1889
|
if (extra.before != "")
|
|
1878
1890
|
src.push(`if(${extra.before} true){`);
|
|
1879
|
-
|
|
1891
|
+
const key = "key" + counter++;
|
|
1892
|
+
src.push(`const ${unName} = [];`, `for (const ${key} in ${varName}) {`, `if (!${evalSet}.has(${key})) {`, `${unName}.push(${key});`, `}`, `}`);
|
|
1880
1893
|
if (schema.unevaluatedProperties === false) {
|
|
1881
1894
|
src.push(`if (${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
|
|
1882
1895
|
keyword: "unevaluatedProperties",
|
|
@@ -1889,7 +1902,8 @@
|
|
|
1889
1902
|
}
|
|
1890
1903
|
else if (schema.unevaluatedProperties === true) {
|
|
1891
1904
|
if (trackingState.parentHasUnevaluatedProperties) {
|
|
1892
|
-
|
|
1905
|
+
const key = "key" + counter++;
|
|
1906
|
+
src.push(`for(const ${key} in ${varName}){${trackingState.parentUnevaluatedPropVar}.add(${key})}`);
|
|
1893
1907
|
}
|
|
1894
1908
|
}
|
|
1895
1909
|
else {
|
|
@@ -2207,7 +2221,8 @@
|
|
|
2207
2221
|
const evalSet = trackingState.unevaluatedItemVar;
|
|
2208
2222
|
if (extra.before != "")
|
|
2209
2223
|
src.push(`if(${extra.before} true){`);
|
|
2210
|
-
|
|
2224
|
+
const i = "i" + counter++;
|
|
2225
|
+
src.push(`const ${unName} = [];`, `for (let ${i} = 0; ${i} < ${varName}.length; ${i}++) {`, `if (!${evalSet}.has(${i})) {`, `${unName}.push(${i});`, `}`, `}`);
|
|
2211
2226
|
if (schema.unevaluatedItems === false) {
|
|
2212
2227
|
src.push(`if (${extra.before}${unName}.length > 0) {${this.buildErrorReturn(pathContext, {
|
|
2213
2228
|
keyword: "unevaluatedItems",
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetio/validator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "The fastest JSON Schema validator for JavaScript and TypeScript. Supports Draft 06, 07, 2019-09, and 2020-12.",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"browser": "dist/validator.umd.js",
|
|
9
|
+
"unpkg": "dist/validator.umd.js",
|
|
10
|
+
"jsdelivr": "dist/validator.umd.js",
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
10
13
|
"import": "./dist/index.esm.js",
|
|
11
14
|
"require": "./dist/index.cjs.js",
|
|
12
|
-
"types": "./dist/index.d.ts"
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"browser": "./dist/validator.umd.js"
|
|
13
17
|
}
|
|
14
18
|
},
|
|
15
19
|
"files": [
|
|
@@ -60,9 +64,11 @@
|
|
|
60
64
|
"@types/node": "^18.19.127",
|
|
61
65
|
"@typescript-eslint/eslint-plugin": "^5.58.0",
|
|
62
66
|
"@typescript-eslint/parser": "^5.58.0",
|
|
67
|
+
"ajv": "^8.17.1",
|
|
68
|
+
"ajv-formats": "^3.0.1",
|
|
63
69
|
"eslint": "^8.38.0",
|
|
64
70
|
"rollup": "^2.79.2",
|
|
65
|
-
"rollup-plugin-filesize": "^
|
|
71
|
+
"rollup-plugin-filesize": "^7.0.0",
|
|
66
72
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
67
73
|
"rollup-plugin-visualizer": "^5.14.0",
|
|
68
74
|
"ts-node": "^10.9.2",
|