@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.
Files changed (81) hide show
  1. package/dist/commonjs/deep-compare-strict.d.ts +1 -0
  2. package/dist/commonjs/deep-compare-strict.d.ts.map +1 -0
  3. package/dist/commonjs/deep-compare-strict.js +10 -4
  4. package/dist/commonjs/deep-compare-strict.js.map +1 -0
  5. package/dist/commonjs/dereference.d.ts +1 -0
  6. package/dist/commonjs/dereference.d.ts.map +1 -0
  7. package/dist/commonjs/dereference.js +4 -2
  8. package/dist/commonjs/dereference.js.map +1 -0
  9. package/dist/commonjs/format.d.ts +1 -0
  10. package/dist/commonjs/format.d.ts.map +1 -0
  11. package/dist/commonjs/format.js +26 -9
  12. package/dist/commonjs/format.js.map +1 -0
  13. package/dist/commonjs/index.d.ts +1 -0
  14. package/dist/commonjs/index.d.ts.map +1 -0
  15. package/dist/commonjs/index.js +1 -0
  16. package/dist/commonjs/index.js.map +1 -0
  17. package/dist/commonjs/package.json +2 -1
  18. package/dist/commonjs/pointer.d.ts +1 -0
  19. package/dist/commonjs/pointer.d.ts.map +1 -0
  20. package/dist/commonjs/pointer.js +1 -0
  21. package/dist/commonjs/pointer.js.map +1 -0
  22. package/dist/commonjs/types.d.ts +1 -0
  23. package/dist/commonjs/types.d.ts.map +1 -0
  24. package/dist/commonjs/types.js +1 -0
  25. package/dist/commonjs/types.js.map +1 -0
  26. package/dist/commonjs/ucs2-length.d.ts +1 -0
  27. package/dist/commonjs/ucs2-length.d.ts.map +1 -0
  28. package/dist/commonjs/ucs2-length.js +1 -0
  29. package/dist/commonjs/ucs2-length.js.map +1 -0
  30. package/dist/commonjs/validate.d.ts +1 -0
  31. package/dist/commonjs/validate.d.ts.map +1 -0
  32. package/dist/commonjs/validate.js +51 -25
  33. package/dist/commonjs/validate.js.map +1 -0
  34. package/dist/commonjs/validator.d.ts +1 -0
  35. package/dist/commonjs/validator.d.ts.map +1 -0
  36. package/dist/commonjs/validator.js +1 -0
  37. package/dist/commonjs/validator.js.map +1 -0
  38. package/dist/esm/deep-compare-strict.d.ts +1 -0
  39. package/dist/esm/deep-compare-strict.d.ts.map +1 -0
  40. package/dist/esm/deep-compare-strict.js +10 -4
  41. package/dist/esm/deep-compare-strict.js.map +1 -0
  42. package/dist/esm/dereference.d.ts +1 -0
  43. package/dist/esm/dereference.d.ts.map +1 -0
  44. package/dist/esm/dereference.js +4 -2
  45. package/dist/esm/dereference.js.map +1 -0
  46. package/dist/esm/format.d.ts +1 -0
  47. package/dist/esm/format.d.ts.map +1 -0
  48. package/dist/esm/format.js +26 -9
  49. package/dist/esm/format.js.map +1 -0
  50. package/dist/esm/index.d.ts +1 -0
  51. package/dist/esm/index.d.ts.map +1 -0
  52. package/dist/esm/index.js +1 -0
  53. package/dist/esm/index.js.map +1 -0
  54. package/dist/esm/package.json +2 -1
  55. package/dist/esm/pointer.d.ts +1 -0
  56. package/dist/esm/pointer.d.ts.map +1 -0
  57. package/dist/esm/pointer.js +1 -0
  58. package/dist/esm/pointer.js.map +1 -0
  59. package/dist/esm/types.d.ts +1 -0
  60. package/dist/esm/types.d.ts.map +1 -0
  61. package/dist/esm/types.js +1 -0
  62. package/dist/esm/types.js.map +1 -0
  63. package/dist/esm/ucs2-length.d.ts +1 -0
  64. package/dist/esm/ucs2-length.d.ts.map +1 -0
  65. package/dist/esm/ucs2-length.js +1 -0
  66. package/dist/esm/ucs2-length.js.map +1 -0
  67. package/dist/esm/validate.d.ts +1 -0
  68. package/dist/esm/validate.d.ts.map +1 -0
  69. package/dist/esm/validate.js +51 -25
  70. package/dist/esm/validate.js.map +1 -0
  71. package/dist/esm/validator.d.ts +1 -0
  72. package/dist/esm/validator.d.ts.map +1 -0
  73. package/dist/esm/validator.js +1 -0
  74. package/dist/esm/validator.js.map +1 -0
  75. package/package.json +13 -10
  76. package/src/deep-compare-strict.ts +9 -4
  77. package/src/dereference.ts +3 -2
  78. package/src/format.ts +27 -9
  79. package/src/types.d.ts +1 -7
  80. package/src/types.ts +1 -7
  81. 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 (let i = 0; i < $anyOf.length; i++) {
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 (let i = 0; i < $allOf.length; i++) {
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 in instance)) {
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 in instance) {
585
+ if (hasOwnProperty(instance, key)) {
546
586
  const required = $dependentRequired[key] as string[];
547
587
  for (const dependantKey of required) {
548
- if (!(dependantKey in instance)) {
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 in $dependentSchemas) {
602
+ for (const [key, dependentSchema] of Object.entries($dependentSchemas)) {
563
603
  const keywordLocation = `${schemaLocation}/dependentSchemas`;
564
- if (key in instance) {
604
+ if (hasOwnProperty(instance, key)) {
565
605
  const result = validate(
566
606
  instance,
567
- $dependentSchemas[key],
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 in instance) {
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 in instance)) {
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 in $properties) {
641
- if (!(key in instance)) {
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
- $properties[key],
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] = thisEvaluated[key] = true;
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 in $patternProperties) {
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] = thisEvaluated[key] = true;
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
- const remainder = instance % $multipleOf;
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',