Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 1x 474x 474x 474x 474x 474x 474x 57x 475x 1x 474x 69x 68x 68x 14x 68x 406x 406x 406x 43x 406x 521x 467x 1x | 'use strict';
/*
* adonis-validator
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const indicative = require('indicative');
/**
* Validation class to validate data with a rules
* schema.
*
* @class Validation
* @constructor
*/
class Validation {
constructor(data, rules, messages, formatter) {
this._data = data;
this._rules = rules;
this._messages = messages;
this._formatter = formatter;
this._errorMessages = null;
this._executed = false;
}
/**
* Sets the error as a property on instance.
*
* @method _useErrors
*
* @param {Array} errors
*
* @return {void}
*/
_useErrors(errors) {
this._errorMessages = errors;
}
/**
* Marks the validation as executed, also makes sure
* that not re-executing the validations
*
* @method _markAsExecuted
*
* @return {void}
*/
_markAsExecuted() {
/**
* Throw exception when re-running the validation for
* multiple times
*/
if (this._executed) {
throw new Error('Cannot re-run validations on same data and rules');
}
this._executed = true;
}
/**
* Run validation on data using defined rules
*
* @method run
*
* @return {this}
*/
async run() {
this._markAsExecuted();
try {
await indicative.validate(this._data, this._rules, this._messages, this._formatter);
} catch (error) {
this._useErrors(error);
}
return this;
}
/**
* Run all validations, regardless of failures. The `run`
* method on the opposite side stops at the first
* validation
*
* @method runAll
*
* @return {this}
*/
async runAll() {
this._markAsExecuted();
try {
await indicative.validateAll(this._data, this._rules, this._messages, this._formatter);
} catch (error) {
this._useErrors(error);
}
return this;
}
/**
* Returns an array of validation messages
* or null, if there are no errors
*
* @method messages
*
* @return {Array|Null}
*/
messages() {
return this._errorMessages;
}
/**
* Returns a boolean indicating if there are
* validation errors
*
* @method fails
*
* @return {Boolean}
*/
fails() {
return !!this.messages();
}
}
module.exports = Validation;
|