@conform-to/dom
Version:
A set of opinionated helpers built on top of the Constraint Validation API
37 lines (32 loc) • 1.52 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var formdata = require('./formdata.js');
var util = require('./util.js');
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 = util.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 = formdata.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;
}
exports.formatIssues = formatIssues;