UNPKG

simple-body-validator

Version:

This package is inspired by Laravel validation, and aims to make body validation easier for Javascript developers

29 lines (28 loc) 805 B
'use strict'; import RuleContract from './ruleContract'; class ClosureValidationRule extends RuleContract { constructor(callback) { super(); /** * Indicates if the validation callback failed. */ this.failed = false; this.callback = callback; } /** * Determine if the validation rule passes. */ passes(value, attribute) { this.failed = false; const result = this.callback(value, function (message) { this.failed = true; this.message = message; }.bind(this), attribute); if (result instanceof Promise) { return result.then(() => !this.failed); } return !this.failed; } ; } export default ClosureValidationRule;