Show the fieldName when validation failed
moktc opened this issue · 1 comments
moktc commented
Would help alot if the validation can show which field failed validation.
See below a small change I made to the code.
function validateRecord(fields, record) {
for (let i = 0; i < fields.length; ++i) {
let name = fields[i].name, type = fields[i].type;
let value = record[name];
// Always allow null values
if (value === null || typeof value === 'undefined')
continue;
// Perform type-specific checks
if (type === 'C') {
if (typeof value !== 'string')
throw new Error('Expected a string for ' + name);
if (value.length > 255)
throw new Error('Text is too long (maximum length is 255 chars) for ' + name);
}
else if (type === 'N' || type === 'F' || type === 'I') {
if (typeof value !== 'number')
throw new Error('Expected a number for ' + name + ' but has ', value);
}
else if (type === 'D') {
if (!(value instanceof Date))
throw new Error('Expected a date for ' + name);
}
}
}