@rljson/rljson 0.0.46 → 0.0.48
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.
- package/dist/content/table-cfg.d.ts +7 -0
- package/dist/rljson.js +33 -2
- package/package.json +1 -1
|
@@ -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,36 @@ 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 value = row[columnKey];
|
|
577
|
+
if (value === void 0 || value === null) {
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
const actualType = jsonValueType(row[columnKey]);
|
|
581
|
+
if (expectedType !== actualType) {
|
|
582
|
+
errors.push(
|
|
583
|
+
`Column "${columnKey}" in row ${i} of "${tableKey}" has type "${actualType}", but expected "${expectedType}"`
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return errors;
|
|
589
|
+
};
|
|
560
590
|
const addColumnsToTableCfg = (tableCfg, columns) => {
|
|
561
591
|
const existingKeys = new Set(tableCfg.columns.map((c) => c.key));
|
|
562
592
|
const duplicates = columns.filter((col) => existingKeys.has(col.key));
|
|
@@ -1570,5 +1600,6 @@ export {
|
|
|
1570
1600
|
reservedFieldNames,
|
|
1571
1601
|
reservedTableKeys,
|
|
1572
1602
|
rljsonIndexed,
|
|
1573
|
-
throwOnInvalidTableCfg
|
|
1603
|
+
throwOnInvalidTableCfg,
|
|
1604
|
+
validateRljsonAgainstTableCfg
|
|
1574
1605
|
};
|