nettofarah / property-validator

:white_check_mark: Easy property validation for JavaScript, Node and Express.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should allow label options also.

delveintechnolabs opened this issue · comments

if i do
presence('first_name')
in req.body and if first_name is not present then the validation message that is returned is
first_name required better option is to allow one more parameter called label and to generate the validation message, label should be used like
presence(param,label)
so in this case i can
presence('first_name','First Name')
so the validation message generated for this field would be
First Name is required
and this message could be directly shown to user in front end.

hi, @delveintechnolabs.
This is a good idea.

I'm not sure about how the API should look like though. It sounds counter intuitive to just pass in an extra param and assume that it is just a label, not a custom message.
It would also probably not work for every scenario, since every single validation function has its own pre-defined message that doesn't necessarily follows a standard template.

I would be more comfortable with something like this:

presence('first_name', 'You are required to provide your first name')

I'm not really sure if this is the API a want though, because it is also not obvious in the API that you can provide a custom message for validations.

I think another good API would look a bit like this: (inspired by the optional API)

validateBody(req, [
  customMessage(presence('first_name'), 'Oops, you forgot to tell us your name')
])

But I'm also not really sure about this, since it could end up making things extra verbose (assuming you'll end up doing this for every field in the end)

In the meantime though, you can get away with creating your own custom validation functions (that's the beauty of property-validators current design).

You can write a function like this: (using your label idea)

function awesomePresence(propertyName, label) {
  return function(params) {
     var isParamPresent = !!params[propertyName]
     return { 
       result: isParamPresent,
       message: label + ' is required.',
       field: propertyName
     }
  }
} 

You can even reuse the existing presence helper if you want:

function smarterPresence(propertyName, label) {
  return function(params) {
     var result = presence(propertyName)(params)
     result.message =  label + ' is required.'
     return result
  }
} 

Hope this helps!

Hi @nettofarah ,

Thanks for your thoughts. I do agree with some of the points. But to provide meaningful & human friendly validation message is what should be there in a validation module/plugin/API

If you wish i can explain further in detail my idea of more precise & friendly validation concept where you wont need to call two different validation functions like
presence('amount') & isDecimal('amount')

All could be done is one single call to our API.

Thanks,
Hannan

Hi, @delveintechnolabs.
Thanks for your feedback.

I'm curious as to what your suggestions are

hey, @delveintechnolabs. Just released a new version of the lib that might solve your problem.

Hi @nettofarah ,

Thanks for the update and sorry i could not find enough time to express my thoughts in detail. But i will surely do that shortly(the thing is that i wanted to make my idea loud & clear so needs too much typing, hopefully ill fiind some time soon and express my thoughts)

Many thanks