HOW TO USE
Middleware Approach
To use this package as a middleware, follow the process below
- Create a new file within your middleware folder or modify an already existing middleware.
- Import the library.
- Create a new instance of
octaValidate
and pass in the form id
as the first argument then any configuration option as the second argument.
//import library
const octaValidate = require('octavalidate-nodejs')
//syntax
//const validate = new octaValidate('my_form', CONFIG_OPTIONS)
const config = {
strictMode : true,
strictWords : ["admin", "fake", "empty", "null"]
}
//create new instance
const validate = new octaValidate('my_form', config)
Now define validation rules for that particular form.
If the form has a file upload field, define separate rules for that field.
Here's the syntax to define validation rules
//syntax
const fieldRules = {
FIELD_NAME : {
RULE_TITLE : ERROR_MESSAGE
}
}
Here's an example
//define valiation rules for field names
const fieldRules = {
email : {
'R' : "Your Email Address is required",
'EMAIL' : "This Email is invalid"
},
username : {
'R' : "Your username is required",
'USERNAME' : "Your username contains invalid characters"
}
}
Begin validation on the form fields by invoking the method validateFields()
.
The method validateFields()
takes in 2 arguments
- The first argument is the validation rules on the form fields
- The second argument is the form fields to validate.
module.exports = (req, res, next) => {
try{
//req.body contains the form fields
//check if validation is successful
if ( validate.validateFields(fieldRules, req.body) ) {
//process form data here
return res.status(200).json({
message: "Form validation successful"
})
}else{
//validation error, now return the errors
return res.status(400).json({
message: "Form validation failed",
formErrors: JSON.stringify(validate.getErrors())
})
}
}catch(err){
//handle error
console.log(err)
}
//move on to the next middleware
next()
}
That's it! It is that simple.
Validating an uploaded File
Now, what if you want to validate an uploaded file, how would you achieve that?
To validate an uploaded file, invoke the validateFiles()
method.
This method accepts 2 arguments;
- The first argument is the validation rules on the file upload fields
- The second argument is the file upload fields to validate.
//validation rules for file upload
const fileRules = {
profile : {
'R' : "Please upload a profile picture",
'ACCEPT' : ["image/png", "File type is not supported"],
'MAXSIZE' : ["5MB", "Image file should not be greater than 5MB"]
}
}
module.exports = (req, res, next) => {
try{
//req.files contains the file upload fields with the help of multer package
//check if validation is successful
if ( validate.validateFiles(fieldRules, req.files) ) {
//process form data here
return res.status(200).json({
message: "Form validation successful"
})
}else{
//validation error, now return the errors
return res.status(400).json({
message: "Form validation failed",
formErrors: JSON.stringify(validate.getErrors())
})
}
}catch(err){
//handle error
console.log(err)
}
//move on to the next middleware
next()
}
Other Approach
In your server file
, you can also validate your form fields by following the process below;
- Import the library.
- Define validation rules for form fields
- Invoke the
validateFields()
method to begin validation.
//define validation rules for field names
const fieldRules = {
email : {
'R' : "Your Email Address is required",
'EMAIL' : "This Email is invalid"
},
username : {
'R' : "Your username is required",
'USERNAME' : "Your username contains invalid characters"
}
}
app.post('auth/register', (req, res) => {
try{
if ( validate.validateFields(fieldRules, req.body) ) {
//process form data here
return res.status(200).json({
message: "Form validation successful"
})
}else{
//validation error, now return the errors
return res.status(400).json({
message: "Form validation failed",
formErrors: JSON.stringify(validate.getErrors())
})
}
}catch(e){
console.log(e)
}
})