UNPKG

@conform-to/yup

Version:

Conform helpers for integrating with yup

194 lines (192 loc) 5.94 kB
function _parseStringFieldTests(def) { var constraint = {}; for (var test of def.tests) { switch (test.name) { case 'required': { constraint.required = true; break; } case 'min': { var _test$params; if (!constraint.minLength && (_test$params = test.params) !== null && _test$params !== void 0 && _test$params.min) { var _test$params2; constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min); } break; } case 'max': { var _test$params3; if (!constraint.maxLength && (_test$params3 = test.params) !== null && _test$params3 !== void 0 && _test$params3.max) { constraint.maxLength = Number(test.params.max); } break; } case 'matches': { var _test$params4; if (!constraint.pattern && ((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.regex) instanceof RegExp) { constraint.pattern = test.params.regex.source; } break; } } } if (!constraint.pattern && def.oneOf.length > 0) { constraint.pattern = def.oneOf.join('|'); } return constraint; } function _parseDateFieldTests(def) { var constraint = {}; for (var test of def.tests) { switch (test.name) { case 'required': constraint.required = true; break; case 'min': if (!constraint.min && test.params) { constraint.min = test.params.min; } break; case 'max': if (!constraint.max && test.params) { constraint.max = test.params.max; } break; } } return constraint; } function _parseNumberFieldTests(def) { var _test$params5, _test$params7; var constraint = {}; for (var test of def.tests) { switch (test.name) { case 'required': constraint.required = true; break; case 'min': if (typeof constraint.min === 'string') { throw new Error('min should not be a string'); } if (typeof constraint.min === 'object') { throw new Error('min should be a number'); } if (!constraint.min || constraint.min < Number((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.min)) { var _test$params6; constraint.min = Number((_test$params6 = test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min); } break; case 'max': if (typeof constraint.max === 'string') { throw new Error('max should not be a number'); } if (typeof constraint.max === 'object') { throw new Error('min should be a number'); } if (!constraint.max || constraint.max > Number((_test$params7 = test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.max)) { var _test$params8; constraint.max = Number((_test$params8 = test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max); } break; } } return constraint; } function _parseObjectFieldTests(def) { var constraint = {}; for (var test of def.tests) { switch (test.name) { case 'required': constraint.required = true; break; } } return constraint; } function _parseYupField(_ref) { var _test$params9, _test$params10; var [key, def] = _ref; var constraints = []; switch (def.type) { case 'object': { var { fields } = def; Object.entries(fields).forEach(_ref2 => { var [fieldKey, field] = _ref2; constraints = [...constraints, ..._parseYupField(["".concat(key, ".").concat(fieldKey), field])]; }); constraints.push([key, _parseObjectFieldTests(def)]); break; } case 'array': { var constraint = {}; constraint.multiple = true; for (var test of def.tests) { switch (test.name) { case 'required': constraint.required = true; break; case 'min': if (Number((_test$params9 = test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.min) > 1) { constraint.multiple = true; } break; case 'max': if (Number((_test$params10 = test.params) === null || _test$params10 === void 0 ? void 0 : _test$params10.max) > 1) { constraint.multiple = true; } break; } } var { innerType } = def; if (innerType) { switch (innerType.type) { case 'string': { constraints.push(["".concat(key, "[]"), _parseStringFieldTests(innerType)]); break; } case 'object': { constraints = constraints.concat(_parseYupField(["".concat(key, "[]"), innerType])); break; } } } constraints.push([key, constraint]); break; } case 'date': { constraints.push([key, _parseDateFieldTests(def)]); break; } case 'string': { constraints = [[key, _parseStringFieldTests(def)]]; break; } case 'number': { constraints = [[key, _parseNumberFieldTests(def)]]; break; } } return constraints; } function getYupConstraint(source) { var description = source.describe(); return Object.fromEntries(Object.entries(description.fields).reduce((acc, fieldEntry) => { return [...acc, ..._parseYupField(fieldEntry)]; }, [])); } export { getYupConstraint };