Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yupResolver): multiple errors messages with same type #156

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions yup/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ export const schema = yup.object({
username: yup.string().matches(/^\w+$/).min(3).max(30).required(),
password: yup
.string()
.matches(/^[a-zA-Z0-9]{3,30}/)
.required(),
.matches(new RegExp('.*[A-Z].*'), 'One uppercase character')
.matches(new RegExp('.*[a-z].*'), 'One lowercase character')
.matches(new RegExp('.*\\d.*'), 'One number')
.matches(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
'One special character',
)
.min(8, 'Must be at least 8 characters in length')
.required('New Password is required'),
repeatPassword: yup.ref('password'),
accessToken: yup.string(),
birthYear: yup.number().min(1900).max(2013),
Expand All @@ -23,8 +30,8 @@ export const schema = yup.object({

export const validData: yup.InferType<typeof schema> = {
username: 'Doe',
password: 'Password123',
repeatPassword: 'Password123',
password: 'Password123_',
repeatPassword: 'Password123_',
birthYear: 2000,
email: 'john@doe.com',
tags: ['tag1', 'tag2'],
Expand Down
22 changes: 16 additions & 6 deletions yup/src/__tests__/__snapshots__/yup.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Object {
},
],
"password": Object {
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
"message": "One uppercase character",
"ref": Object {
"name": "password",
},
Expand Down Expand Up @@ -64,7 +64,7 @@ Object {
},
],
"password": Object {
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
"message": "One uppercase character",
"ref": Object {
"name": "password",
},
Expand Down Expand Up @@ -114,13 +114,18 @@ Object {
},
],
"password": Object {
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
"message": "One uppercase character",
"ref": Object {
"name": "password",
},
"type": "matches",
"types": Object {
"matches": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
"matches": Array [
"One uppercase character",
"One lowercase character",
"One number",
],
"min": "Must be at least 8 characters in length",
},
},
"username": Object {
Expand Down Expand Up @@ -170,13 +175,18 @@ Object {
},
],
"password": Object {
"message": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
"message": "One uppercase character",
"ref": Object {
"name": "password",
},
"type": "matches",
"types": Object {
"matches": "password must match the following: \\"/^[a-zA-Z0-9]{3,30}/\\"",
"matches": Array [
"One uppercase character",
"One lowercase character",
"One number",
],
"min": "Must be at least 8 characters in length",
},
},
"username": Object {
Expand Down
7 changes: 6 additions & 1 deletion yup/src/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ const parseErrorSchema = (
}

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

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

Expand Down