-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (107 loc) · 3.03 KB
/
index.js
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
130
131
132
133
134
135
import Field from './Field';
const defaultOptions = {
justRenderAfterValidate: true,
defaultMessage: '',
displayErrorCallback: text => {
return text;
}
};
export default class SimpleFormValidation {
constructor(params) {
//TODO: Add tests to 'justRenderAfterValidate'
this.options = { ...defaultOptions, ...params };
this.fields = [];
this.errors = {};
this._validateHasRan = false;
}
canRender() {
if (!this.options.justRenderAfterValidate) return true;
return this.options.justRenderAfterValidate && this._validateHasRan;
}
reset() {
this.fields = [];
this._validateHasRan = false;
this.resetErrors();
}
addError(stateKeyName, msg) {
this.errors[stateKeyName] = msg;
}
resetErrors() {
this.errors = {};
}
isValid() {
return !this.hasErrors();
}
hasErrors() {
return Object.keys(this.errors).length > 0;
}
renderAllErrors() {
if (!this.canRender()) return null;
const errorList = [];
for (var key in this.errors) {
errorList.push(this.options.displayErrorCallback(this.errors[key]));
}
return errorList;
}
renderError(field, value) {
if (!this.canRender()) return null;
if (typeof this.fields[field] === 'undefined') return null;
this.fields[field].run(value);
if (this.fields[field].hasError()) {
const errorMessage = this.fields[field].getMessage();
this.addError(field, errorMessage);
if (errorMessage == '') {
console.warn(`Empty validator message to '${field}'.`);
}
return this.options.displayErrorCallback(errorMessage);
} else {
delete this.errors[field];
return null;
}
}
/**
*
* @param {object} obj Object with the values that fields must be setted.
* @param {boolean} reset If true, the the fields that are not found on obj, will be setted as null. Default true.
*/
setValues(obj, reset = true) {
for (let k in this.fields) {
if (typeof obj[k] === 'undefined') {
if (reset) {
this.fields[k].setValue(null);
}
} else {
this.fields[k].setValue(obj[k]);
}
}
}
field(fieldName, reset = false) {
if (reset || typeof this.fields[fieldName] === 'undefined') {
this.fields[fieldName] = new Field(this.options.defaultMessage);
}
return this.fields[fieldName];
}
validate(state) {
return new Promise((resolve, reject) => {
if (this.validateSync(state)) {
resolve();
} else {
throw this.errors;
}
});
}
validateSync(state) {
this._validateHasRan = true;
this.resetErrors();
if (typeof state !== 'undefined') {
this.setValues(state);
}
for (var key in this.fields) {
const result = this.fields[key].run();
if (result !== true) {
this.addError(key, this.fields[key].validator.getMessage());
}
}
return !this.hasErrors();
}
}