@conform-to/yup
Version:
Conform helpers for integrating with yup
43 lines (40 loc) • 1.31 kB
JavaScript
import { parse } from '@conform-to/dom';
import * as yup from 'yup';
export { getYupConstraint } from './constraint.mjs';
function parseWithYup(payload, config) {
return parse(payload, {
resolve(payload, intent) {
var schema = typeof config.schema === 'function' ? config.schema(intent) : config.schema;
var resolveData = value => ({
value
});
var resolveError = error => {
if (error instanceof yup.ValidationError) {
return {
error: error.inner.reduce((result, e) => {
var _e$path, _result$name;
var name = (_e$path = e.path) !== null && _e$path !== void 0 ? _e$path : '';
result[name] = [...((_result$name = result[name]) !== null && _result$name !== void 0 ? _result$name : []), e.message];
return result;
}, {})
};
}
throw error;
};
if (!config.async) {
try {
var data = schema.validateSync(payload, {
abortEarly: false
});
return resolveData(data);
} catch (error) {
return resolveError(error);
}
}
return schema.validate(payload, {
abortEarly: false
}).then(resolveData).catch(resolveError);
}
});
}
export { parseWithYup };