@rljson/rljson 0.0.45 → 0.0.47

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.
@@ -72,6 +72,13 @@ export type TablesCfgTable = RljsonTable<TableCfg, 'ingredients'>;
72
72
  * @param tableCfg - The table configuration to check
73
73
  */
74
74
  export declare const throwOnInvalidTableCfg: (tableCfg: TableCfg) => void;
75
+ /**
76
+ * Validates the table configuration against the data.
77
+ * @param rows - The rows to validate
78
+ * @param tableCfg - The table configuration to validate against
79
+ * @returns - An array of error messages
80
+ */
81
+ export declare const validateRljsonAgainstTableCfg: (rows: Json[], tableCfg: TableCfg) => string[];
75
82
  /**
76
83
  * Add columns to table config
77
84
  * @param tableCfg - The table configuration to add columns to
package/dist/rljson.js CHANGED
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import { hip, hsh } from "@rljson/hash";
5
- import { exampleJsonObject, jsonValueTypes, jsonValueMatchesType } from "@rljson/json";
5
+ import { exampleJsonObject, jsonValueTypes, jsonValueType, jsonValueMatchesType } from "@rljson/json";
6
6
  // @license
7
7
  const bakeryExample = () => {
8
8
  const nutritionalValues = hip({
@@ -557,6 +557,32 @@ const throwOnInvalidTableCfg = (tableCfg) => {
557
557
  }
558
558
  }
559
559
  };
560
+ const validateRljsonAgainstTableCfg = (rows, tableCfg) => {
561
+ const errors = [];
562
+ const tableKey = tableCfg.key;
563
+ for (let i = 0; i < rows.length; i++) {
564
+ const row = rows[i];
565
+ for (const columnKey in row) {
566
+ const columnCfg = tableCfg.columns.find(
567
+ (column) => column.key === columnKey
568
+ );
569
+ if (!columnCfg) {
570
+ errors.push(
571
+ `Column "${columnKey}" in row ${i} of table "${tableKey}" does not exist.`
572
+ );
573
+ continue;
574
+ }
575
+ const expectedType = columnCfg.type;
576
+ const actualType = jsonValueType(row[columnKey]);
577
+ if (expectedType !== actualType) {
578
+ errors.push(
579
+ `Column "${columnKey}" in row ${i} of "${tableKey}" has type "${actualType}", but expected "${expectedType}"`
580
+ );
581
+ }
582
+ }
583
+ }
584
+ return errors;
585
+ };
560
586
  const addColumnsToTableCfg = (tableCfg, columns) => {
561
587
  const existingKeys = new Set(tableCfg.columns.map((c) => c.key));
562
588
  const duplicates = columns.filter((col) => existingKeys.has(col.key));
@@ -566,10 +592,16 @@ const addColumnsToTableCfg = (tableCfg, columns) => {
566
592
  `The following columns already exist in the table "${tableCfg.key}": ${keys}`
567
593
  );
568
594
  }
569
- return hsh({
570
- ...tableCfg,
571
- columns: [...tableCfg.columns, ...columns]
572
- });
595
+ return hsh(
596
+ {
597
+ ...tableCfg,
598
+ columns: [...tableCfg.columns, ...columns]
599
+ },
600
+ {
601
+ updateExistingHashes: true,
602
+ throwOnWrongHashes: false
603
+ }
604
+ );
573
605
  };
574
606
  const exampleTableCfgTable = () => Example.ok.singleRow().tableCfgs;
575
607
  const exampleTableCfg = (tableCfg = void 0) => {
@@ -1564,5 +1596,6 @@ export {
1564
1596
  reservedFieldNames,
1565
1597
  reservedTableKeys,
1566
1598
  rljsonIndexed,
1567
- throwOnInvalidTableCfg
1599
+ throwOnInvalidTableCfg,
1600
+ validateRljsonAgainstTableCfg
1568
1601
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/rljson",
3
- "version": "0.0.45",
3
+ "version": "0.0.47",
4
4
  "packageManager": "pnpm@10.6.3",
5
5
  "description": "The RLJSON data format specification",
6
6
  "homepage": "https://github.com/rljson/rljson",