|
| 1 | +// @flow |
| 2 | +// $FlowFixMe |
1 | 3 | import validator from 'validator';
|
| 4 | +import type { ValidationRules, SanitizingRules } from './types'; |
2 | 5 |
|
3 | 6 | const GENERIC_ERROR_MESSAGE = 'Invalid input';
|
4 | 7 |
|
5 |
| -export function checkRules(rules = {}, value) { |
| 8 | +export function checkRules(rules: ValidationRules = {}, value: string) { |
6 | 9 | const errors = [];
|
7 | 10 | let success = true;
|
8 | 11 | Object.keys(rules)
|
9 | 12 | .forEach((rule) => {
|
10 | 13 | const validatorFn = validator[rule];
|
11 | 14 | const config = rules[rule];
|
12 |
| - const { options, errorMessage = GENERIC_ERROR_MESSAGE } = config; |
| 15 | + const options = (typeof config !== 'boolean') && config.options; |
| 16 | + const errorMessage = (typeof config !== 'boolean') ? config.errorMessage : GENERIC_ERROR_MESSAGE; |
13 | 17 | success = validatorFn(value, options);
|
14 | 18 | if (!success) errors.push({ errorMessage, value });
|
15 | 19 | });
|
16 | 20 |
|
17 | 21 | return { success, errors };
|
18 | 22 | }
|
19 | 23 |
|
20 |
| -export function applySanitizers(sanitizers = {}, value) { |
| 24 | +export function applySanitizers(sanitizers: SanitizingRules = {}, value: mixed): mixed { |
21 | 25 | return Object.keys(sanitizers)
|
22 | 26 | .reduce((result, sanitizer) => {
|
23 | 27 | const sanitizerFn = validator[sanitizer];
|
24 |
| - const options = sanitizers[sanitizer]; |
| 28 | + const config = sanitizers[sanitizer]; |
| 29 | + const options = (typeof config !== 'boolean') && config.options; |
25 | 30 | return sanitizerFn(result, options);
|
26 | 31 | }, value) || value;
|
27 | 32 | }
|
0 commit comments