-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.js
More file actions
executable file
·70 lines (55 loc) · 1.59 KB
/
actions.js
File metadata and controls
executable file
·70 lines (55 loc) · 1.59 KB
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
export const REGISTERED_FORM_ELEMENT = 'REGISTERED_FORM_ELEMENT';
export const UNREGISTERED_FORM_ELEMENT = 'UNREGISTERED_FORM_ELEMENT';
export const FORM_ELEMENT_UPDATED = 'FORM_ELEMENT_UPDATED';
export const FORM_VALIDATION_RESULT = 'FORM_VALIDATION_RESULT';
export const register = (id, rules) => ({
type: REGISTERED_FORM_ELEMENT, id, rules
});
export const unregister = id => ({
type: UNREGISTERED_FORM_ELEMENT, id
});
export const updateValue = (id, value) => ({
type: FORM_ELEMENT_UPDATED, id, value
});
export const setInvalid = invalid => ({
type: FORM_VALIDATION_RESULT, invalid
});
export const isFunction = x => typeof x === 'function';
export const validateComponent = (comp, value) => {
let valid = true;
let rule = null;
let actualRule = null;
const { rules } = comp;
if (!rules || rules.length === 0) return { valid };
if (rules) {
for (let i = 0; i < rules.length; i += 1) {
rule = rules[i];
if (isFunction(rule)) {
actualRule = rule();
} else {
actualRule = rule;
}
if (
(value === undefined || value === null)
&& !actualRule.handlesNull
) {
break;
}
const result = !!actualRule.validate(value);
valid = !!result;
if (!valid) {
break;
}
}
}
const hint = !valid ? actualRule.hint : null;
let errorMessage = null;
if (hint) {
if (isFunction(hint)) {
errorMessage = hint(value);
} else {
errorMessage = hint;
}
}
return { valid, errorMessage };
};