Neah/node_modules/@hookform/resolvers/io-ts/src/io-ts.ts
2025-04-17 12:39:10 +02:00

33 lines
849 B
TypeScript

import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import * as Either from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import errorsToRecord from './errorsToRecord';
import { Resolver } from './types';
export const ioTsResolver: Resolver = (codec) => (values, _context, options) =>
pipe(
values,
codec.decode,
Either.mapLeft(
errorsToRecord(
!options.shouldUseNativeValidation && options.criteriaMode === 'all',
),
),
Either.mapLeft((errors) => toNestErrors(errors, options)),
Either.fold(
(errors) => ({
values: {},
errors,
}),
(values) => {
options.shouldUseNativeValidation &&
validateFieldsNatively({}, options);
return {
values,
errors: {},
} as any;
},
),
);