@seed-hypermedia/client 0.0.133 → 0.0.135

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.
@@ -2,7 +2,7 @@ import {
2
2
  HM_SCHEMAS,
3
3
  HM_SCHEMA_MANIFEST,
4
4
  HM_SCHEMA_PAGES
5
- } from "./chunk-T7SWLR32.mjs";
5
+ } from "./chunk-U5K6COJT.mjs";
6
6
 
7
7
  // src/schema-engine.ts
8
8
  var LIBRARY_AUTHORITY = "hyper.media";
@@ -18,6 +18,7 @@ var LIBRARY_CORE = new Set(
18
18
  "property",
19
19
  "schema",
20
20
  "anyof",
21
+ "allof",
21
22
  "literal-schema",
22
23
  "struct-schema",
23
24
  "map-schema",
@@ -130,7 +131,7 @@ function isLiteralSchema(s) {
130
131
  if (s === void 0) return false;
131
132
  if (s === null || typeof s !== "object") return true;
132
133
  if (Array.isArray(s)) return false;
133
- return "value" in s && !("type" in s || "ref" in s || "anyOf" in s || "var" in s || "params" in s);
134
+ return "value" in s && !("type" in s || "ref" in s || "anyOf" in s || "allOf" in s || "var" in s || "params" in s);
134
135
  }
135
136
  var literalSchema = (v) => v;
136
137
  var literalValue = (s) => s !== null && typeof s === "object" ? s.value : s;
@@ -158,7 +159,8 @@ function structFields(schema) {
158
159
  name,
159
160
  schema: (entry == null ? void 0 : entry.value) === void 0 ? {} : entry.value,
160
161
  required: (entry == null ? void 0 : entry.required) === true,
161
- description: typeof (entry == null ? void 0 : entry.description) === "string" ? entry.description : void 0
162
+ description: typeof (entry == null ? void 0 : entry.description) === "string" ? entry.description : void 0,
163
+ ...entry && typeof entry === "object" && entry.default !== void 0 ? { default: entry.default } : {}
162
164
  }));
163
165
  }
164
166
  function fieldSchema(schema, name) {
@@ -176,6 +178,7 @@ function fieldsToProperties(fields) {
176
178
  const e = { value: f.schema };
177
179
  if (f.required) e.required = true;
178
180
  if (f.description) e.description = f.description;
181
+ if (f.default !== void 0) e.default = f.default;
179
182
  out[f.name] = e;
180
183
  }
181
184
  return out;
@@ -207,6 +210,40 @@ var LEAF_KEYS = [
207
210
  "maxItems",
208
211
  "target"
209
212
  ];
213
+ function mergeAllOf(arms) {
214
+ const byName = /* @__PURE__ */ new Map();
215
+ let closed = false;
216
+ let isStruct = false;
217
+ let values;
218
+ for (const [i, arm] of Array.from(arms.entries())) {
219
+ if (arm.__unbound || arm.__missing || arm.__invalid) return arm;
220
+ const kind = arm.type ? kindOf(arm.type) : null;
221
+ if (isLiteralSchema(arm) || arm.anyOf || kind !== "map" && kind !== "struct")
222
+ return { __invalid: `allOf arm ${i + 1} is not a struct or map` };
223
+ if (kind === "struct" || arm.properties) isStruct = true;
224
+ for (const f of structFields(arm)) {
225
+ const prior = byName.get(f.name);
226
+ if (prior && !deepEqual(prior.schema, f.schema))
227
+ return { __invalid: `allOf arms define the field "${f.name}" differently` };
228
+ byName.set(f.name, {
229
+ name: f.name,
230
+ schema: f.schema,
231
+ required: ((prior == null ? void 0 : prior.required) ?? false) || f.required,
232
+ description: (prior == null ? void 0 : prior.description) ?? f.description
233
+ });
234
+ }
235
+ if (arm.properties && !arm.values) closed = true;
236
+ else if (arm.values !== void 0) {
237
+ if (values !== void 0 && !deepEqual(values, arm.values))
238
+ return { __invalid: "allOf arms constrain extra keys (`values`) differently" };
239
+ values = arm.values;
240
+ }
241
+ }
242
+ const merged = { type: isStruct ? STRUCT_URL : MAP_URL };
243
+ if (byName.size) merged.properties = fieldsToProperties(Array.from(byName.values()));
244
+ if (!closed && values !== void 0) merged.values = values;
245
+ return merged;
246
+ }
210
247
  function namedSchemaUrl(schema) {
211
248
  if (typeof schema.type === "string") return kindOf(schema.type) === schema.type ? schema.type : null;
212
249
  return typeof schema.ref === "string" ? schema.ref : null;
@@ -224,6 +261,13 @@ function resolveSchema(schema, env = {}, reg = {}) {
224
261
  if (bound === void 0) return { schema: { __unbound: schema.var }, env: {} };
225
262
  return resolveSchema(bound, {}, reg);
226
263
  }
264
+ if (Array.isArray(schema.allOf)) {
265
+ if (schema.allOf.length === 0) return { schema: { __invalid: "allOf needs at least one arm" }, env };
266
+ return {
267
+ schema: mergeAllOf(schema.allOf.map((arm) => resolveSchema(arm, env, reg).schema)),
268
+ env
269
+ };
270
+ }
227
271
  const named = namedSchemaUrl(schema);
228
272
  if (named && schema.anyOf === void 0) {
229
273
  const target = loadFrom(reg, named);
@@ -247,6 +291,7 @@ function validate(schema0, data, path = "$", env0 = {}, reg = {}) {
247
291
  const { schema, env } = resolveSchema(schema0, env0, reg);
248
292
  if (schema.__unbound) return [`${path}: unbound type variable "${schema.__unbound}"`];
249
293
  if (schema.__missing) return [`${path}: unresolved reference "${schema.__missing}"`];
294
+ if (schema.__invalid) return [`${path}: ${schema.__invalid}`];
250
295
  if (isLiteralSchema(schema))
251
296
  return deepEqual(schema.value, data) ? [] : [`${path}: expected ${JSON.stringify(schema.value)}, got ${JSON.stringify(data)}`];
252
297
  if (schema.anyOf) {
@@ -310,6 +355,99 @@ function validate(schema0, data, path = "$", env0 = {}, reg = {}) {
310
355
  return errors;
311
356
  }
312
357
  var validateAdvisory = validate;
358
+ var cloneValue = (v) => v !== null && typeof v === "object" ? JSON.parse(JSON.stringify(v)) : v;
359
+ function seedValue(schema, registry = {}) {
360
+ return seedWithEnv(schema, {}, registry);
361
+ }
362
+ function seedWithEnv(schema0, env, reg) {
363
+ return seedAt(schema0, env, reg, 0);
364
+ }
365
+ var MAX_SEED_DEPTH = 24;
366
+ function seedAt(schema0, env, reg, depth) {
367
+ const { schema, env: e } = resolveSchema(schema0, env, reg);
368
+ if (isLiteralSchema(schema)) return schema.value;
369
+ if (schema.anyOf) return schema.anyOf.length ? seedAt(schema.anyOf[0], e, reg, depth + 1) : void 0;
370
+ const kind = schema.type ? kindOf(schema.type) : null;
371
+ switch (kind) {
372
+ case "map":
373
+ case "struct": {
374
+ const o = {};
375
+ for (const f of structFields(schema)) {
376
+ if (f.default !== void 0) o[f.name] = cloneValue(f.default);
377
+ else if (f.required && depth < MAX_SEED_DEPTH) o[f.name] = seedAt(f.schema, e, reg, depth + 1);
378
+ }
379
+ return o;
380
+ }
381
+ case "list":
382
+ return [];
383
+ case "string":
384
+ return "";
385
+ case "integer":
386
+ case "float":
387
+ return 0;
388
+ case "boolean":
389
+ return false;
390
+ case "null":
391
+ return null;
392
+ case "link":
393
+ return { "/": "" };
394
+ case "bytes":
395
+ return { "/": { bytes: "" } };
396
+ default:
397
+ return null;
398
+ }
399
+ }
400
+ function seedField(field, env = {}, reg = {}) {
401
+ return field.default !== void 0 ? cloneValue(field.default) : seedWithEnv(field.schema, env, reg);
402
+ }
403
+ function propertyDefaults(schema, registry = {}) {
404
+ if (!schema) return {};
405
+ const { schema: resolved } = resolveSchema(schema, {}, registry);
406
+ const out = {};
407
+ for (const f of structFields(resolved)) if (f.default !== void 0) out[f.name] = cloneValue(f.default);
408
+ return out;
409
+ }
410
+ function missingPropertyDefaults(schema, current, registry = {}) {
411
+ const defaults = propertyDefaults(schema, registry);
412
+ const held = current ?? {};
413
+ for (const key of Object.keys(defaults)) if (key in held && held[key] !== void 0) delete defaults[key];
414
+ return defaults;
415
+ }
416
+ function validateDefaults(schema, path = "$", env = {}, reg = {}) {
417
+ if (!schema || typeof schema !== "object" || Array.isArray(schema)) return [];
418
+ let scope = env;
419
+ if (schema.params && typeof schema.params === "object") {
420
+ scope = { ...env };
421
+ for (const [p, def] of Object.entries(schema.params)) if (scope[p] === void 0) scope[p] = def;
422
+ }
423
+ const errors = [];
424
+ if (schema.properties && typeof schema.properties === "object") {
425
+ for (const [name, entry] of Object.entries(schema.properties)) {
426
+ if (!entry || typeof entry !== "object") continue;
427
+ const at = `${path}.properties.${name}`;
428
+ const value = entry.value === void 0 ? {} : entry.value;
429
+ if (entry.default !== void 0) errors.push(...validate(value, entry.default, `${at}.default`, scope, reg));
430
+ errors.push(...validateDefaults(value, `${at}.value`, scope, reg));
431
+ }
432
+ }
433
+ if (schema.items) errors.push(...validateDefaults(schema.items, `${path}.items`, scope, reg));
434
+ if (schema.values) errors.push(...validateDefaults(schema.values, `${path}.values`, scope, reg));
435
+ if (Array.isArray(schema.anyOf))
436
+ schema.anyOf.forEach(
437
+ (arm, i) => errors.push(...validateDefaults(arm, `${path}.anyOf[${i}]`, scope, reg))
438
+ );
439
+ if (schema.params && typeof schema.params === "object")
440
+ for (const [p, def] of Object.entries(schema.params))
441
+ errors.push(...validateDefaults(def, `${path}.params.${p}`, scope, reg));
442
+ return errors;
443
+ }
444
+ function schemaErrors(value, reg = {}) {
445
+ const meta = HM_SCHEMAS["schema"];
446
+ if (!meta) return [];
447
+ const errors = validate(meta, value, "$", {}, reg);
448
+ if (errors.length || !value || typeof value !== "object") return errors;
449
+ return validateDefaults(value, "$", {}, reg);
450
+ }
313
451
  function isHypermediaSchema(value, reg = {}) {
314
452
  const meta = HM_SCHEMAS["schema"];
315
453
  if (!meta || !value || typeof value !== "object") return false;
@@ -321,6 +459,7 @@ function schemaShape(schema) {
321
459
  if (isLiteralSchema(schema)) return { label: "Literal", slug: "schema/literal-schema" };
322
460
  if (typeof schema !== "object") return null;
323
461
  if (Array.isArray(schema.anyOf)) return { label: "Union", slug: "schema/anyof" };
462
+ if (Array.isArray(schema.allOf)) return { label: "Intersection", slug: "schema/allof" };
324
463
  if (typeof schema.var === "string") return { label: `\u27E8${schema.var}\u27E9`, slug: "schema/var-schema" };
325
464
  if (typeof schema.type === "string") {
326
465
  const kind = kindOf(schema.type);
@@ -350,6 +489,10 @@ function collectRefs(schema, acc = /* @__PURE__ */ new Set()) {
350
489
  if (typeof schema.target === "string") acc.add(refToName(schema.target));
351
490
  for (const [k, v] of Object.entries(schema)) {
352
491
  if (k === "ref" || k === "type" || k === "target") continue;
492
+ if (k === "properties" && v && typeof v === "object" && !Array.isArray(v)) {
493
+ for (const entry of Object.values(v)) collectRefs(entry == null ? void 0 : entry.value, acc);
494
+ continue;
495
+ }
353
496
  if (v && typeof v === "object") collectRefs(v, acc);
354
497
  }
355
498
  return acc;
@@ -396,10 +539,18 @@ export {
396
539
  requiredFieldNames,
397
540
  fieldsToProperties,
398
541
  mergeExtend,
542
+ mergeAllOf,
399
543
  namedSchemaUrl,
400
544
  resolveSchema,
401
545
  validate,
402
546
  validateAdvisory,
547
+ seedValue,
548
+ seedWithEnv,
549
+ seedField,
550
+ propertyDefaults,
551
+ missingPropertyDefaults,
552
+ validateDefaults,
553
+ schemaErrors,
403
554
  isHypermediaSchema,
404
555
  schemaShape,
405
556
  collectRefs,