Skip to content

Commit

Permalink
fix(yupResolver): prevent reduce to throw error (#300)
Browse files Browse the repository at this point in the history
Related to #299

* prevent reduce to throw error

* prevent reduce to throw error
  • Loading branch information
bluebill1049 authored Dec 3, 2021
1 parent 96c19f0 commit 8a938cc
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions yup/src/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,31 @@ const parseErrorSchema = (
error: Yup.ValidationError,
validateAllFieldCriteria: boolean,
) => {
return error.inner.reduce<Record<string, FieldError>>((previous, error) => {
if (!previous[error.path!]) {
previous[error.path!] = { message: error.message, type: error.type! };
}
return (error.inner || []).reduce<Record<string, FieldError>>(
(previous, error) => {
if (!previous[error.path!]) {
previous[error.path!] = { message: error.message, type: error.type! };
}

if (validateAllFieldCriteria) {
const types = previous[error.path!].types;
const messages = types && types[error.type!];
if (validateAllFieldCriteria) {
const types = previous[error.path!].types;
const messages = types && types[error.type!];

previous[error.path!] = appendErrors(
error.path!,
validateAllFieldCriteria,
previous,
error.type!,
messages
? ([] as string[]).concat(messages as string[], error.message)
: error.message,
) as FieldError;
}
previous[error.path!] = appendErrors(
error.path!,
validateAllFieldCriteria,
previous,
error.type!,
messages
? ([] as string[]).concat(messages as string[], error.message)
: error.message,
) as FieldError;
}

return previous;
}, {});
return previous;
},
{},
);
};

export const yupResolver: Resolver =
Expand Down

0 comments on commit 8a938cc

Please sign in to comment.