UNPKG

@ismail424/svelte-formly

Version:

<p align="center"> <img width="100%" height="300" src="./logo.png" alt="Svelte Formly" /> </p>

58 lines (57 loc) 1.89 kB
import * as CoreRules from './rules/index'; const _coreRules = CoreRules; /** * Validate field by rule. * @param {configs field} field */ export async function validate(_field) { let { value, rules } = _field; let valid = true; let rule; let errors = []; let validation; if (rules) { await Promise.all(rules.map(async (validator) => { // For file type. if (validator === 'file') { if (value) { Object.keys(value).map((i) => { if (_field.file) { Object.entries(_field.file).map(([key, val]) => { valid = _coreRules[key].call(null, value[i], val); if (!valid) { errors = [...errors, key]; } }); } }); } } else { // For custom rule. if (typeof validator === 'function') { valid = await validator.call; rule = validator.name; } else if (typeof validator === 'object') { valid = await validator.fnc(); rule = validator.name; } else { const args = validator.split(/:/g); rule = args.shift(); valid = _coreRules[rule].call(null, value, args); } if (!valid) { errors = [...errors, rule]; } } })); validation = { errors, dirty: errors.length > 0 }; } else { validation = { errors, dirty: false }; } _field.validation = validation; return _field; }