node.jsmongodbvalidationexpressexpress-validator

white space issue in isAlpha() function of express-validator


I am using express-validator in my project my json from the client is

{"name": "john doe"}

my express validation code is

[check('name', 'invalid name').isAlpha()]

why this code is returning invalid name while this is a string. Also I tried isString() but it is also not working it is working in the same style as isAlpha(). Error json response to the client is

{
"errors": [
    {
        "value": "john doe",
        "msg": "invalid name",
        "param": "name",
        "location": "body"
    }
]
}

does isAlpha() function consider only one word as a string

How can I fix this


Solution

  • I got the answer. I used custom validation method. It resolved my issue.

    [check('name').custom((value,{req})=>{
                    if(isNaN(value)){
                        return true;
                    }else{
                        throw new Error('invalid name')
                    }
     })]