longcelot-sheet-db
Version:
Google Sheets-backed staging database adapter for Node.js with schema-first design
32 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildValidationRules = buildValidationRules;
const BOOLEAN_FORMAT_VALUES = {
TRUE_FALSE: ['TRUE', 'FALSE'],
'1_0': ['1', '0'],
};
/**
* Maps boolean()/enum() columns to the Sheets data validation rules formatSheet()/extendValidation()
* apply. boolean() columns use ONE_OF_LIST (not the native BOOLEAN checkbox type) deliberately: Sheets'
* BOOLEAN validation writes a real FALSE into every blank cell it covers, while an unselected ONE_OF_LIST
* cell stays genuinely empty — see FAQ.md #10 for why that distinction is the actual root cause of the
* phantom-row issue for boolean() specifically. `defaultBooleanFormat` is the project-wide sheetStyle
* fallback; a column's own `booleanFormat` (set via `boolean({ format })`) takes priority.
*/
function buildValidationRules(headers, columns, defaultBooleanFormat = 'TRUE_FALSE') {
const rules = [];
headers.forEach((header, columnIndex) => {
const col = columns[header];
if (!col)
return;
if (col.type === 'boolean') {
const values = BOOLEAN_FORMAT_VALUES[col.booleanFormat ?? defaultBooleanFormat];
rules.push({ columnIndex, type: 'ONE_OF_LIST', values });
}
else if (col.enum && col.enum.length > 0) {
rules.push({ columnIndex, type: 'ONE_OF_LIST', values: col.enum });
}
});
return rules;
}
//# sourceMappingURL=validationRules.js.map