zarr-metadata 0.7.0 → 0.9.0

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 (156) hide show
  1. package/README.md +13 -6
  2. package/dist/chunk-grid/index.d.ts +27 -0
  3. package/dist/chunk-grid/index.d.ts.map +1 -0
  4. package/dist/chunk-grid/index.js +21 -0
  5. package/dist/chunk-grid/index.js.map +1 -0
  6. package/dist/chunk-grid/rectilinear.d.ts +14 -0
  7. package/dist/chunk-grid/rectilinear.d.ts.map +1 -0
  8. package/dist/chunk-grid/rectilinear.js +76 -0
  9. package/dist/chunk-grid/rectilinear.js.map +1 -0
  10. package/dist/chunk-grid/regular.d.ts +7 -0
  11. package/dist/chunk-grid/regular.d.ts.map +1 -0
  12. package/dist/chunk-grid/regular.js +36 -0
  13. package/dist/chunk-grid/regular.js.map +1 -0
  14. package/dist/codec/index.d.ts +34 -0
  15. package/dist/codec/index.d.ts.map +1 -0
  16. package/dist/codec/index.js +35 -0
  17. package/dist/codec/index.js.map +1 -0
  18. package/dist/codec/sharding-indexed.d.ts +26 -0
  19. package/dist/codec/sharding-indexed.d.ts.map +1 -0
  20. package/dist/codec/sharding-indexed.js +80 -0
  21. package/dist/codec/sharding-indexed.js.map +1 -0
  22. package/dist/codec/transpose.d.ts +19 -0
  23. package/dist/codec/transpose.d.ts.map +1 -0
  24. package/dist/codec/transpose.js +66 -0
  25. package/dist/codec/transpose.js.map +1 -0
  26. package/dist/data-type/bool.d.ts +4 -0
  27. package/dist/data-type/bool.d.ts.map +1 -0
  28. package/dist/data-type/bool.js +7 -0
  29. package/dist/data-type/bool.js.map +1 -0
  30. package/dist/data-type/bytes.d.ts +11 -0
  31. package/dist/data-type/bytes.d.ts.map +1 -0
  32. package/dist/data-type/bytes.js +18 -0
  33. package/dist/data-type/bytes.js.map +1 -0
  34. package/dist/data-type/complex128.d.ts +3 -0
  35. package/dist/data-type/complex128.d.ts.map +1 -0
  36. package/dist/data-type/complex128.js +4 -0
  37. package/dist/data-type/complex128.js.map +1 -0
  38. package/dist/data-type/complex64.d.ts +3 -0
  39. package/dist/data-type/complex64.d.ts.map +1 -0
  40. package/dist/data-type/complex64.js +4 -0
  41. package/dist/data-type/complex64.js.map +1 -0
  42. package/dist/data-type/descriptor.d.ts +44 -0
  43. package/dist/data-type/descriptor.d.ts.map +1 -0
  44. package/dist/data-type/descriptor.js +57 -0
  45. package/dist/data-type/descriptor.js.map +1 -0
  46. package/dist/data-type/float16.d.ts +3 -0
  47. package/dist/data-type/float16.d.ts.map +1 -0
  48. package/dist/data-type/float16.js +4 -0
  49. package/dist/data-type/float16.js.map +1 -0
  50. package/dist/data-type/float32.d.ts +3 -0
  51. package/dist/data-type/float32.d.ts.map +1 -0
  52. package/dist/data-type/float32.js +4 -0
  53. package/dist/data-type/float32.js.map +1 -0
  54. package/dist/data-type/float64.d.ts +3 -0
  55. package/dist/data-type/float64.d.ts.map +1 -0
  56. package/dist/data-type/float64.js +4 -0
  57. package/dist/data-type/float64.js.map +1 -0
  58. package/dist/data-type/index.d.ts +23 -0
  59. package/dist/data-type/index.d.ts.map +1 -0
  60. package/dist/data-type/index.js +92 -0
  61. package/dist/data-type/index.js.map +1 -0
  62. package/dist/data-type/int16.d.ts +3 -0
  63. package/dist/data-type/int16.d.ts.map +1 -0
  64. package/dist/data-type/int16.js +4 -0
  65. package/dist/data-type/int16.js.map +1 -0
  66. package/dist/data-type/int32.d.ts +3 -0
  67. package/dist/data-type/int32.d.ts.map +1 -0
  68. package/dist/data-type/int32.js +4 -0
  69. package/dist/data-type/int32.js.map +1 -0
  70. package/dist/data-type/int64.d.ts +3 -0
  71. package/dist/data-type/int64.d.ts.map +1 -0
  72. package/dist/data-type/int64.js +4 -0
  73. package/dist/data-type/int64.js.map +1 -0
  74. package/dist/data-type/int8.d.ts +3 -0
  75. package/dist/data-type/int8.d.ts.map +1 -0
  76. package/dist/data-type/int8.js +4 -0
  77. package/dist/data-type/int8.js.map +1 -0
  78. package/dist/data-type/numpy-datetime64.d.ts +18 -0
  79. package/dist/data-type/numpy-datetime64.d.ts.map +1 -0
  80. package/dist/data-type/numpy-datetime64.js +14 -0
  81. package/dist/data-type/numpy-datetime64.js.map +1 -0
  82. package/dist/data-type/numpy-timedelta64.d.ts +4 -0
  83. package/dist/data-type/numpy-timedelta64.d.ts.map +1 -0
  84. package/dist/data-type/numpy-timedelta64.js +4 -0
  85. package/dist/data-type/numpy-timedelta64.js.map +1 -0
  86. package/dist/data-type/raw.d.ts +10 -0
  87. package/dist/data-type/raw.d.ts.map +1 -0
  88. package/dist/data-type/raw.js +26 -0
  89. package/dist/data-type/raw.js.map +1 -0
  90. package/dist/data-type/string.d.ts +6 -0
  91. package/dist/data-type/string.d.ts.map +1 -0
  92. package/dist/data-type/string.js +7 -0
  93. package/dist/data-type/string.js.map +1 -0
  94. package/dist/data-type/struct.d.ts +17 -0
  95. package/dist/data-type/struct.d.ts.map +1 -0
  96. package/dist/data-type/struct.js +44 -0
  97. package/dist/data-type/struct.js.map +1 -0
  98. package/dist/data-type/uint16.d.ts +3 -0
  99. package/dist/data-type/uint16.d.ts.map +1 -0
  100. package/dist/data-type/uint16.js +4 -0
  101. package/dist/data-type/uint16.js.map +1 -0
  102. package/dist/data-type/uint32.d.ts +3 -0
  103. package/dist/data-type/uint32.d.ts.map +1 -0
  104. package/dist/data-type/uint32.js +4 -0
  105. package/dist/data-type/uint32.js.map +1 -0
  106. package/dist/data-type/uint64.d.ts +3 -0
  107. package/dist/data-type/uint64.d.ts.map +1 -0
  108. package/dist/data-type/uint64.js +4 -0
  109. package/dist/data-type/uint64.js.map +1 -0
  110. package/dist/data-type/uint8.d.ts +3 -0
  111. package/dist/data-type/uint8.d.ts.map +1 -0
  112. package/dist/data-type/uint8.js +4 -0
  113. package/dist/data-type/uint8.js.map +1 -0
  114. package/dist/guards.d.ts +21 -0
  115. package/dist/guards.d.ts.map +1 -0
  116. package/dist/guards.js +39 -0
  117. package/dist/guards.js.map +1 -0
  118. package/dist/index.d.ts +3 -0
  119. package/dist/index.d.ts.map +1 -1
  120. package/dist/index.js.map +1 -1
  121. package/dist/semantics.d.ts +14 -42
  122. package/dist/semantics.d.ts.map +1 -1
  123. package/dist/semantics.js +22 -416
  124. package/dist/semantics.js.map +1 -1
  125. package/package.json +4 -2
  126. package/src/chunk-grid/index.ts +40 -0
  127. package/src/chunk-grid/rectilinear.ts +88 -0
  128. package/src/chunk-grid/regular.ts +42 -0
  129. package/src/codec/index.ts +76 -0
  130. package/src/codec/sharding-indexed.ts +113 -0
  131. package/src/codec/transpose.ts +80 -0
  132. package/src/data-type/bool.ts +8 -0
  133. package/src/data-type/bytes.ts +26 -0
  134. package/src/data-type/complex128.ts +4 -0
  135. package/src/data-type/complex64.ts +4 -0
  136. package/src/data-type/descriptor.ts +115 -0
  137. package/src/data-type/float16.ts +4 -0
  138. package/src/data-type/float32.ts +4 -0
  139. package/src/data-type/float64.ts +4 -0
  140. package/src/data-type/index.ts +129 -0
  141. package/src/data-type/int16.ts +4 -0
  142. package/src/data-type/int32.ts +4 -0
  143. package/src/data-type/int64.ts +4 -0
  144. package/src/data-type/int8.ts +4 -0
  145. package/src/data-type/numpy-datetime64.ts +33 -0
  146. package/src/data-type/numpy-timedelta64.ts +9 -0
  147. package/src/data-type/raw.ts +31 -0
  148. package/src/data-type/string.ts +11 -0
  149. package/src/data-type/struct.ts +55 -0
  150. package/src/data-type/uint16.ts +4 -0
  151. package/src/data-type/uint32.ts +4 -0
  152. package/src/data-type/uint64.ts +4 -0
  153. package/src/data-type/uint8.ts +4 -0
  154. package/src/guards.ts +48 -0
  155. package/src/index.ts +22 -0
  156. package/src/semantics.ts +24 -444
package/src/semantics.ts CHANGED
@@ -1,464 +1,44 @@
1
1
  /**
2
- * Semantic (cross-field) validation for v3 array metadata.
2
+ * Semantic (cross-field) validation for v3 array metadata: the
3
+ * orchestrator. The rules themselves live with their content —
4
+ * `chunk-grid/`, `codec/`, and `data-type/` hold one module per
5
+ * interpreted name (grids, codecs, data types), each with its syntax
6
+ * (types) and semantics (procedures), mirroring how the spec, the Python
7
+ * reference's type layout, and the zarr-extensions registry are organized. This module only walks
8
+ * documents: extract shape, ask each content module for its verdicts,
9
+ * thread the chunk-size context into codec pipelines, and descend into a
10
+ * group's inline consolidated entries.
3
11
  *
4
- * The structural validators never interpret what an extension point names;
5
- * this module deliberately interprets a small set of WELL-KNOWN core
6
- * extension points to enforce the cross-field rules the specs state in
7
- * prose:
8
- *
9
- * - the `regular` chunk grid REQUIRES a configuration with `chunk_shape`,
10
- * which has one length per dimension of `shape`;
11
- * - the `rectilinear` chunk grid requires a configuration with `kind` and
12
- * `chunk_shapes`; `chunk_shapes` has one entry per dimension, and each
13
- * explicit chunk list (integers and `[size, count]` run-length pairs)
14
- * sums exactly to that dimension's length (the bare-integer uniform
15
- * shorthand carries no sum constraint, like the regular grid);
16
- * - a `transpose` codec requires a configuration with `order`, a
17
- * permutation of `0..n-1` with one entry per array dimension;
18
- * - a `sharding_indexed` codec requires a configuration with
19
- * `chunk_shape`, `codecs`, and `index_codecs`; its inner `chunk_shape`
20
- * matches the array's
21
- * dimensionality and evenly divides every chunk it shards — the grid's
22
- * chunk at the top level (each distinct per-dimension size, for
23
- * rectilinear grids), the parent shard's inner chunk when sharding
24
- * nests;
25
- * - `fill_value` has a JSON shape permitted for the named core data type
26
- * (booleans for `bool`, ranged integers for the int types, numbers /
27
- * the "NaN"-family sentinels / width-checked "0x…" strings for the
28
- * float types, two-element arrays for the complex types).
29
- *
30
- * Dimensional context is THREADED through codec pipelines rather than
31
- * assumed constant (mirroring zarr-python's chunk-spec threading): a valid
32
- * `transpose` permutes the per-dimension chunk sizes for the codecs after
33
- * it, and any codec this layer cannot reason about — `reshape` may change
34
- * a chunk's rank, and unknown codecs may do anything — invalidates the
35
- * dimensional context for the rest of the pipeline instead of letting
36
- * stale array-level facts produce false verdicts.
37
- *
38
- * Required-configuration rules cover ONLY the names this layer interprets
39
- * (the two grids, transpose, sharding). Other extensions' required fields
40
- * are registry-schema facts — the editor's registry layer enforces them
41
- * from the vendored schemas, and duplicating that knowledge here as
42
- * hardcoded rules would drift. Unrecognized names are skipped everywhere —
43
- * the extension name space is open, and a rule that guessed would lie. This layer has no counterpart
12
+ * Unrecognized names are skipped everywhere — the extension name space is
13
+ * open, and a rule that guessed would lie. This layer has no counterpart
44
14
  * in the Python reference implementation (which stops at structure), so it
45
15
  * is covered by this package's own tests rather than the conformance
46
- * corpus.
16
+ * corpus. The structural document layer (validation.ts) stays
17
+ * content-agnostic and corpus-governed; it never reads the content
18
+ * modules.
47
19
  */
48
20
 
21
+ import { chunkGridVerdict } from "./chunk-grid/index.js";
22
+ import { pipelineIssues } from "./codec/index.js";
23
+ import { dataTypeVerdict } from "./data-type/index.js";
49
24
  import { treeOf, type ErrorTree, type PathedIssue } from "./errors.js";
50
-
51
- type Path = ReadonlyArray<string | number>;
52
-
53
- function isPlainObject(value: unknown): value is Record<string, unknown> {
54
- if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
55
- const proto: unknown = Object.getPrototypeOf(value);
56
- return proto === null || proto === Object.prototype;
57
- }
58
-
59
- function isIntArray(value: unknown): value is number[] {
60
- return (
61
- Array.isArray(value) &&
62
- Object.keys(value).length === value.length &&
63
- value.every((item) => Number.isInteger(item))
64
- );
65
- }
66
-
67
- /** The `{name, configuration}` of a metadata field, when extractable. */
68
- function fieldParts(
69
- field: unknown,
70
- ): { name: string; configuration: Record<string, unknown> | undefined } | undefined {
71
- if (typeof field === "string") return { name: field, configuration: undefined };
72
- if (isPlainObject(field) && typeof field["name"] === "string") {
73
- const configuration = field["configuration"];
74
- return {
75
- name: field["name"],
76
- configuration: isPlainObject(configuration) ? configuration : undefined,
77
- };
78
- }
79
- return undefined;
80
- }
81
-
82
- // --- fill_value vs data_type ----------------------------------------------
83
-
84
- const INT_RANGES: Record<string, readonly [number, number]> = {
85
- int8: [-128, 127],
86
- int16: [-32768, 32767],
87
- int32: [-2147483648, 2147483647],
88
- int64: [-9223372036854775808, 9223372036854775807],
89
- uint8: [0, 255],
90
- uint16: [0, 65535],
91
- uint32: [0, 4294967295],
92
- uint64: [0, 18446744073709551615],
93
- };
94
-
95
- const FLOAT_HEX_DIGITS: Record<string, number> = {
96
- float16: 4,
97
- float32: 8,
98
- float64: 16,
99
- };
100
-
101
- /** Hex digit width of each complex type's component floats. */
102
- const COMPLEX_COMPONENT_HEX_DIGITS: Record<string, number> = {
103
- complex64: 8,
104
- complex128: 16,
105
- };
106
-
107
- function isFloatFill(value: unknown, hexDigits: number): boolean {
108
- if (typeof value === "number") return true;
109
- if (typeof value !== "string") return false;
110
- if (value === "NaN" || value === "Infinity" || value === "-Infinity") return true;
111
- return new RegExp(`^0x[0-9a-fA-F]{${hexDigits}}$`).test(value);
112
- }
113
-
114
- function floatFillMessage(name: string, hexDigits: number): string {
115
- return (
116
- `expected a number, "NaN", "Infinity", "-Infinity", or a ` +
117
- `${hexDigits}-hex-digit "0x..." string for data type ${JSON.stringify(name)}`
118
- );
119
- }
120
-
121
- /** The fill_value issue for a KNOWN core data type, if any; undefined otherwise. */
122
- function fillValueIssue(dataTypeName: string, fill: unknown): string | undefined {
123
- if (dataTypeName === "bool") {
124
- return typeof fill === "boolean"
125
- ? undefined
126
- : 'expected a boolean fill value for data type "bool"';
127
- }
128
- const intRange = INT_RANGES[dataTypeName];
129
- if (intRange !== undefined) {
130
- const [low, high] = intRange;
131
- return Number.isInteger(fill) && (fill as number) >= low && (fill as number) <= high
132
- ? undefined
133
- : `expected an integer in [${low}, ${high}] for data type ${JSON.stringify(dataTypeName)}`;
134
- }
135
- const floatDigits = FLOAT_HEX_DIGITS[dataTypeName];
136
- if (floatDigits !== undefined) {
137
- return isFloatFill(fill, floatDigits)
138
- ? undefined
139
- : floatFillMessage(dataTypeName, floatDigits);
140
- }
141
- const complexDigits = COMPLEX_COMPONENT_HEX_DIGITS[dataTypeName];
142
- if (complexDigits !== undefined) {
143
- const ok =
144
- Array.isArray(fill) &&
145
- fill.length === 2 &&
146
- fill.every((part) => isFloatFill(part, complexDigits));
147
- return ok
148
- ? undefined
149
- : `expected a two-element [real, imaginary] array for data type ${JSON.stringify(dataTypeName)}`;
150
- }
151
- return undefined; // unrecognized data type: no verdict
152
- }
153
-
154
- // --- codec pipelines (transpose / sharding) --------------------------------
155
-
156
- function isPermutation(order: number[]): boolean {
157
- const seen = new Set(order);
158
- return seen.size === order.length && order.every((entry) => entry >= 0 && entry < order.length);
159
- }
160
-
161
- /**
162
- * Walk one codec pipeline. `dims` is the array dimensionality at this point
163
- * (undefined when unknowable); `outerChunkSizes` holds the distinct chunk
164
- * lengths per dimension this pipeline may encode — what a sharding codec's
165
- * inner chunks must divide. A regular grid contributes one size per
166
- * dimension; a rectilinear grid may contribute several.
167
- */
168
- function pipelineIssues(
169
- pipeline: unknown[],
170
- path: Path,
171
- initialDims: number | undefined,
172
- initialChunkSizes: number[][] | undefined,
173
- ): PathedIssue[] {
174
- const issues: PathedIssue[] = [];
175
- // The dimensional context THREADS through the pipeline: transpose permutes
176
- // it, and anything this layer cannot reason about invalidates it for the
177
- // remaining codecs.
178
- let dims = initialDims;
179
- let chunkSizes = initialChunkSizes;
180
- const dropContext = (): void => {
181
- dims = undefined;
182
- chunkSizes = undefined;
183
- };
184
- pipeline.forEach((entry, index) => {
185
- const parts = fieldParts(entry);
186
- if (parts === undefined) {
187
- dropContext(); // structurally invalid entry: no further reasoning
188
- return;
189
- }
190
- const { name, configuration } = parts;
191
- // Whether the entry genuinely lacks a configuration member (a malformed
192
- // one is the structural layer's complaint, not ours).
193
- const configMissing =
194
- typeof entry === "string" ||
195
- (isPlainObject(entry) && !Object.hasOwn(entry, "configuration"));
196
- if (name === "transpose") {
197
- if (configMissing) {
198
- issues.push({
199
- path: [...path, index],
200
- message: '"transpose" requires a configuration with "order"',
201
- kind: "missing_key",
202
- });
203
- dropContext();
204
- return;
205
- }
206
- if (configuration !== undefined && !Object.hasOwn(configuration, "order")) {
207
- issues.push({
208
- path: [...path, index, "configuration", "order"],
209
- message: "missing required key",
210
- kind: "missing_key",
211
- });
212
- dropContext();
213
- return;
214
- }
215
- const order = configuration?.["order"];
216
- if (!isIntArray(order)) {
217
- dropContext(); // shape errors are the schema layer's
218
- return;
219
- }
220
- const orderPath = [...path, index, "configuration", "order"];
221
- let sound = true;
222
- if (!isPermutation(order)) {
223
- sound = false;
224
- issues.push({
225
- path: orderPath,
226
- message: `expected a permutation of the integers 0..${order.length - 1}`,
227
- kind: "invalid_value",
228
- });
229
- }
230
- if (dims !== undefined && order.length !== dims) {
231
- sound = false;
232
- issues.push({
233
- path: orderPath,
234
- message: `expected one entry per array dimension (${dims})`,
235
- kind: "invalid_value",
236
- });
237
- }
238
- if (!sound) {
239
- dropContext();
240
- } else if (chunkSizes !== undefined) {
241
- const sizes = chunkSizes;
242
- chunkSizes =
243
- order.length === sizes.length
244
- ? order.map((axis) => sizes[axis] as number[])
245
- : undefined;
246
- }
247
- } else if (name === "sharding_indexed") {
248
- if (configMissing) {
249
- issues.push({
250
- path: [...path, index],
251
- message:
252
- '"sharding_indexed" requires a configuration with "chunk_shape", "codecs", and "index_codecs"',
253
- kind: "missing_key",
254
- });
255
- dropContext();
256
- return;
257
- }
258
- if (configuration !== undefined) {
259
- for (const key of ["chunk_shape", "codecs", "index_codecs"]) {
260
- if (!Object.hasOwn(configuration, key)) {
261
- issues.push({
262
- path: [...path, index, "configuration", key],
263
- message: "missing required key",
264
- kind: "missing_key",
265
- });
266
- }
267
- }
268
- }
269
- const chunkShape = configuration?.["chunk_shape"];
270
- if (!isIntArray(chunkShape)) {
271
- // Still walk a present inner pipeline (entries deserve their own
272
- // verdicts) before the context becomes unknowable.
273
- const inner = configuration?.["codecs"];
274
- if (Array.isArray(inner)) {
275
- issues.push(
276
- ...pipelineIssues(
277
- inner,
278
- [...path, index, "configuration", "codecs"],
279
- undefined,
280
- undefined,
281
- ),
282
- );
283
- }
284
- dropContext();
285
- return;
286
- }
287
- const chunkShapePath = [...path, index, "configuration", "chunk_shape"];
288
- if (dims !== undefined && chunkShape.length !== dims) {
289
- issues.push({
290
- path: chunkShapePath,
291
- message: `expected one length per array dimension (${dims})`,
292
- kind: "invalid_value",
293
- });
294
- } else if (
295
- chunkSizes !== undefined &&
296
- chunkShape.length === chunkSizes.length &&
297
- chunkShape.every((length) => length > 0)
298
- ) {
299
- let violation: { axis: number; size: number } | undefined;
300
- chunkSizes.forEach((sizes, axis) => {
301
- if (violation !== undefined) return;
302
- const bad = sizes.find((size) => size % (chunkShape[axis] as number) !== 0);
303
- if (bad !== undefined) violation = { axis, size: bad };
304
- });
305
- if (violation !== undefined) {
306
- const { axis, size } = violation;
307
- const sizesNow = chunkSizes;
308
- const uniform = sizesNow.every((sizes) => sizes.length === 1);
309
- issues.push({
310
- path: chunkShapePath,
311
- message: uniform
312
- ? `expected ${JSON.stringify(chunkShape)} to evenly divide the outer chunk shape ${JSON.stringify(sizesNow.map((sizes) => sizes[0]))}`
313
- : `expected ${JSON.stringify(chunkShape)} to evenly divide every chunk size of the grid (dimension ${axis} has chunk size ${size})`,
314
- kind: "invalid_value",
315
- });
316
- }
317
- }
318
- const inner = configuration?.["codecs"];
319
- if (Array.isArray(inner)) {
320
- issues.push(
321
- ...pipelineIssues(
322
- inner,
323
- [...path, index, "configuration", "codecs"],
324
- chunkShape.length,
325
- chunkShape.map((length) => [length]),
326
- ),
327
- );
328
- }
329
- // index_codecs encode the shard index, whose shape differs from the
330
- // array's — no dimensional context applies there. After the
331
- // array -> bytes boundary the dimensional context is spent.
332
- dropContext();
333
- } else {
334
- // A codec this layer cannot reason about (reshape may change a
335
- // chunk's rank; unknown codecs may do anything): stale array-level
336
- // facts must not judge the codecs after it.
337
- dropContext();
338
- }
339
- });
340
- return issues;
341
- }
25
+ import { isIntArray, isPlainObject } from "./guards.js";
342
26
 
343
27
  function arraySemanticsIssues(value: unknown): PathedIssue[] {
344
28
  if (!isPlainObject(value) || value["node_type"] !== "array") return [];
345
29
  const issues: PathedIssue[] = [];
346
30
  const shape = isIntArray(value["shape"]) ? value["shape"] : undefined;
347
- const dims = shape?.length;
348
31
 
349
- const rawGrid = value["chunk_grid"];
350
- const grid = fieldParts(rawGrid);
351
- // Whether the field genuinely lacks a configuration member (a malformed
352
- // one — configuration: 5 — is the structural layer's complaint, not ours).
353
- const configMissing =
354
- typeof rawGrid === "string" ||
355
- (isPlainObject(rawGrid) && !Object.hasOwn(rawGrid, "configuration"));
356
- let chunkSizes: number[][] | undefined;
357
- if (grid?.name === "regular") {
358
- if (configMissing) {
359
- issues.push({
360
- path: ["chunk_grid"],
361
- message: '"regular" requires a configuration with "chunk_shape"',
362
- kind: "missing_key",
363
- });
364
- } else if (grid.configuration !== undefined && !Object.hasOwn(grid.configuration, "chunk_shape")) {
365
- issues.push({
366
- path: ["chunk_grid", "configuration", "chunk_shape"],
367
- message: "missing required key",
368
- kind: "missing_key",
369
- });
370
- }
371
- const configured = grid.configuration?.["chunk_shape"];
372
- if (isIntArray(configured)) {
373
- if (dims !== undefined && configured.length !== dims) {
374
- issues.push({
375
- path: ["chunk_grid", "configuration", "chunk_shape"],
376
- message: `expected one length per dimension of shape (${dims})`,
377
- kind: "invalid_value",
378
- });
379
- // wrong arity: unusable as division context
380
- } else {
381
- chunkSizes = configured.map((length) => [length]);
382
- }
383
- }
384
- } else if (grid?.name === "rectilinear") {
385
- if (configMissing) {
386
- issues.push({
387
- path: ["chunk_grid"],
388
- message: '"rectilinear" requires a configuration with "kind" and "chunk_shapes"',
389
- kind: "missing_key",
390
- });
391
- } else if (grid.configuration !== undefined) {
392
- for (const key of ["kind", "chunk_shapes"]) {
393
- if (!Object.hasOwn(grid.configuration, key)) {
394
- issues.push({
395
- path: ["chunk_grid", "configuration", key],
396
- message: "missing required key",
397
- kind: "missing_key",
398
- });
399
- }
400
- }
401
- }
402
- const specs = grid.configuration?.["chunk_shapes"];
403
- if (Array.isArray(specs)) {
404
- if (dims !== undefined && specs.length !== dims) {
405
- issues.push({
406
- path: ["chunk_grid", "configuration", "chunk_shapes"],
407
- message: `expected one entry per dimension of shape (${dims})`,
408
- kind: "invalid_value",
409
- });
410
- } else {
411
- const perDim: (number[] | undefined)[] = specs.map((spec, dim) => {
412
- // Bare integer: uniform shorthand, no sum constraint (edge chunks
413
- // are permitted, as with the regular grid).
414
- if (Number.isInteger(spec)) return spec as number > 0 ? [spec as number] : undefined;
415
- if (!Array.isArray(spec)) return undefined; // registry schema's problem
416
- const sizes = new Set<number>();
417
- let total = 0;
418
- for (const entry of spec) {
419
- if (Number.isInteger(entry)) {
420
- sizes.add(entry as number);
421
- total += entry as number;
422
- } else if (
423
- Array.isArray(entry) &&
424
- entry.length === 2 &&
425
- Number.isInteger(entry[0]) &&
426
- Number.isInteger(entry[1])
427
- ) {
428
- sizes.add(entry[0] as number);
429
- total += (entry[0] as number) * (entry[1] as number);
430
- } else {
431
- return undefined; // malformed entry: registry schema's problem
432
- }
433
- }
434
- const extent = shape?.[dim];
435
- if (extent !== undefined && total !== extent) {
436
- issues.push({
437
- path: ["chunk_grid", "configuration", "chunk_shapes", dim],
438
- message: `expected chunk sizes summing to ${extent} along dimension ${dim}, got ${total}`,
439
- kind: "invalid_value",
440
- });
441
- }
442
- return [...sizes];
443
- });
444
- if (perDim.every((sizes) => sizes !== undefined)) {
445
- chunkSizes = perDim as number[][];
446
- }
447
- }
448
- }
449
- }
32
+ const grid = chunkGridVerdict(value["chunk_grid"], shape);
33
+ issues.push(
34
+ ...grid.issues.map((issue) => ({ ...issue, path: ["chunk_grid", ...issue.path] })),
35
+ );
450
36
 
451
- const dataType = fieldParts(value["data_type"]);
452
- if (dataType !== undefined && "fill_value" in value) {
453
- const message = fillValueIssue(dataType.name, value["fill_value"]);
454
- if (message !== undefined) {
455
- issues.push({ path: ["fill_value"], message, kind: "invalid_value" });
456
- }
457
- }
37
+ issues.push(...dataTypeVerdict(value["data_type"], "fill_value" in value, value["fill_value"]));
458
38
 
459
39
  const codecs = value["codecs"];
460
40
  if (Array.isArray(codecs)) {
461
- issues.push(...pipelineIssues(codecs, ["codecs"], dims, chunkSizes));
41
+ issues.push(...pipelineIssues(codecs, ["codecs"], shape?.length, grid.chunkSizes));
462
42
  }
463
43
  return issues;
464
44
  }