Created
April 3, 2018 18:56
-
-
Save navarroaxel/47ce6d194881102d2e5d3fb817c2128d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Uncaught TypeError: Class constructor Validator cannot be invoked without 'new' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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