@cfworker/json-schema 4.1.0 → 4.1.3
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/commonjs/deep-compare-strict.d.ts +1 -0
- package/dist/commonjs/deep-compare-strict.d.ts.map +1 -0
- package/dist/commonjs/deep-compare-strict.js +10 -4
- package/dist/commonjs/deep-compare-strict.js.map +1 -0
- package/dist/commonjs/dereference.d.ts +1 -0
- package/dist/commonjs/dereference.d.ts.map +1 -0
- package/dist/commonjs/dereference.js +4 -2
- package/dist/commonjs/dereference.js.map +1 -0
- package/dist/commonjs/format.d.ts +1 -0
- package/dist/commonjs/format.d.ts.map +1 -0
- package/dist/commonjs/format.js +26 -9
- package/dist/commonjs/format.js.map +1 -0
- package/dist/commonjs/index.d.ts +1 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +1 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +2 -1
- package/dist/commonjs/pointer.d.ts +1 -0
- package/dist/commonjs/pointer.d.ts.map +1 -0
- package/dist/commonjs/pointer.js +1 -0
- package/dist/commonjs/pointer.js.map +1 -0
- package/dist/commonjs/types.d.ts +1 -0
- package/dist/commonjs/types.d.ts.map +1 -0
- package/dist/commonjs/types.js +1 -0
- package/dist/commonjs/types.js.map +1 -0
- package/dist/commonjs/ucs2-length.d.ts +1 -0
- package/dist/commonjs/ucs2-length.d.ts.map +1 -0
- package/dist/commonjs/ucs2-length.js +1 -0
- package/dist/commonjs/ucs2-length.js.map +1 -0
- package/dist/commonjs/validate.d.ts +1 -0
- package/dist/commonjs/validate.d.ts.map +1 -0
- package/dist/commonjs/validate.js +51 -25
- package/dist/commonjs/validate.js.map +1 -0
- package/dist/commonjs/validator.d.ts +1 -0
- package/dist/commonjs/validator.d.ts.map +1 -0
- package/dist/commonjs/validator.js +1 -0
- package/dist/commonjs/validator.js.map +1 -0
- package/dist/esm/deep-compare-strict.d.ts +1 -0
- package/dist/esm/deep-compare-strict.d.ts.map +1 -0
- package/dist/esm/deep-compare-strict.js +10 -4
- package/dist/esm/deep-compare-strict.js.map +1 -0
- package/dist/esm/dereference.d.ts +1 -0
- package/dist/esm/dereference.d.ts.map +1 -0
- package/dist/esm/dereference.js +4 -2
- package/dist/esm/dereference.js.map +1 -0
- package/dist/esm/format.d.ts +1 -0
- package/dist/esm/format.d.ts.map +1 -0
- package/dist/esm/format.js +26 -9
- package/dist/esm/format.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +2 -1
- package/dist/esm/pointer.d.ts +1 -0
- package/dist/esm/pointer.d.ts.map +1 -0
- package/dist/esm/pointer.js +1 -0
- package/dist/esm/pointer.js.map +1 -0
- package/dist/esm/types.d.ts +1 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/ucs2-length.d.ts +1 -0
- package/dist/esm/ucs2-length.d.ts.map +1 -0
- package/dist/esm/ucs2-length.js +1 -0
- package/dist/esm/ucs2-length.js.map +1 -0
- package/dist/esm/validate.d.ts +1 -0
- package/dist/esm/validate.d.ts.map +1 -0
- package/dist/esm/validate.js +51 -25
- package/dist/esm/validate.js.map +1 -0
- package/dist/esm/validator.d.ts +1 -0
- package/dist/esm/validator.d.ts.map +1 -0
- package/dist/esm/validator.js +1 -0
- package/dist/esm/validator.js.map +1 -0
- package/package.json +13 -10
- package/src/deep-compare-strict.ts +9 -4
- package/src/dereference.ts +3 -2
- package/src/format.ts +27 -9
- package/src/types.d.ts +1 -7
- package/src/types.ts +1 -7
- package/src/validate.ts +67 -31
package/src/validate.ts
CHANGED
|
@@ -13,6 +13,48 @@ import { ucs2length } from './ucs2-length.js';
|
|
|
13
13
|
|
|
14
14
|
export type Evaluated = Record<string | number, boolean>;
|
|
15
15
|
|
|
16
|
+
function hasOwnProperty(instance: object, key: PropertyKey): boolean {
|
|
17
|
+
return Object.prototype.hasOwnProperty.call(instance, key);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function decimalInteger(
|
|
21
|
+
value: number
|
|
22
|
+
): [coefficient: bigint, exponent: number] {
|
|
23
|
+
const coefficientAndExponent = Math.abs(value).toString().split('e');
|
|
24
|
+
const coefficient = coefficientAndExponent[0]!;
|
|
25
|
+
const exponent = coefficientAndExponent[1] ?? '0';
|
|
26
|
+
const point = coefficient.indexOf('.');
|
|
27
|
+
const fractionLength = point === -1 ? 0 : coefficient.length - point - 1;
|
|
28
|
+
return [
|
|
29
|
+
BigInt(coefficient.replace('.', '')),
|
|
30
|
+
Number(exponent) - fractionLength
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isMultipleOf(instance: number, divisor: number): boolean {
|
|
35
|
+
if (!Number.isFinite(instance) || !Number.isFinite(divisor) || divisor <= 0) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const [instanceCoefficient, instanceExponent] = decimalInteger(instance);
|
|
40
|
+
const [divisorCoefficient, divisorExponent] = decimalInteger(divisor);
|
|
41
|
+
const exponentDifference = instanceExponent - divisorExponent;
|
|
42
|
+
|
|
43
|
+
if (exponentDifference >= 0) {
|
|
44
|
+
return (
|
|
45
|
+
(instanceCoefficient * 10n ** BigInt(exponentDifference)) %
|
|
46
|
+
divisorCoefficient ===
|
|
47
|
+
0n
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
instanceCoefficient %
|
|
53
|
+
(divisorCoefficient * 10n ** BigInt(-exponentDifference)) ===
|
|
54
|
+
0n
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
16
58
|
export function validate(
|
|
17
59
|
instance: any,
|
|
18
60
|
schema: Schema | boolean,
|
|
@@ -307,8 +349,7 @@ export function validate(
|
|
|
307
349
|
const keywordLocation = `${schemaLocation}/anyOf`;
|
|
308
350
|
const errorsLength = errors.length;
|
|
309
351
|
let anyValid = false;
|
|
310
|
-
for (
|
|
311
|
-
const subSchema = $anyOf[i];
|
|
352
|
+
for (const [i, subSchema] of $anyOf.entries()) {
|
|
312
353
|
const subEvaluated: Evaluated = Object.create(evaluated);
|
|
313
354
|
const result = validate(
|
|
314
355
|
instance,
|
|
@@ -343,8 +384,7 @@ export function validate(
|
|
|
343
384
|
const keywordLocation = `${schemaLocation}/allOf`;
|
|
344
385
|
const errorsLength = errors.length;
|
|
345
386
|
let allValid = true;
|
|
346
|
-
for (
|
|
347
|
-
const subSchema = $allOf[i];
|
|
387
|
+
for (const [i, subSchema] of $allOf.entries()) {
|
|
348
388
|
const subEvaluated: Evaluated = Object.create(evaluated);
|
|
349
389
|
const result = validate(
|
|
350
390
|
instance,
|
|
@@ -480,7 +520,7 @@ export function validate(
|
|
|
480
520
|
if (instanceType === 'object') {
|
|
481
521
|
if ($required !== undefined) {
|
|
482
522
|
for (const key of $required) {
|
|
483
|
-
if (!(key
|
|
523
|
+
if (!hasOwnProperty(instance, key)) {
|
|
484
524
|
errors.push({
|
|
485
525
|
instanceLocation,
|
|
486
526
|
keyword: 'required',
|
|
@@ -542,10 +582,10 @@ export function validate(
|
|
|
542
582
|
if ($dependentRequired !== undefined) {
|
|
543
583
|
const keywordLocation = `${schemaLocation}/dependantRequired`;
|
|
544
584
|
for (const key in $dependentRequired) {
|
|
545
|
-
if (key
|
|
585
|
+
if (hasOwnProperty(instance, key)) {
|
|
546
586
|
const required = $dependentRequired[key] as string[];
|
|
547
587
|
for (const dependantKey of required) {
|
|
548
|
-
if (!(dependantKey
|
|
588
|
+
if (!hasOwnProperty(instance, dependantKey)) {
|
|
549
589
|
errors.push({
|
|
550
590
|
instanceLocation,
|
|
551
591
|
keyword: 'dependentRequired',
|
|
@@ -559,12 +599,12 @@ export function validate(
|
|
|
559
599
|
}
|
|
560
600
|
|
|
561
601
|
if ($dependentSchemas !== undefined) {
|
|
562
|
-
for (const key
|
|
602
|
+
for (const [key, dependentSchema] of Object.entries($dependentSchemas)) {
|
|
563
603
|
const keywordLocation = `${schemaLocation}/dependentSchemas`;
|
|
564
|
-
if (key
|
|
604
|
+
if (hasOwnProperty(instance, key)) {
|
|
565
605
|
const result = validate(
|
|
566
606
|
instance,
|
|
567
|
-
|
|
607
|
+
dependentSchema,
|
|
568
608
|
draft,
|
|
569
609
|
lookup,
|
|
570
610
|
shortCircuit,
|
|
@@ -591,11 +631,11 @@ export function validate(
|
|
|
591
631
|
if ($dependencies !== undefined) {
|
|
592
632
|
const keywordLocation = `${schemaLocation}/dependencies`;
|
|
593
633
|
for (const key in $dependencies) {
|
|
594
|
-
if (key
|
|
634
|
+
if (hasOwnProperty(instance, key)) {
|
|
595
635
|
const propsOrSchema = $dependencies[key] as Schema | string[];
|
|
596
636
|
if (Array.isArray(propsOrSchema)) {
|
|
597
637
|
for (const dependantKey of propsOrSchema) {
|
|
598
|
-
if (!(dependantKey
|
|
638
|
+
if (!hasOwnProperty(instance, dependantKey)) {
|
|
599
639
|
errors.push({
|
|
600
640
|
instanceLocation,
|
|
601
641
|
keyword: 'dependencies',
|
|
@@ -637,14 +677,14 @@ export function validate(
|
|
|
637
677
|
|
|
638
678
|
if ($properties !== undefined) {
|
|
639
679
|
const keywordLocation = `${schemaLocation}/properties`;
|
|
640
|
-
for (const key
|
|
641
|
-
if (!(key
|
|
680
|
+
for (const [key, propertySchema] of Object.entries($properties)) {
|
|
681
|
+
if (!hasOwnProperty(instance, key)) {
|
|
642
682
|
continue;
|
|
643
683
|
}
|
|
644
684
|
const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`;
|
|
645
685
|
const result = validate(
|
|
646
686
|
instance[key],
|
|
647
|
-
|
|
687
|
+
propertySchema,
|
|
648
688
|
draft,
|
|
649
689
|
lookup,
|
|
650
690
|
shortCircuit,
|
|
@@ -652,8 +692,11 @@ export function validate(
|
|
|
652
692
|
subInstancePointer,
|
|
653
693
|
`${keywordLocation}/${encodePointer(key)}`
|
|
654
694
|
);
|
|
695
|
+
// Covered by "properties" whether or not it is valid, so "additionalProperties"
|
|
696
|
+
// must not apply to it. Only a valid result is an annotation for "unevaluatedProperties".
|
|
697
|
+
thisEvaluated[key] = true;
|
|
655
698
|
if (result.valid) {
|
|
656
|
-
evaluated[key] =
|
|
699
|
+
evaluated[key] = true;
|
|
657
700
|
} else {
|
|
658
701
|
stop = shortCircuit;
|
|
659
702
|
errors.push(
|
|
@@ -672,9 +715,8 @@ export function validate(
|
|
|
672
715
|
|
|
673
716
|
if (!stop && $patternProperties !== undefined) {
|
|
674
717
|
const keywordLocation = `${schemaLocation}/patternProperties`;
|
|
675
|
-
for (const pattern
|
|
718
|
+
for (const [pattern, subSchema] of Object.entries($patternProperties)) {
|
|
676
719
|
const regex = new RegExp(pattern, 'u');
|
|
677
|
-
const subSchema = $patternProperties[pattern];
|
|
678
720
|
for (const key in instance) {
|
|
679
721
|
if (!regex.test(key)) {
|
|
680
722
|
continue;
|
|
@@ -692,8 +734,10 @@ export function validate(
|
|
|
692
734
|
subInstancePointer,
|
|
693
735
|
`${keywordLocation}/${encodePointer(pattern)}`
|
|
694
736
|
);
|
|
737
|
+
// Covered by "patternProperties" whether or not it is valid; see "properties" above.
|
|
738
|
+
thisEvaluated[key] = true;
|
|
695
739
|
if (result.valid) {
|
|
696
|
-
evaluated[key] =
|
|
740
|
+
evaluated[key] = true;
|
|
697
741
|
} else {
|
|
698
742
|
stop = shortCircuit;
|
|
699
743
|
errors.push(
|
|
@@ -804,7 +848,7 @@ export function validate(
|
|
|
804
848
|
for (; i < length2; i++) {
|
|
805
849
|
const result = validate(
|
|
806
850
|
instance[i],
|
|
807
|
-
$prefixItems[i]
|
|
851
|
+
$prefixItems[i]!,
|
|
808
852
|
draft,
|
|
809
853
|
lookup,
|
|
810
854
|
shortCircuit,
|
|
@@ -836,7 +880,7 @@ export function validate(
|
|
|
836
880
|
for (; i < length2; i++) {
|
|
837
881
|
const result = validate(
|
|
838
882
|
instance[i],
|
|
839
|
-
$items[i]
|
|
883
|
+
$items[i]!,
|
|
840
884
|
draft,
|
|
841
885
|
lookup,
|
|
842
886
|
shortCircuit,
|
|
@@ -960,11 +1004,7 @@ export function validate(
|
|
|
960
1004
|
errors.length = errorsLength;
|
|
961
1005
|
}
|
|
962
1006
|
|
|
963
|
-
if (
|
|
964
|
-
$minContains === undefined &&
|
|
965
|
-
$maxContains === undefined &&
|
|
966
|
-
contained === 0
|
|
967
|
-
) {
|
|
1007
|
+
if ($minContains === undefined && contained === 0) {
|
|
968
1008
|
errors.splice(errorsLength, 0, {
|
|
969
1009
|
instanceLocation,
|
|
970
1010
|
keyword: 'contains',
|
|
@@ -1108,11 +1148,7 @@ export function validate(
|
|
|
1108
1148
|
}
|
|
1109
1149
|
}
|
|
1110
1150
|
if ($multipleOf !== undefined) {
|
|
1111
|
-
|
|
1112
|
-
if (
|
|
1113
|
-
Math.abs(0 - remainder) >= 1.1920929e-7 &&
|
|
1114
|
-
Math.abs($multipleOf - remainder) >= 1.1920929e-7
|
|
1115
|
-
) {
|
|
1151
|
+
if (!isMultipleOf(instance, $multipleOf)) {
|
|
1116
1152
|
errors.push({
|
|
1117
1153
|
instanceLocation,
|
|
1118
1154
|
keyword: 'multipleOf',
|