UNPKG

generator-begcode

Version:

Spring Boot + Angular/React/Vue in one handy generator

27 lines (26 loc) 875 B
export default class Validator { objectType; fieldsToCheck; constructor(objectType, fieldsToCheck) { this.objectType = objectType; this.fieldsToCheck = fieldsToCheck; } validate(object, _options) { if (!object) { throw new Error(`No ${this.objectType}.`); } checkForAbsentAttributes(this, object); } } function checkForAbsentAttributes(validator, object) { const absentAttributes = []; validator.fieldsToCheck.forEach(attribute => { if (!object[attribute]) { absentAttributes.push(attribute); } }); if (absentAttributes.length !== 0) { const plural = absentAttributes.length > 1; throw new Error(`The ${validator.objectType} attribute${plural ? 's' : ''} ${absentAttributes.join(', ')} ${plural ? 'were not' : 'was not'} found.`); } }