raju
raju

Reputation: 4978

How does assert (req.assert) work in nodejs

I am currently working on MEAN stack using node, express and angularjs. I downloaded boiler plate code from mean.io and also using debugger while I explore the code.

In the controller which gets req and res as parameters, how does req.assert work?

In the file server/controllers/users.js

req.assert('username', 'Username cannot be more than 20 characters').len(1,20);

adds into validation error even when the username is empty or null. How do I check for current username value in the req? Where is the assert function of req defined.

I come from java background and find it tricky to find the function code some times as I wont be sure about where is it defined and how is it prototyped. How does one properly read the objects and browse the functions that are being used in javascript?

Upvotes: 12

Views: 14607

Answers (2)

Steve
Steve

Reputation: 4975

Accepted answer refers to what is now the Legacy API hence broken links.

Check out the documentation for Sanitization and the available sanitizing tools. You can make custom validators in there.

Upvotes: 1

Matt
Matt

Reputation: 1038

It's defined in Express's dependency express-validator. Check here: https://github.com/ctavan/express-validator/blob/master/lib/express_validator.js

which depends on validator: https://github.com/chriso/validator.js

Upvotes: 10

Related Questions