UNPKG

verificator

Version:

Client and server-side validation JavaScript library

75 lines (74 loc) 2.44 kB
import get from '../utils/get'; import isPlainObject from '../utils/isPlainObject'; import { SET_RULES } from '../constants/types'; const parseParameters = (rule, parameter) => { if (Array.isArray(parameter)) { return parameter; } if (rule.toLowerCase() === 'regex') { return [parameter]; } return parameter.split(','); }; const parseRule = (rule) => { if (Array.isArray(rule)) { return { name: rule[0].trim(), parameters: rule.slice(1), }; } const name = rule.indexOf(':') > -1 ? rule.substr(0, rule.indexOf(':')) : rule; const parameter = rule.indexOf(':') > -1 ? rule.substr(rule.indexOf(':') + 1) : []; const parameters = parseParameters(name, parameter); return { name: name.trim(), parameters, }; }; const parseRules = (rules) => { if (typeof rules === 'string') { rules = rules.split('|'); } return rules.filter(rule => (Array.isArray(rule) || typeof rule === 'string') && rule.length).map(parseRule); }; export const setRules = (data, initialRules) => { const _rules = {}; const _attributes = {}; function explodeRules(attribute, rule) { if (attribute.indexOf('*') > -1) { const path = attribute.substr(0, attribute.indexOf('*') - 1); const value = get(data, path); if (value) { if (Array.isArray(value)) { value.forEach((v, i) => { explodeRules(attribute.replace('*', String(i)), rule); }); } else if (isPlainObject(value)) { Object.keys(value).forEach(key => { explodeRules(attribute.replace('*', key), rule); }); } } } else { const [name, rules] = rule; _rules[attribute] = parseRules(rules); if (name.indexOf('*') > -1) { _attributes[name] = _attributes[name] || []; _attributes[name].push(attribute); } } } Object.keys(initialRules).forEach(attribute => { explodeRules(attribute, [attribute, initialRules[attribute]]); }); return { type: SET_RULES, payload: { initialRules, rules: _rules, implicitAttributes: _attributes, }, }; };