knicola / yup-password

Yup, dead simple password validation.
MIT License
48 stars 6 forks source link

password() message #7

Closed kgrigorian closed 2 years ago

kgrigorian commented 2 years ago

.password() method does not take message argument (other method do have it)

knicola commented 2 years ago

the .password() is mostly a convenience method, if you will, with reasonable defaults. it doesn't make much sense to me to add options to customize its behavior when you can either chain the rule you want to customize (ie .password().minLowercase(1, 'custom message') or replace .password() completely with the rules you think is best for your use case.

however, if more people would like this option I will not object to it.

two variations come to mind:

// 1st option
.password({
  messages: {
    minLowercase: 'custom message',
    minUppercase: 'custom message',
    ...
  },
  rules: {
    minLowercase: 2,
    minUppercase: 3,
    ...
  },
})
// 2nd option
.password({
  minLowercase: [2, 'custom message'],
  minUppercase: [2, 'custom message'],
  ..
})

also lets compare it to this:

.password().minLowercase(2, 'custom message').minUppercase(3, 'custom message')

additional ideas are welcome

knicola commented 2 years ago

closing this ticket for now. if anyone thinks this is a useful option to have and/or have any additional ideas please drop them here and will reopen the ticket for discussion.

MadhawaS96 commented 1 week ago

Just a tiny issue, maybe I missed this in the docs. Say I have a field name as "currentPassword". The returned error message would be displayed as ie: "currentPassword must contain at least 1 uppercase letter".

Do you have any option to change the name set into the error message? I hope the description explains the issue clearly.

Let me know if you prefer this to be opened as a new issue. Tagged here since I thought this can be related to the current topic.

Thanks!

knicola commented 1 week ago

@MadhawaS96 you can use Schema.label()

const schema = yup.object().shape({
    currentPassword: yup.string().password().required().label('Current password'),
})
MadhawaS96 commented 1 week ago

@MadhawaS96 you can use Schema.label()

const schema = yup.object().shape({
    currentPassword: yup.string().password().required().label('Current password'),
})

Brilliant! Thank you so much for both the answer and your quick response. Much appreciated!