feathers-hooks-validator
v0.0.3
Published
This modules for the feathersjs framework.
Downloads
2
Maintainers
Readme
Feathers hooks validator
This repo modules for the feathersjs framework
Quick start
feathers-hooks-validator
allows to register hooks in before when a hook's executes validator. This makes it easy to check rule data processing and error handling from your service logic.
To install from npm, run:
$ npm i feathers-hooks-validator --save
Then, to use the plugin in your Feathers app:
const feathers = require('feathers');
const hooks = require('feathers-hooks');
const validator = require('feathers-hooks-validator');
const app = feathers();
app.configure(hooks());
// Set up after set up hooks
app.configure(validator());
Then, you can register a hook for a service:
// User service
const service = require('feathers-memory');
module.exports = function(){
const app = this;
let myHook = function(options) {
return
}
// Initialize our service
app.use('/users', service());
// Get our initialize service to that we can bind hooks
const userService = app.service('/users');
// Set up our before hook
userService.validator(
[
{
methods: ['create','update'],
rules: {
username: 'required|max:20',
password: 'required|max:20'
}
}
]
);
}
Documentation
Use package indicative
validator data
Structure validator
[
{
methods: ['create','update'],
useQuery: true,
rules: {
username: 'required|max:20',
password: 'required|max:20'
},
messages: {
required: '{{field}} is required to complete registeration process'
}
},
...
]
methods
: the methods using with check ruleuseQuery
: uses data on query inurl
when check rulerules
: rule was defined in packageindicative
messages
: custom message instead of a self-constructed message in packageindicative
Message Error Response
{
"name": "BadRequest",
"message": "Invalid data",
"code": 400,
"className": "bad-request",
"data": {},
"errors": [
{
"field": "password",
"validation": "required",
"message": "required validation failed on message"
}
]
}