UNPKG

simple-body-validator

Version:

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

33 lines (32 loc) 1.05 kB
'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ruleContract_1 = __importDefault(require("./ruleContract")); class ClosureValidationRule extends ruleContract_1.default { 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; } ; } exports.default = ClosureValidationRule;