@conform-to/yup
Advanced tools
Comparing version 0.3.1 to 0.4.0-pre.0
213
index.js
@@ -5,95 +5,15 @@ 'use strict'; | ||
var dom = require('@conform-to/dom'); | ||
var yup = require('yup'); | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
} | ||
var yup__namespace = /*#__PURE__*/_interopNamespace(yup); | ||
function formatError(error) { | ||
var result = {}; | ||
var _loop = function _loop(validationError) { | ||
dom.setValue(result, dom.getPaths(validationError.path).flatMap(path => ['details', path]).concat('message'), prev => prev ? prev : validationError.message); | ||
}; | ||
for (var validationError of error.inner) { | ||
_loop(validationError); | ||
} | ||
return result; | ||
} | ||
function resolve(source) { | ||
function getFieldsetConstraint(source) { | ||
var description = source.describe(); | ||
return { | ||
source, | ||
constraint: Object.fromEntries(Object.entries(description.fields).map(_ref => { | ||
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8; | ||
return Object.fromEntries(Object.entries(description.fields).map(_ref => { | ||
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8; | ||
var [key, def] = _ref; | ||
var constraint = {}; | ||
var [key, def] = _ref; | ||
var constraint = {}; | ||
switch (def.type) { | ||
case 'string': | ||
{ | ||
for (var test of def.tests) { | ||
switch (test.name) { | ||
case 'required': | ||
constraint.required = true; | ||
break; | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? 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': | ||
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) { | ||
var _test$params4; | ||
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max); | ||
} | ||
break; | ||
case 'matches': | ||
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) { | ||
constraint.pattern = test.params.regex.source; | ||
} | ||
break; | ||
} | ||
} | ||
if (!constraint.pattern && def.oneOf.length > 0) { | ||
constraint.pattern = def.oneOf.join('|'); | ||
} | ||
break; | ||
} | ||
case 'number': | ||
for (var _test of def.tests) { | ||
switch (_test.name) { | ||
switch (def.type) { | ||
case 'string': | ||
{ | ||
for (var test of def.tests) { | ||
switch (test.name) { | ||
case 'required': | ||
@@ -104,6 +24,6 @@ constraint.required = true; | ||
case 'min': | ||
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) { | ||
var _test$params7; | ||
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) { | ||
var _test$params2; | ||
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min); | ||
constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min); | ||
} | ||
@@ -114,77 +34,76 @@ | ||
case 'max': | ||
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) { | ||
var _test$params9; | ||
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) { | ||
var _test$params4; | ||
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max); | ||
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max); | ||
} | ||
break; | ||
case 'matches': | ||
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) { | ||
constraint.pattern = test.params.regex.source; | ||
} | ||
break; | ||
} | ||
} | ||
if (!constraint.pattern && def.oneOf.length > 0) { | ||
constraint.pattern = def.oneOf.join('|'); | ||
} | ||
break; | ||
} | ||
} | ||
return [key, constraint]; | ||
})), | ||
case 'number': | ||
for (var _test of def.tests) { | ||
switch (_test.name) { | ||
case 'required': | ||
constraint.required = true; | ||
break; | ||
validate(form, submitter) { | ||
var payload = dom.getFormData(form, submitter); | ||
var submission = dom.createSubmission(payload); | ||
var errors = []; | ||
case 'min': | ||
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) { | ||
var _test$params7; | ||
try { | ||
source.validateSync(submission.form.value, { | ||
abortEarly: false | ||
}); | ||
} catch (e) { | ||
if (e instanceof yup__namespace.ValidationError) { | ||
errors.push(...e.inner.map(error => { | ||
var _error$path; | ||
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min); | ||
} | ||
return [(_error$path = error.path) !== null && _error$path !== void 0 ? _error$path : '', error.message]; | ||
})); | ||
} else { | ||
throw e; | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) { | ||
var _test$params9; | ||
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
dom.setFormError(form, errors); | ||
}, | ||
break; | ||
} | ||
parse(payload) { | ||
var submission = dom.createSubmission(payload); | ||
return [key, constraint]; | ||
})); | ||
} | ||
function getError(error, scope) { | ||
var _error$inner$reduce; | ||
if (submission.state !== 'accepted') { | ||
return submission; | ||
} | ||
return (_error$inner$reduce = error === null || error === void 0 ? void 0 : error.inner.reduce((result, e) => { | ||
var _e$path; | ||
try { | ||
var result = source.validateSync(submission.form.value, { | ||
abortEarly: false | ||
}); | ||
return { | ||
state: 'accepted', | ||
data: result, | ||
form: submission.form | ||
}; | ||
} catch (error) { | ||
if (error instanceof yup__namespace.ValidationError) { | ||
return { | ||
state: 'rejected', | ||
form: { | ||
// @ts-expect-error | ||
value: submission.form.value, | ||
error: formatError(error) | ||
} | ||
}; | ||
} | ||
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : ''; | ||
throw error; | ||
} | ||
if (!scope || scope.includes(name)) { | ||
result.push([name, e.message]); | ||
} | ||
}; | ||
return result; | ||
}, [])) !== null && _error$inner$reduce !== void 0 ? _error$inner$reduce : []; | ||
} | ||
exports.resolve = resolve; | ||
exports.getError = getError; | ||
exports.getFieldsetConstraint = getFieldsetConstraint; |
@@ -1,74 +0,14 @@ | ||
import { getFormData, createSubmission, setFormError, setValue, getPaths } from '@conform-to/dom'; | ||
import * as yup from 'yup'; | ||
function formatError(error) { | ||
var result = {}; | ||
var _loop = function _loop(validationError) { | ||
setValue(result, getPaths(validationError.path).flatMap(path => ['details', path]).concat('message'), prev => prev ? prev : validationError.message); | ||
}; | ||
for (var validationError of error.inner) { | ||
_loop(validationError); | ||
} | ||
return result; | ||
} | ||
function resolve(source) { | ||
function getFieldsetConstraint(source) { | ||
var description = source.describe(); | ||
return { | ||
source, | ||
constraint: Object.fromEntries(Object.entries(description.fields).map(_ref => { | ||
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8; | ||
return Object.fromEntries(Object.entries(description.fields).map(_ref => { | ||
var _test$params, _test$params3, _test$params5, _test$params6, _test$params8; | ||
var [key, def] = _ref; | ||
var constraint = {}; | ||
var [key, def] = _ref; | ||
var constraint = {}; | ||
switch (def.type) { | ||
case 'string': | ||
{ | ||
for (var test of def.tests) { | ||
switch (test.name) { | ||
case 'required': | ||
constraint.required = true; | ||
break; | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? 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': | ||
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) { | ||
var _test$params4; | ||
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max); | ||
} | ||
break; | ||
case 'matches': | ||
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) { | ||
constraint.pattern = test.params.regex.source; | ||
} | ||
break; | ||
} | ||
} | ||
if (!constraint.pattern && def.oneOf.length > 0) { | ||
constraint.pattern = def.oneOf.join('|'); | ||
} | ||
break; | ||
} | ||
case 'number': | ||
for (var _test of def.tests) { | ||
switch (_test.name) { | ||
switch (def.type) { | ||
case 'string': | ||
{ | ||
for (var test of def.tests) { | ||
switch (test.name) { | ||
case 'required': | ||
@@ -79,6 +19,6 @@ constraint.required = true; | ||
case 'min': | ||
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) { | ||
var _test$params7; | ||
if (!constraint.minLength || constraint.minLength < Number((_test$params = test.params) === null || _test$params === void 0 ? void 0 : _test$params.min)) { | ||
var _test$params2; | ||
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min); | ||
constraint.minLength = Number((_test$params2 = test.params) === null || _test$params2 === void 0 ? void 0 : _test$params2.min); | ||
} | ||
@@ -89,77 +29,75 @@ | ||
case 'max': | ||
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) { | ||
var _test$params9; | ||
if (!constraint.maxLength || constraint.maxLength > Number((_test$params3 = test.params) === null || _test$params3 === void 0 ? void 0 : _test$params3.max)) { | ||
var _test$params4; | ||
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max); | ||
constraint.maxLength = Number((_test$params4 = test.params) === null || _test$params4 === void 0 ? void 0 : _test$params4.max); | ||
} | ||
break; | ||
case 'matches': | ||
if (!constraint.pattern && ((_test$params5 = test.params) === null || _test$params5 === void 0 ? void 0 : _test$params5.regex) instanceof RegExp) { | ||
constraint.pattern = test.params.regex.source; | ||
} | ||
break; | ||
} | ||
} | ||
if (!constraint.pattern && def.oneOf.length > 0) { | ||
constraint.pattern = def.oneOf.join('|'); | ||
} | ||
break; | ||
} | ||
} | ||
return [key, constraint]; | ||
})), | ||
case 'number': | ||
for (var _test of def.tests) { | ||
switch (_test.name) { | ||
case 'required': | ||
constraint.required = true; | ||
break; | ||
validate(form, submitter) { | ||
var payload = getFormData(form, submitter); | ||
var submission = createSubmission(payload); | ||
var errors = []; | ||
case 'min': | ||
if (!constraint.min || constraint.min < Number((_test$params6 = _test.params) === null || _test$params6 === void 0 ? void 0 : _test$params6.min)) { | ||
var _test$params7; | ||
try { | ||
source.validateSync(submission.form.value, { | ||
abortEarly: false | ||
}); | ||
} catch (e) { | ||
if (e instanceof yup.ValidationError) { | ||
errors.push(...e.inner.map(error => { | ||
var _error$path; | ||
constraint.min = Number((_test$params7 = _test.params) === null || _test$params7 === void 0 ? void 0 : _test$params7.min); | ||
} | ||
return [(_error$path = error.path) !== null && _error$path !== void 0 ? _error$path : '', error.message]; | ||
})); | ||
} else { | ||
throw e; | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > Number((_test$params8 = _test.params) === null || _test$params8 === void 0 ? void 0 : _test$params8.max)) { | ||
var _test$params9; | ||
constraint.max = Number((_test$params9 = _test.params) === null || _test$params9 === void 0 ? void 0 : _test$params9.max); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
setFormError(form, errors); | ||
}, | ||
break; | ||
} | ||
parse(payload) { | ||
var submission = createSubmission(payload); | ||
return [key, constraint]; | ||
})); | ||
} | ||
function getError(error, scope) { | ||
var _error$inner$reduce; | ||
if (submission.state !== 'accepted') { | ||
return submission; | ||
} | ||
return (_error$inner$reduce = error === null || error === void 0 ? void 0 : error.inner.reduce((result, e) => { | ||
var _e$path; | ||
try { | ||
var result = source.validateSync(submission.form.value, { | ||
abortEarly: false | ||
}); | ||
return { | ||
state: 'accepted', | ||
data: result, | ||
form: submission.form | ||
}; | ||
} catch (error) { | ||
if (error instanceof yup.ValidationError) { | ||
return { | ||
state: 'rejected', | ||
form: { | ||
// @ts-expect-error | ||
value: submission.form.value, | ||
error: formatError(error) | ||
} | ||
}; | ||
} | ||
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : ''; | ||
throw error; | ||
} | ||
if (!scope || scope.includes(name)) { | ||
result.push([name, e.message]); | ||
} | ||
}; | ||
return result; | ||
}, [])) !== null && _error$inner$reduce !== void 0 ? _error$inner$reduce : []; | ||
} | ||
export { resolve }; | ||
export { getError, getFieldsetConstraint }; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.3.1", | ||
"version": "0.4.0-pre.0", | ||
"main": "index.js", | ||
@@ -18,3 +18,3 @@ "module": "module/index.js", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.3.1", | ||
"@conform-to/dom": "0.4.0-pre.0", | ||
"yup": ">=0.32.0" | ||
@@ -21,0 +21,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13353
226