UNPKG

789 BJavaScriptView 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 CheckboxValidator extends Base {
9
10 constructor (config) {
11 super({
12 skipOnEmpty: false,
13 ...config
14 });
15 }
16
17 getMessage () {
18 return this.createMessage(this.message, 'Invalid value');
19 }
20
21 validateAttr (attr, model) {
22 const value = model.get(attr);
23 if (typeof value === 'boolean' ) {
24 return true;
25 }
26 const trueValue = value === 'true' || value === 'on';
27 if (value && value !== 'false' && !trueValue) {
28 return this.getMessage();
29 }
30 model.set(attr, trueValue);
31 }
32};
\No newline at end of file