z-schema 12.2.0 → 12.3.1

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 (110) hide show
  1. package/README.md +2 -2
  2. package/bin/z-schema +1 -1
  3. package/cjs/{index.js → index.cjs} +696 -687
  4. package/cjs/{index.d.ts → index.d.cts} +47 -26
  5. package/dist/{errors.d.mts → errors.d.ts} +2 -2
  6. package/dist/{errors.mjs → errors.js} +1 -2
  7. package/dist/{format-validators.mjs → format-validators.js} +43 -36
  8. package/dist/{index.d.mts → index.d.ts} +9 -9
  9. package/dist/{index.mjs → index.js} +3 -3
  10. package/dist/{json-schema-versions.d.mts → json-schema-versions.d.ts} +34 -3
  11. package/dist/{json-schema.d.mts → json-schema.d.ts} +7 -7
  12. package/dist/{json-schema.mjs → json-schema.js} +7 -12
  13. package/dist/{json-validation.mjs → json-validation.js} +143 -127
  14. package/dist/{report.d.mts → report.d.ts} +7 -8
  15. package/dist/{report.mjs → report.js} +28 -31
  16. package/dist/{schema-cache.d.mts → schema-cache.d.ts} +4 -4
  17. package/dist/{schema-cache.mjs → schema-cache.js} +10 -11
  18. package/dist/{schema-compiler.d.mts → schema-compiler.d.ts} +4 -4
  19. package/dist/{schema-compiler.mjs → schema-compiler.js} +95 -77
  20. package/dist/{schema-validator.d.mts → schema-validator.d.ts} +5 -5
  21. package/dist/{schema-validator.mjs → schema-validator.js} +138 -166
  22. package/dist/utils/{array.mjs → array.js} +4 -3
  23. package/dist/utils/{base64.mjs → base64.js} +3 -2
  24. package/dist/utils/{clone.mjs → clone.js} +18 -20
  25. package/dist/utils/{hostname.mjs → hostname.js} +19 -22
  26. package/dist/utils/{json.mjs → json.js} +11 -7
  27. package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
  28. package/dist/utils/{time.mjs → time.js} +5 -5
  29. package/dist/utils/unicode.js +22 -0
  30. package/dist/utils/{what-is.mjs → what-is.js} +1 -2
  31. package/dist/validation/{array.mjs → array.js} +18 -20
  32. package/dist/validation/{combinators.mjs → combinators.js} +16 -16
  33. package/dist/validation/{numeric.mjs → numeric.js} +11 -11
  34. package/dist/validation/{object.mjs → object.js} +35 -34
  35. package/dist/validation/{ref.mjs → ref.js} +4 -4
  36. package/dist/validation/{shared.mjs → shared.js} +12 -11
  37. package/dist/validation/{string.mjs → string.js} +32 -32
  38. package/dist/validation/type.js +34 -0
  39. package/dist/{z-schema-base.d.mts → z-schema-base.d.ts} +11 -12
  40. package/dist/{z-schema-base.mjs → z-schema-base.js} +45 -40
  41. package/dist/{z-schema-options.d.mts → z-schema-options.d.ts} +3 -3
  42. package/dist/{z-schema-options.mjs → z-schema-options.js} +4 -4
  43. package/dist/{z-schema-reader.d.mts → z-schema-reader.d.ts} +1 -1
  44. package/dist/{z-schema-versions.mjs → z-schema-versions.js} +21 -21
  45. package/dist/{z-schema.d.mts → z-schema.d.ts} +5 -13
  46. package/dist/{z-schema.mjs → z-schema.js} +37 -47
  47. package/package.json +22 -23
  48. package/src/errors.ts +1 -2
  49. package/src/format-validators.ts +139 -59
  50. package/src/json-schema-versions.ts +56 -2
  51. package/src/json-schema.ts +10 -9
  52. package/src/json-validation.ts +189 -146
  53. package/src/report.ts +37 -49
  54. package/src/schema-cache.ts +13 -13
  55. package/src/schema-compiler.ts +170 -117
  56. package/src/schema-validator.ts +239 -238
  57. package/src/utils/array.ts +9 -6
  58. package/src/utils/base64.ts +13 -2
  59. package/src/utils/clone.ts +28 -30
  60. package/src/utils/date.ts +6 -3
  61. package/src/utils/hostname.ts +27 -27
  62. package/src/utils/json.ts +16 -9
  63. package/src/utils/properties.ts +2 -2
  64. package/src/utils/schema-regex.ts +4 -4
  65. package/src/utils/time.ts +5 -5
  66. package/src/utils/unicode.ts +12 -5
  67. package/src/utils/what-is.ts +1 -5
  68. package/src/validation/array.ts +24 -22
  69. package/src/validation/combinators.ts +14 -14
  70. package/src/validation/numeric.ts +14 -28
  71. package/src/validation/object.ts +32 -36
  72. package/src/validation/ref.ts +5 -6
  73. package/src/validation/shared.ts +22 -21
  74. package/src/validation/string.ts +29 -39
  75. package/src/validation/type.ts +17 -17
  76. package/src/z-schema-base.ts +49 -38
  77. package/src/z-schema-options.ts +4 -3
  78. package/src/z-schema.ts +35 -45
  79. package/umd/ZSchema.js +711 -695
  80. package/umd/ZSchema.min.js +2 -2
  81. package/umd/package.json +3 -0
  82. package/dist/utils/unicode.mjs +0 -12
  83. package/dist/validation/type.mjs +0 -32
  84. /package/dist/{format-validators.d.mts → format-validators.d.ts} +0 -0
  85. /package/dist/{json-schema-versions.mjs → json-schema-versions.js} +0 -0
  86. /package/dist/schemas/{draft-04-schema.mjs → draft-04-schema.js} +0 -0
  87. /package/dist/schemas/{draft-06-schema.mjs → draft-06-schema.js} +0 -0
  88. /package/dist/schemas/{draft-07-schema.mjs → draft-07-schema.js} +0 -0
  89. /package/dist/schemas/{draft-2019-09-meta-applicator.mjs → draft-2019-09-meta-applicator.js} +0 -0
  90. /package/dist/schemas/{draft-2019-09-meta-content.mjs → draft-2019-09-meta-content.js} +0 -0
  91. /package/dist/schemas/{draft-2019-09-meta-core.mjs → draft-2019-09-meta-core.js} +0 -0
  92. /package/dist/schemas/{draft-2019-09-meta-format.mjs → draft-2019-09-meta-format.js} +0 -0
  93. /package/dist/schemas/{draft-2019-09-meta-meta-data.mjs → draft-2019-09-meta-meta-data.js} +0 -0
  94. /package/dist/schemas/{draft-2019-09-meta-validation.mjs → draft-2019-09-meta-validation.js} +0 -0
  95. /package/dist/schemas/{draft-2019-09-schema.mjs → draft-2019-09-schema.js} +0 -0
  96. /package/dist/schemas/{draft-2020-12-meta-applicator.mjs → draft-2020-12-meta-applicator.js} +0 -0
  97. /package/dist/schemas/{draft-2020-12-meta-content.mjs → draft-2020-12-meta-content.js} +0 -0
  98. /package/dist/schemas/{draft-2020-12-meta-core.mjs → draft-2020-12-meta-core.js} +0 -0
  99. /package/dist/schemas/{draft-2020-12-meta-format-annotation.mjs → draft-2020-12-meta-format-annotation.js} +0 -0
  100. /package/dist/schemas/{draft-2020-12-meta-format-assertion.mjs → draft-2020-12-meta-format-assertion.js} +0 -0
  101. /package/dist/schemas/{draft-2020-12-meta-meta-data.mjs → draft-2020-12-meta-meta-data.js} +0 -0
  102. /package/dist/schemas/{draft-2020-12-meta-unevaluated.mjs → draft-2020-12-meta-unevaluated.js} +0 -0
  103. /package/dist/schemas/{draft-2020-12-meta-validation.mjs → draft-2020-12-meta-validation.js} +0 -0
  104. /package/dist/schemas/{draft-2020-12-schema.mjs → draft-2020-12-schema.js} +0 -0
  105. /package/dist/utils/{constants.mjs → constants.js} +0 -0
  106. /package/dist/utils/{date.mjs → date.js} +0 -0
  107. /package/dist/utils/{properties.mjs → properties.js} +0 -0
  108. /package/dist/utils/{symbols.mjs → symbols.js} +0 -0
  109. /package/dist/utils/{uri.mjs → uri.js} +0 -0
  110. /package/dist/{z-schema-reader.mjs → z-schema-reader.js} +0 -0
@@ -75,11 +75,65 @@ export interface JsonSchemaAll extends JsonSchemaCommon {
75
75
  // Internal types (schema + z-schema runtime properties)
76
76
  // ---------------------------------------------------------------------------
77
77
 
78
+ /**
79
+ * Sub-schema-valued keywords. On the internal type these point at
80
+ * `JsonSchemaInternal` (rather than the public `JsonSchema`) so the validator
81
+ * can traverse nested schemas without asserting `as JsonSchemaInternal` at
82
+ * every step.
83
+ */
84
+ type InternalSchemaKeys =
85
+ | 'items'
86
+ | 'additionalItems'
87
+ | 'properties'
88
+ | 'patternProperties'
89
+ | 'additionalProperties'
90
+ | 'dependencies'
91
+ | 'allOf'
92
+ | 'anyOf'
93
+ | 'oneOf'
94
+ | 'not'
95
+ | 'definitions'
96
+ | 'contains'
97
+ | 'propertyNames'
98
+ | 'if'
99
+ | 'then'
100
+ | 'else'
101
+ | '$defs'
102
+ | 'dependentSchemas'
103
+ | 'unevaluatedItems'
104
+ | 'unevaluatedProperties'
105
+ | 'prefixItems';
106
+
78
107
  /**
79
108
  * Internal schema type used throughout the validator. Based on `JsonSchemaAll`
80
- * so that validators can access any draft-specific property without narrowing.
109
+ * so that validators can access any draft-specific property without narrowing,
110
+ * but with every sub-schema keyword re-typed to `JsonSchemaInternal` so nested
111
+ * traversal needs no type assertions.
81
112
  */
82
- export type JsonSchemaInternal = JsonSchemaAll & ZSchemaInternalProperties;
113
+ export type JsonSchemaInternal = Omit<JsonSchemaAll, InternalSchemaKeys> &
114
+ ZSchemaInternalProperties & {
115
+ items?: JsonSchemaInternal | boolean | Array<JsonSchemaInternal | boolean>;
116
+ additionalItems?: boolean | JsonSchemaInternal;
117
+ properties?: Record<string, JsonSchemaInternal | boolean>;
118
+ patternProperties?: Record<string, JsonSchemaInternal>;
119
+ additionalProperties?: boolean | JsonSchemaInternal;
120
+ dependencies?: Record<string, string[] | JsonSchemaInternal>;
121
+ allOf?: JsonSchemaInternal[];
122
+ anyOf?: JsonSchemaInternal[];
123
+ oneOf?: JsonSchemaInternal[];
124
+ not?: JsonSchemaInternal;
125
+ definitions?: Record<string, JsonSchemaInternal>;
126
+ contains?: JsonSchemaInternal;
127
+ propertyNames?: JsonSchemaInternal;
128
+ if?: JsonSchemaInternal | boolean;
129
+ then?: JsonSchemaInternal | boolean;
130
+ else?: JsonSchemaInternal | boolean;
131
+ $defs?: Record<string, JsonSchemaInternal>;
132
+ dependentSchemas?: Record<string, JsonSchemaInternal>;
133
+ unevaluatedItems?: JsonSchemaInternal | boolean;
134
+ unevaluatedProperties?: JsonSchemaInternal | boolean;
135
+ prefixItems?: Array<JsonSchemaInternal | boolean>;
136
+ };
83
137
 
84
138
  /** @deprecated Use `JsonSchemaInternal` — they are now equivalent. */
85
139
  export type JsonSchemaInternalAll = JsonSchemaAll & ZSchemaInternalProperties;
@@ -2,18 +2,19 @@ import type { JsonSchema, JsonSchemaInternal } from './json-schema-versions.js';
2
2
  import type { Reference } from './schema-compiler.js';
3
3
  import type { ZSchemaOptions } from './z-schema-options.js';
4
4
 
5
+ import { DEFAULT_MAX_RECURSION_DEPTH } from './utils/constants.js';
5
6
  import { getRemotePath, isAbsoluteUri } from './utils/uri.js';
7
+ import { isObject } from './utils/what-is.js';
6
8
 
7
9
  /**
8
10
  * Keywords whose values are not JSON Schema sub-schemas and must not be
9
11
  * traversed during schema walking (id collection, reference collection, etc.).
10
12
  */
11
13
  export const NON_SCHEMA_KEYWORDS = ['enum', 'const', 'default', 'examples'] as const;
14
+ export const NON_SCHEMA_KEYWORDS_SET = new Set<string>(NON_SCHEMA_KEYWORDS);
12
15
 
13
16
  /** Returns true if the key is an internal z-schema property (prefixed with `__$`). */
14
17
  export const isInternalKey = (key: string): boolean => key.startsWith('__$');
15
- import { DEFAULT_MAX_RECURSION_DEPTH } from './utils/constants.js';
16
- import { isObject } from './utils/what-is.js';
17
18
 
18
19
  /**
19
20
  * Properties present in ALL JSON Schema drafts (04 through 2020-12) with
@@ -30,7 +31,7 @@ export interface JsonSchemaCommon {
30
31
 
31
32
  // ── Type / enum ─────────────────────────────────────────────────────────
32
33
  type?: string | string[];
33
- enum?: Array<unknown>;
34
+ enum?: unknown[];
34
35
  format?: string;
35
36
 
36
37
  // ── Numeric ─────────────────────────────────────────────────────────────
@@ -78,9 +79,9 @@ export type JsonSchemaType = 'array' | 'boolean' | 'integer' | 'null' | 'number'
78
79
  export interface ZSchemaInternalProperties {
79
80
  __$compiled?: unknown;
80
81
  __$missingReferences?: Reference[];
81
- __$refResolved?: JsonSchema;
82
- __$dynamicRefResolved?: JsonSchema;
83
- __$recursiveRefResolved?: JsonSchema;
82
+ __$refResolved?: JsonSchemaInternal;
83
+ __$dynamicRefResolved?: JsonSchemaInternal;
84
+ __$recursiveRefResolved?: JsonSchemaInternal;
84
85
  __$resourceRoot?: JsonSchemaInternal;
85
86
  __$schemaResolved?: unknown;
86
87
  __$validated?: boolean;
@@ -142,7 +143,7 @@ export const findId = (
142
143
  const inTargetBase = !targetBaseUri || nextBaseUri === targetBaseUri;
143
144
 
144
145
  if (inTargetBase) {
145
- if (schemaId && (schemaId === id || (schemaId[0] === '#' && schemaId.substring(1) === id))) {
146
+ if (schemaId && (schemaId === id || (schemaId.startsWith('#') && schemaId.slice(1) === id))) {
146
147
  return schema;
147
148
  }
148
149
  if (schema.$anchor === id || schema.$dynamicAnchor === id) {
@@ -153,7 +154,7 @@ export const findId = (
153
154
  let result;
154
155
  if (Array.isArray(schema)) {
155
156
  for (let i = 0; i < schema.length; i++) {
156
- result = findId(schema[i], id, targetBaseUri, nextBaseUri, maxDepth, _depth + 1);
157
+ result = findId(schema[i] as JsonSchemaInternal, id, targetBaseUri, nextBaseUri, maxDepth, _depth + 1);
157
158
  if (result) {
158
159
  return result;
159
160
  }
@@ -167,7 +168,7 @@ export const findId = (
167
168
  // intermediate resources - pointer reference across resource boundary".
168
169
  for (let i = keys.length - 1; i >= 0; i--) {
169
170
  const k = keys[i];
170
- if (isInternalKey(k) || NON_SCHEMA_KEYWORDS.includes(k as any)) {
171
+ if (isInternalKey(k) || NON_SCHEMA_KEYWORDS_SET.has(k)) {
171
172
  continue;
172
173
  }
173
174
  result = findId(schema[k] as JsonSchemaInternal, id, targetBaseUri, nextBaseUri, maxDepth, _depth + 1);