Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xtrinch/sails-hook-validation-ev
Sails 1.x validation hook using express-validate functions
https://github.com/xtrinch/sails-hook-validation-ev
sails sails-hook-validation sails-validation validation
Last synced: 2 months ago
JSON representation
Sails 1.x validation hook using express-validate functions
- Host: GitHub
- URL: https://github.com/xtrinch/sails-hook-validation-ev
- Owner: xtrinch
- License: mit
- Created: 2018-09-03T08:46:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-03T13:40:59.000Z (over 6 years ago)
- Last Synced: 2024-05-02T00:04:53.575Z (8 months ago)
- Topics: sails, sails-hook-validation, sails-validation, validation
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![npm version](https://badge.fury.io/js/sails-hook-validation-ev.svg)](https://badge.fury.io/js/sails-hook-validation-ev)
[![npm](https://img.shields.io/npm/dt/sails-hook-validation-ev.svg)]()# sails-hook-validation-ev
## About
Sails 1.x hook for validating REST requests with the help of [express-validator](https://github.com/express-validator/express-validator). Because - why reinvent the wheel? Uses the same configuration functions as `express-validator`. Supports validators, sanitizers, localization, ...
## Installation
As simple as `npm install --save sails-hook-validation-ev`.
## Usage with blueprints
For models with blueprint routes you wish to validate, you need only add a `validate` function to it.
### Example model
`Todo.js:`
module.exports = {
attributes: {
title: {
type: 'string',
required: true
},
description: {
type: 'string',
required: true
},
},validate: (req) => {
req.check('title')
.exists()
.isLength({ min: 1 }).withMessage('must be at least 5 chars long');
req.check('description').exists();
}};
For info on which functions you can use, see [express-validator check API](https://express-validator.github.io/docs/check-api.html). It opens a world of possibilities :)
### Example `POST /todo` response
{
"errors": [
{
"location": "params",
"param": "title",
"msg": "Invalid value"
},
{
"location": "params",
"param": "title",
"msg": "must be at least 5 chars long"
}
]
}## Usage with custom handlers
### Example `TodoController.js`
The following snippet will override default /POST blueprint handler with custom function. In overridden functions you hold the responsibility to validate requests.
#### Option 1: Validate via validate function in model
`TodoController.js:`
var validate = require('sails-hook-validation-ev/lib/validate')
module.exports = {
create: async function(req, res) {
validate(req)
const errors = await req.getValidationResult();
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
return res.ok()
}
};#### Option 2: Provide a custom validate function
`TodoController.js:`
var validate = require('sails-hook-validation-ev/lib/validate')
module.exports = {
create: async function(req, res) {
validate(req, (req) => {
req.check('title')
.exists()
.isLength({ min: 1 }).withMessage('must be at least 5 chars long');
req.check('description').exists();
})const errors = await req.getValidationResult();
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
return res.ok()
}
};