Skip to content

Instantly share code, notes, and snippets.

@navarroaxel
Created April 3, 2018 18:56
Show Gist options
  • Save navarroaxel/47ce6d194881102d2e5d3fb817c2128d to your computer and use it in GitHub Desktop.
Save navarroaxel/47ce6d194881102d2e5d3fb817c2128d to your computer and use it in GitHub Desktop.
Uncaught TypeError: Class constructor Validator cannot be invoked without 'new'
export default class MegaValidator extends Validator {
constructor(validationLogic) {
super();
if (!isFunction(validationLogic)) {
throw new Error('Validator must have a method for validating.');
}
this.validationLogic = validationLogic;
}
forThis(entity) {
this.entity = entity;
return this;
}
using(...requiredQuestions) {
this.requiredQuestions = requiredQuestions;
return this;
}
isValid() {
return every(
getValidators(this.validationLogic, this.requiredQuestions, this.entity),
validation => validation.validator.isValid(validation.value)
);
}
}
export default class Validator {
constructor(arg) {
this._arg = arg;
this.withErrorMessage('Validation failed');
}
get arg() {
return this._arg;
}
errorMessage() {
return this._errorMessage;
}
withErrorMessage(errorMessage) {
this._errorMessage = errorMessage;
return this;
}
forThis() {
return this;
}
validate(obj) {
if (!this.isValid(obj)) {
throw new Error(this.errorMessage());
}
}
isValid(obj) {
throw new Error('Must be implemented in subclass');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment