js-bao 0.4.0 → 0.4.2

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.
@@ -1383,6 +1383,7 @@ function defineModelSchema(input) {
1383
1383
  const { name, fields } = input;
1384
1384
  const options = {
1385
1385
  name,
1386
+ className: input.options?.className,
1386
1387
  uniqueConstraints: input.options?.uniqueConstraints ? [...input.options.uniqueConstraints] : void 0,
1387
1388
  relationships: input.options?.relationships
1388
1389
  };
@@ -1459,12 +1460,52 @@ var VALID_FIELD_TYPES = /* @__PURE__ */ new Set([
1459
1460
  "id",
1460
1461
  "stringset"
1461
1462
  ]);
1462
- function parseFieldOptions(raw) {
1463
+ var KNOWN_FIELD_KEYS = /* @__PURE__ */ new Set([
1464
+ "type",
1465
+ "indexed",
1466
+ "unique",
1467
+ "required",
1468
+ "auto_assign",
1469
+ "max_length",
1470
+ "max_count",
1471
+ "default"
1472
+ ]);
1473
+ var KNOWN_MODEL_KEYS = /* @__PURE__ */ new Set([
1474
+ "fields",
1475
+ "relationships",
1476
+ "unique_constraints",
1477
+ "class_name"
1478
+ ]);
1479
+ var KNOWN_RELATIONSHIP_KEYS = /* @__PURE__ */ new Set([
1480
+ "type",
1481
+ "model",
1482
+ "related_id_field",
1483
+ "join_model",
1484
+ "join_model_local_field",
1485
+ "join_model_related_field",
1486
+ "order_by_field",
1487
+ "order_direction",
1488
+ "join_model_order_by_field",
1489
+ "join_model_order_direction"
1490
+ ]);
1491
+ var KNOWN_UNIQUE_CONSTRAINT_KEYS = /* @__PURE__ */ new Set(["name", "fields"]);
1492
+ function checkUnknownKeys(raw, known, context, strict) {
1493
+ if (!strict) return;
1494
+ for (const key of Object.keys(raw)) {
1495
+ if (!known.has(key)) {
1496
+ throw new Error(
1497
+ `${context}: unknown key "${key}". Allowed: ${[...known].join(", ")}`
1498
+ );
1499
+ }
1500
+ }
1501
+ }
1502
+ function parseFieldOptions(raw, context, strict) {
1463
1503
  if (!raw.type || !VALID_FIELD_TYPES.has(raw.type)) {
1464
1504
  throw new Error(
1465
1505
  `Invalid field type "${raw.type}". Must be one of: ${[...VALID_FIELD_TYPES].join(", ")}`
1466
1506
  );
1467
1507
  }
1508
+ checkUnknownKeys(raw, KNOWN_FIELD_KEYS, context, strict);
1468
1509
  const opts = { type: raw.type };
1469
1510
  if (raw.indexed === true) opts.indexed = true;
1470
1511
  if (raw.unique === true) opts.unique = true;
@@ -1480,8 +1521,9 @@ function requireField(raw, field, context) {
1480
1521
  throw new Error(`Relationship ${context}: missing required field "${field}"`);
1481
1522
  }
1482
1523
  }
1483
- function parseRelationship(raw) {
1524
+ function parseRelationship(raw, context, strict) {
1484
1525
  const type = raw.type;
1526
+ checkUnknownKeys(raw, KNOWN_RELATIONSHIP_KEYS, context, strict);
1485
1527
  if (type === "refersTo") {
1486
1528
  requireField(raw, "model", "refersTo");
1487
1529
  requireField(raw, "related_id_field", "refersTo");
@@ -1523,7 +1565,8 @@ function parseRelationship(raw) {
1523
1565
  }
1524
1566
  throw new Error(`Unknown relationship type: ${type}`);
1525
1567
  }
1526
- function loadSchemaFromTomlString(tomlString) {
1568
+ function loadSchemaFromTomlString(tomlString, options = {}) {
1569
+ const strict = options.strict !== false;
1527
1570
  const parsed = parseToml(tomlString);
1528
1571
  const models = parsed.models;
1529
1572
  if (!models || typeof models !== "object") {
@@ -1531,10 +1574,20 @@ function loadSchemaFromTomlString(tomlString) {
1531
1574
  }
1532
1575
  const schemas = [];
1533
1576
  for (const [modelName, modelDef] of Object.entries(models)) {
1577
+ checkUnknownKeys(
1578
+ modelDef,
1579
+ KNOWN_MODEL_KEYS,
1580
+ `[models.${modelName}]`,
1581
+ strict
1582
+ );
1534
1583
  const fields = {};
1535
1584
  if (modelDef.fields) {
1536
1585
  for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
1537
- fields[fieldName] = parseFieldOptions(fieldDef);
1586
+ fields[fieldName] = parseFieldOptions(
1587
+ fieldDef,
1588
+ `[models.${modelName}.fields.${fieldName}]`,
1589
+ strict
1590
+ );
1538
1591
  }
1539
1592
  }
1540
1593
  let relationships;
@@ -1543,24 +1596,36 @@ function loadSchemaFromTomlString(tomlString) {
1543
1596
  for (const [relName, relDef] of Object.entries(
1544
1597
  modelDef.relationships
1545
1598
  )) {
1546
- relationships[relName] = parseRelationship(relDef);
1599
+ relationships[relName] = parseRelationship(
1600
+ relDef,
1601
+ `[models.${modelName}.relationships.${relName}]`,
1602
+ strict
1603
+ );
1547
1604
  }
1548
1605
  }
1549
1606
  let uniqueConstraints;
1550
1607
  if (modelDef.unique_constraints) {
1551
1608
  uniqueConstraints = [];
1552
1609
  for (const raw of modelDef.unique_constraints) {
1610
+ checkUnknownKeys(
1611
+ raw,
1612
+ KNOWN_UNIQUE_CONSTRAINT_KEYS,
1613
+ `[[models.${modelName}.unique_constraints]]`,
1614
+ strict
1615
+ );
1553
1616
  uniqueConstraints.push({
1554
1617
  name: raw.name,
1555
1618
  fields: [...raw.fields]
1556
1619
  });
1557
1620
  }
1558
1621
  }
1622
+ const className = typeof modelDef.class_name === "string" ? modelDef.class_name : void 0;
1559
1623
  schemas.push(
1560
1624
  defineModelSchema({
1561
1625
  name: modelName,
1562
1626
  fields,
1563
1627
  options: {
1628
+ className,
1564
1629
  uniqueConstraints,
1565
1630
  relationships
1566
1631
  }