UNPKG

1.31 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('./Validator');
7
8module.exports = class RequiredValidator extends Base {
9
10 constructor (config) {
11 super({
12 requiredValue: null,
13 strict: false,
14 skip: false, // skip validation
15 trimming: true,
16 ...config
17 });
18 this.skipOnEmpty = false;
19 }
20
21 getMessage () {
22 return this.createMessage(this.message, 'Value cannot be blank');
23 }
24
25 getRequiredMessage () {
26 return this.createMessage(this.message, 'Value must be "{requiredValue}"', {
27 requiredValue: this.requiredValue
28 });
29 }
30
31 validateValue (value) {
32 if (this.skip) {
33 return;
34 }
35 if (this.requiredValue === null) {
36 value = typeof value === 'string' && this.trimming ? value.trim() : value;
37 if (this.strict ? value !== null : !this.isEmptyValue(value)) {
38 return;
39 }
40 return this.getMessage();
41 }
42 if (this.strict ? value === this.requiredValue : value == this.requiredValue) {
43 return;
44 }
45 return this.getRequiredMessage();
46 }
47};
\No newline at end of file