UNPKG

@fesjs/fes-design

Version:
52 lines (50 loc) 1.89 kB
// wrap sync validator function wrapValidator(validator, async) { return function () { try { const validateResult = validator(...arguments); if (!async && (typeof validateResult === 'boolean' || validateResult instanceof Error || Array.isArray(validateResult)) // Error[] || typeof validateResult !== 'boolean' && validateResult !== null && validateResult !== void 0 && validateResult.then) { return validateResult; } if (typeof validateResult === 'undefined') { return true; } console.warn('form-item/validate', `You return a ${typeof validateResult} typed value in the validator method, which is not recommended. Please use ${async ? '`Promise`' : '`boolean`, `Error` or `Promise`'} typed value instead.`); return true; } catch (err) { console.warn('form-item/validate', 'An error is catched in the validation, ' + 'so the validation won\'t be done. Your callback in `validate` method of ' + '`form` or `form-item` won\'t be called in this validation.'); console.error(err); // If returns undefined, async-validator won't trigger callback // so the result will be abandoned, which means not true and not false return 'undefined'; } }; } function allPromiseFinish(promiseList) { if (!promiseList.length) { return Promise.resolve([]); } let hasError = false; let count = promiseList.length; const results = []; return new Promise((resolve, reject) => { promiseList.forEach((promise, index) => { promise.catch(e => { hasError = true; return e; }).then(result => { count -= 1; results[index] = result; if (count > 0) { return; } if (hasError) { reject(results); } resolve(results); }); }); }); } export { allPromiseFinish, wrapValidator };