@conform-to/dom
Version:
A set of opinionated helpers built on top of the Constraint Validation API
33 lines (30 loc) • 1.43 kB
JavaScript
import { formatPath } from './formdata.mjs';
import { isPlainObject } from './util.mjs';
function formatIssues(issues) {
var error = {
formErrors: null,
fieldErrors: {}
};
for (var issue of issues) {
var _issue$path$map, _issue$path;
var segments = (_issue$path$map = (_issue$path = issue.path) === null || _issue$path === void 0 ? void 0 : _issue$path.map(segment => {
var path = isPlainObject(segment) && 'key' in segment ? segment.key : segment;
if (typeof path !== 'string' && typeof path !== 'number') {
throw new Error("Only string or numeric path segment schemes are supported. Received segment: ".concat(String(path)));
}
return path;
})) !== null && _issue$path$map !== void 0 ? _issue$path$map : [];
var name = formatPath(segments !== null && segments !== void 0 ? segments : []);
if (!name) {
var _error$formErrors;
(_error$formErrors = error.formErrors) !== null && _error$formErrors !== void 0 ? _error$formErrors : error.formErrors = [];
error.formErrors.push(issue.message);
} else {
var _error$fieldErrors, _error$fieldErrors$na;
(_error$fieldErrors$na = (_error$fieldErrors = error.fieldErrors)[name]) !== null && _error$fieldErrors$na !== void 0 ? _error$fieldErrors$na : _error$fieldErrors[name] = [];
error.fieldErrors[name].push(issue.message);
}
}
return error;
}
export { formatIssues };