@pujansrt/data-genie
Version:
High performant ETL engine written in TypeScript
81 lines (80 loc) • 2.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldValidator = exports.ValidatingReader = void 0;
const transformers_1 = require("../transformers/transformers");
class ValidatingReader extends transformers_1.DataTransformer {
constructor(reader) {
super(reader);
this.rules = [];
this.messages = [];
this.throwExceptionOnFailure = false;
this.recordStackTraceInMessage = false; // For more detailed error reporting
}
add(rule) {
this.rules.push(rule);
return this;
}
setExceptionOnFailure(value) {
this.throwExceptionOnFailure = value;
return this;
}
setRecordStackTraceInMessage(value) {
this.recordStackTraceInMessage = value;
return this;
}
getMessages() {
return this.messages;
}
async *read() {
for await (const record of this.reader.read()) {
let isValid = true;
const recordMessages = [];
for (const rule of this.rules) {
if (!rule(record, recordMessages)) {
isValid = false;
}
}
if (!isValid && this.throwExceptionOnFailure) {
const errorMessage = `Validation failed for record: ${JSON.stringify(record)}. Messages: ${JSON.stringify(recordMessages)}`;
if (this.recordStackTraceInMessage) {
// You might want to capture a stack trace here if needed, but it's more complex
}
throw new Error(errorMessage);
}
else if (!isValid) {
this.messages.push(...recordMessages);
}
yield record; // Yield all records, valid or not, or add a flag to filter them out
}
}
}
exports.ValidatingReader = ValidatingReader;
class FieldValidator {
constructor(fieldName) {
this.rules = [];
this.fieldName = fieldName;
}
addRule(rule, errorMessage) {
this.rules.push((value, messages) => {
if (!rule(value)) {
messages.push({ record: {}, field: this.fieldName, message: errorMessage });
return false;
}
return true;
});
return this;
}
createRecordValidationRule() {
return (record, messages) => {
const value = record[this.fieldName];
let isValid = true;
for (const rule of this.rules) {
if (!rule(value, messages)) {
isValid = false;
}
}
return isValid;
};
}
}
exports.FieldValidator = FieldValidator;