-
Notifications
You must be signed in to change notification settings - Fork 2
validator
jarnoux edited this page Dec 1, 2013
·
3 revisions
The validator middleware is Rig's default middleware for validating and sanitizing user inputs. It is using the node-validator library and applies sanitization and validation rules according to its given configuration.
"validator": {
"/register": { # the path of the incoming request
"email": { # the name of the parameter to filter
"sanitize": { # preprocess the parameter value
"trim": null
},
"assert": { # verify the preprocessed input is well-formed
"isEmail": null
},
"errorMessage": "Invalid Email" # message of the thrown error in case assert fails
}
},
"/login": {}
}
Each key of the config object is a route path. Its associated value lists the parameter names to sanitize. Each name has three properties:
-
sanitize
(optional): in a first step, the input will be passed through the methods passed to this object (thus is an order that is not guaranteed). Each key of that object is the name of the sanitization method you want to apply. You can find a list of those methods here. The associated value is an array of the arguments toapply
to that method or[]
if none is needed. -
assert
(optional): in a second step, the sanitized values are passed through assertions. Just likesanitize
, the keys are the names of the methods the input has to be verified against (you can find a list of those here). The value associated to a method name is a (possibly empty) array of arguments to apply to that method. -
errorMessage
(optional): if the assert failed, this is the message the thrown error will get. You can forward that error with a error handler to the client.