diff options
Diffstat (limited to 'src/helpers/validation-helpers.js')
-rw-r--r-- | src/helpers/validation-helpers.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/helpers/validation-helpers.js b/src/helpers/validation-helpers.js new file mode 100644 index 000000000..eeb12cab7 --- /dev/null +++ b/src/helpers/validation-helpers.js | |||
@@ -0,0 +1,48 @@ | |||
1 | export function required({ field }) { | ||
2 | const isValid = (field.value.trim() !== ''); | ||
3 | return [isValid, `${field.label} is required`]; | ||
4 | } | ||
5 | |||
6 | export function email({ field }) { | ||
7 | const value = field.value.trim(); | ||
8 | let isValid = false; | ||
9 | |||
10 | if (value !== '') { | ||
11 | isValid = Boolean(value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}/i)); | ||
12 | } else { | ||
13 | isValid = true; | ||
14 | } | ||
15 | |||
16 | return [isValid, `${field.label} is not a valid email address`]; | ||
17 | } | ||
18 | |||
19 | export function url({ field }) { | ||
20 | const value = field.value.trim(); | ||
21 | let isValid = false; | ||
22 | |||
23 | if (value !== '') { | ||
24 | // eslint-disable-next-line | ||
25 | isValid = Boolean(value.match(/(^|[\s.:;?\-\]<(])(https?:\/\/[-\w;/?:@&=+$|_.!~*|'()[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',|().:;?\-[\]>)])/i)); | ||
26 | } else { | ||
27 | isValid = true; | ||
28 | } | ||
29 | |||
30 | return [isValid, `${field.label} is not a valid url`]; | ||
31 | } | ||
32 | |||
33 | export function minLength(length) { | ||
34 | return ({ field }) => { | ||
35 | let isValid = true; | ||
36 | if (field.touched) { | ||
37 | isValid = field.value.length >= length; | ||
38 | } | ||
39 | return [isValid, `${field.label} should be at least ${length} characters long.`]; | ||
40 | }; | ||
41 | } | ||
42 | |||
43 | export function oneRequired(targets) { | ||
44 | return ({ field, form }) => { | ||
45 | const invalidFields = targets.filter(target => form.$(target).value === ''); | ||
46 | return [targets.length !== invalidFields.length, `${field.label} is required`]; | ||
47 | }; | ||
48 | } | ||