RocksonZeta / koa-validate

validate koa request params and format request params
https://github.com/RocksonZeta/koa-validate
MIT License
284 stars 32 forks source link

Check is string #27

Open tonyxiao opened 7 years ago

tonyxiao commented 7 years ago

how do you check a body parameter is actually string, rather than number or null or object?

mikiebrenbren commented 7 years ago

use the 'typeof' operator, if it is not a string you can push an error on to the errors array (create one if it does not exist) which is on the context.

should look similar to this if (typeof someValue !== 'string'){ context.errors = context.errors || [] context.errors.push({notString:Field must be a string!}) }

tonyxiao commented 7 years ago

I see. I would have expected / hoped for something built in for this. But ok :)

manuel-di-iorio commented 6 years ago

+1 for isString() validator