0.0.3 • Published 7 years ago

feathers-hooks-validator v0.0.3

Weekly downloads
1
License
ISC
Repository
github
Last release
7 years ago

Feathers hooks validator

Build Status Coverage Status Code Climate

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 rule
  • useQuery: uses data on query in url when check rule
  • rules: rule was defined in package indicative
  • messages: custom message instead of a self-constructed message in package indicative

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"
        }
    ]
}