@hookform/resolvers
Version:
React Hook Form validation resolvers: Yup, Joi, Superstruct, Zod, Vest, Class Validator, io-ts, Nope, computed-types, TypeBox, arktype and Typanion
36 lines (29 loc) • 979 B
text/typescript
import { FieldError } from 'react-hook-form';
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
import { StructError, validate } from 'superstruct';
import { Resolver } from './types';
const parseErrorSchema = (error: StructError) =>
error.failures().reduce<Record<string, FieldError>>(
(previous, error) =>
(previous[error.path.join('.')] = {
message: error.message,
type: error.type,
}) && previous,
{},
);
export const superstructResolver: Resolver =
(schema, schemaOptions, resolverOptions = {}) =>
(values, _, options) => {
const result = validate(values, schema, schemaOptions);
if (result[0]) {
return {
values: {},
errors: toNestError(parseErrorSchema(result[0]), options),
};
}
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
return {
values: resolverOptions.raw ? values : result[1],
errors: {},
};
};