├── .gitignore ├── LICENSE ├── README.md ├── example ├── .editorconfig ├── .gitignore ├── .jshintrc ├── .npmignore ├── LICENSE ├── README.md ├── config │ ├── default.json │ └── production.json ├── package.json ├── public │ ├── favicon.ico │ └── index.html ├── src │ ├── app.js │ ├── hooks │ │ └── index.js │ ├── index.js │ ├── middleware │ │ ├── index.js │ │ ├── logger.js │ │ └── not-found-handler.js │ └── services │ │ ├── index.js │ │ └── message │ │ ├── hooks │ │ └── index.js │ │ └── index.js └── test │ ├── app.test.js │ └── services │ └── message │ └── index.test.js ├── lib └── validate │ └── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | _src 4 | _tests -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ## MIT License 2 | 3 | Copyright (C) 2016 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## feathers-validate-hook 2 | 3 | [![npm package](https://nodei.co/npm/feathers-validate-hook.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/feathers-validate-hook/) 4 | 5 | [![NPM version](http://img.shields.io/npm/v/feathers-validate-hook.svg)](https://www.npmjs.org/package/feathers-validate-hook) 6 | [![Dependency Status](https://david-dm.org/kulakowka/feathers-validate-hook.svg)](https://david-dm.org/kulakowka/feathers-validate-hook) 7 | 8 | 9 | This is experiment. **Work in progress!** 10 | 11 | Feathers hook for validate json-schema with [is-my-json-valid](https://www.npmjs.com/package/is-my-json-valid) 12 | 13 | ```javascript 14 | const validateHook = require('feathers-validate-hook') 15 | 16 | // Define schema 17 | const schema = { 18 | required: true, 19 | type: 'object', 20 | properties: { 21 | // Required attribute 'text' with type 'string' 22 | text: { 23 | required: true, 24 | type: 'string' 25 | } 26 | } 27 | } 28 | 29 | app.service('/messages').before({ 30 | create: [ 31 | validateHook(schema) 32 | ] 33 | }) 34 | ``` 35 | 36 | ## Example 37 | 38 | Look [example folder](https://github.com/kulakowka/feathers-validate-hook/tree/master/example) for more information. 39 | 40 | Test request: 41 | ``` 42 | curl -H "Accept: application/json" -X POST http://localhost:3030/messages 43 | ``` 44 | 45 | Server response example: 46 | ``` 47 | { 48 | "name": "BadRequest", 49 | "message": "Validation failed", 50 | "code": 400, 51 | "className": "bad-request", 52 | "data": {}, 53 | "errors": [ 54 | { 55 | "field": "data.text", 56 | "message": "is required", 57 | "type": "string" 58 | } 59 | ] 60 | } 61 | ``` 62 | -------------------------------------------------------------------------------- /example/.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | lib/ 31 | data/ 32 | -------------------------------------------------------------------------------- /example/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 2, 10 | "latedef": "nofunc", 11 | "newcap": false, 12 | "noarg": true, 13 | "quotmark": "single", 14 | "regexp": true, 15 | "undef": true, 16 | "unused": false, 17 | "strict": false, 18 | "trailing": true, 19 | "smarttabs": true, 20 | "white": false, 21 | "globals": { 22 | "it": true, 23 | "describe": true, 24 | "before": true, 25 | "beforeEach": true, 26 | "after": true, 27 | "afterEach": true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /example/.npmignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | data/ 31 | -------------------------------------------------------------------------------- /example/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Feathers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # example 2 | 3 | ```bash 4 | git clone git@github.com:kulakowka/feathers-validate-hook.git 5 | cd feathers-validate-hook 6 | npm install 7 | cd example 8 | npm install 9 | npm start 10 | ``` 11 | 12 | Test request: 13 | ``` 14 | curl -H "Accept: application/json" -X POST http://localhost:3030/messages 15 | ``` 16 | 17 | Server response example: 18 | ``` 19 | { 20 | "name": "BadRequest", 21 | "message": "Validation failed", 22 | "code": 400, 23 | "className": "bad-request", 24 | "data": {}, 25 | "errors": [ 26 | { 27 | "field": "data.text", 28 | "message": "is required", 29 | "type": "string" 30 | } 31 | ] 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /example/config/default.json: -------------------------------------------------------------------------------- 1 | { 2 | "host": "localhost", 3 | "port": 3030, 4 | "public": "../public/" 5 | } 6 | -------------------------------------------------------------------------------- /example/config/production.json: -------------------------------------------------------------------------------- 1 | { 2 | "host": "example-app.feathersjs.com", 3 | "port": 80, 4 | "public": "../public/" 5 | } 6 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "description": "", 4 | "version": "0.0.0", 5 | "homepage": "", 6 | "main": "src/", 7 | "keywords": [ 8 | "feathers" 9 | ], 10 | "license": "MIT", 11 | "repository": {}, 12 | "author": {}, 13 | "contributors": [], 14 | "bugs": {}, 15 | "engines": { 16 | "node": ">= 0.12.0" 17 | }, 18 | "scripts": { 19 | "start": "node --harmony --harmony_default_parameters src/", 20 | "jshint": "jshint src/. test/. --config", 21 | "mocha": "mocha test/ --recursive", 22 | "test": "npm run jshint && npm run mocha" 23 | }, 24 | "dependencies": { 25 | "body-parser": "^1.15.0", 26 | "compression": "^1.6.1", 27 | "cors": "^2.7.1", 28 | "feathers": "^2.0.0", 29 | "feathers-configuration": "^0.1.0", 30 | "feathers-errors": "^2.0.1", 31 | "feathers-hooks": "^1.0.0", 32 | "feathers-memory": "^0.6.3", 33 | "feathers-rest": "^1.0.0", 34 | "serve-favicon": "^2.3.0", 35 | "winston": "^2.2.0" 36 | }, 37 | "devDependencies": { 38 | "jshint": "^2.9.1", 39 | "mocha": "^2.4.5", 40 | "request": "^2.69.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kulakowka/feathers-validate-hook/4c31d3515c820a6fd4d225e7d2c55db974a55e0c/example/public/favicon.ico -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Welcome to Feathers 4 | 62 | 63 | 64 |
65 | 66 |

A minimalist real-time framework for tomorrow's apps.

67 | 68 | 71 |
72 | 73 | -------------------------------------------------------------------------------- /example/src/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const serveStatic = require('feathers').static; 5 | const favicon = require('serve-favicon'); 6 | const compress = require('compression'); 7 | const cors = require('cors'); 8 | const feathers = require('feathers'); 9 | const configuration = require('feathers-configuration'); 10 | const hooks = require('feathers-hooks'); 11 | const rest = require('feathers-rest'); 12 | const bodyParser = require('body-parser'); 13 | 14 | const middleware = require('./middleware'); 15 | const services = require('./services'); 16 | 17 | const app = feathers(); 18 | 19 | app.configure(configuration(path.join(__dirname, '..'))); 20 | 21 | app.use(compress()) 22 | .options('*', cors()) 23 | .use(cors()) 24 | .use(favicon( path.join(app.get('public'), 'favicon.ico') )) 25 | .use('/', serveStatic( app.get('public') )) 26 | .use(bodyParser.json()) 27 | .use(bodyParser.urlencoded({ extended: true })) 28 | .configure(hooks()) 29 | .configure(rest()) 30 | .configure(services) 31 | .configure(middleware); 32 | 33 | module.exports = app; 34 | -------------------------------------------------------------------------------- /example/src/hooks/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Add any common hooks you want to share across services in here. 4 | // 5 | // Below is an example of how a hook is written and exported. Please 6 | // see http://docs.feathersjs.com/hooks/readme.html for more details 7 | // on hooks. 8 | 9 | exports.myHook = function(options) { 10 | return function(hook) { 11 | console.log('My custom global hook ran. Feathers is awesome!'); 12 | }; 13 | }; 14 | -------------------------------------------------------------------------------- /example/src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const app = require('./app'); 4 | const port = app.get('port'); 5 | const server = app.listen(port); 6 | 7 | server.on('listening', () => 8 | console.log(`Feathers application started on ${app.get('host')}:${port}`) 9 | ); -------------------------------------------------------------------------------- /example/src/middleware/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const handler = require('feathers-errors/handler'); 4 | const notFound = require('./not-found-handler'); 5 | const logger = require('./logger'); 6 | 7 | module.exports = function() { 8 | // Add your custom middleware here. Remember, that 9 | // just like Express the order matters, so error 10 | // handling middleware should go last. 11 | const app = this; 12 | 13 | app.use(notFound()); 14 | app.use(logger(app)); 15 | app.use(handler()); 16 | }; 17 | -------------------------------------------------------------------------------- /example/src/middleware/logger.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const winston = require('winston'); 4 | 5 | module.exports = function(app) { 6 | // Add a logger to our app object for convenience 7 | app.logger = winston; 8 | 9 | return function(error, req, res, next) { 10 | if (error) { 11 | const message = `${error.code ? `(${error.code}) ` : '' }Route: ${req.url} - ${error.message}`; 12 | 13 | if (error.code === 404) { 14 | winston.info(message); 15 | } 16 | else { 17 | winston.error(message); 18 | winston.info(error.stack); 19 | } 20 | } 21 | 22 | next(error); 23 | }; 24 | }; 25 | -------------------------------------------------------------------------------- /example/src/middleware/not-found-handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const errors = require('feathers-errors'); 4 | 5 | module.exports = function() { 6 | return function(req, res, next) { 7 | next(new errors.NotFound('Page not found')); 8 | }; 9 | }; 10 | -------------------------------------------------------------------------------- /example/src/services/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | 4 | const message = require('./message'); 5 | 6 | 7 | module.exports = function() { 8 | const app = this; 9 | 10 | 11 | app.configure(message); 12 | }; 13 | -------------------------------------------------------------------------------- /example/src/services/message/hooks/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const globalHooks = require('../../../hooks'); 4 | const validateHook = require('../../../../../lib/validate') 5 | 6 | const schema = { 7 | required: true, 8 | type: 'object', 9 | properties: { 10 | text: { 11 | required: true, 12 | type: 'string' 13 | } 14 | } 15 | } 16 | 17 | exports.before = { 18 | all: [], 19 | find: [], 20 | get: [], 21 | create: [ 22 | validateHook(schema) 23 | ], 24 | update: [], 25 | patch: [], 26 | remove: [] 27 | }; 28 | 29 | exports.after = { 30 | all: [], 31 | find: [], 32 | get: [], 33 | create: [], 34 | update: [], 35 | patch: [], 36 | remove: [] 37 | }; 38 | -------------------------------------------------------------------------------- /example/src/services/message/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const hooks = require('./hooks'); 4 | 5 | class Service { 6 | constructor(options = {}) { 7 | this.options = options; 8 | } 9 | 10 | find(params) { 11 | return Promise.resolve([]); 12 | } 13 | 14 | get(id, params) { 15 | return Promise.resolve({ 16 | id, text: `A new message with ID: ${id}!` 17 | }); 18 | } 19 | 20 | create(data, params) { 21 | if(Array.isArray(data)) { 22 | return Promise.all(data.map(current => this.create(current))); 23 | } 24 | 25 | return Promise.resolve(data); 26 | } 27 | 28 | update(id, data, params) { 29 | return Promise.resolve(data); 30 | } 31 | 32 | patch(id, data, params) { 33 | return Promise.resolve(data); 34 | } 35 | 36 | remove(id, params) { 37 | return Promise.resolve({ id }); 38 | } 39 | } 40 | 41 | module.exports = function(){ 42 | const app = this; 43 | 44 | // Initialize our service with any options it requires 45 | app.use('/messages', new Service()); 46 | 47 | // Get our initialize service to that we can bind hooks 48 | const messageService = app.service('/messages'); 49 | 50 | // Set up our before hooks 51 | messageService.before(hooks.before); 52 | 53 | // Set up our after hooks 54 | messageService.after(hooks.after); 55 | }; 56 | 57 | module.exports.Service = Service; 58 | -------------------------------------------------------------------------------- /example/test/app.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const request = require('request'); 5 | const app = require('../src/app'); 6 | 7 | describe('Feathers application tests', () => { 8 | before(function(done) { 9 | this.server = app.listen(3030); 10 | this.server.once('listening', () => done()); 11 | }); 12 | 13 | after(function(done) { 14 | this.server.close(done); 15 | }); 16 | 17 | it('starts and shows the index page', done => { 18 | request('http://localhost:3030', (err, res, body) => { 19 | assert.ok(body.indexOf('') !== -1); 20 | done(err); 21 | }); 22 | }); 23 | 24 | describe('404', () => { 25 | it('shows a 404 HTML page', done => { 26 | request('http://localhost:3030/path/to/nowhere', (err, res, body) => { 27 | assert.equal(res.statusCode, 404); 28 | assert.ok(body.indexOf('') !== -1); 29 | done(err); 30 | }); 31 | }); 32 | 33 | it('shows a 404 JSON error without stack trace', done => { 34 | request({ 35 | url: 'http://localhost:3030/path/to/nowhere', 36 | json: true 37 | }, (err, res, body) => { 38 | assert.equal(res.statusCode, 404); 39 | assert.equal(body.code, 404); 40 | assert.equal(body.message, 'Page not found'); 41 | assert.equal(body.name, 'NotFound'); 42 | done(err); 43 | }); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /example/test/services/message/index.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const app = require('../../../src/app'); 5 | 6 | describe('message service', () => { 7 | it('registered the messages service', () => { 8 | assert.ok(app.service('messages')); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /lib/validate/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var validator = require('is-my-json-valid') 4 | 5 | var errors = require('feathers-errors') 6 | 7 | module.exports = function validateHook (schema, options) { 8 | options = Object.assign({ 9 | verbose: true, 10 | greedy: true 11 | }, options) 12 | 13 | return (hook) => { 14 | var validate = validator(schema, options) 15 | var valid = validate(hook.data) 16 | 17 | if (!valid) { 18 | let data = hook.data 19 | data.errors = validate.errors.map(errorsMap) 20 | throw new errors.BadRequest('Validation failed', data) 21 | } 22 | } 23 | } 24 | 25 | function errorsMap (error) { 26 | error.path = error.field.replace(/^data\./, '') 27 | delete error.field 28 | return error 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "feathers-validate-hook", 3 | "version": "0.0.7", 4 | "homepage": "http://feathersjs.com", 5 | "description": "Feathers hook for validate json-schema with is-my-json-valid", 6 | "repository": { 7 | "type": "git", 8 | "url": "git@github.com:kulakowka/feathers-validate-hook.git" 9 | }, 10 | "keywords": [ 11 | "feathers", 12 | "feathers-plugin", 13 | "validate", 14 | "validation" 15 | ], 16 | "main": "lib/validate/index.js", 17 | "author": "Feathers (http://feathersjs.com)", 18 | "contributors": [ 19 | "Anton Kulakov (http://kulakowka.com)" 20 | ], 21 | "directories": { 22 | "lib": "lib" 23 | }, 24 | "engines": { 25 | "node": ">= 5.6.0", 26 | "npm": ">= 3.6.0" 27 | }, 28 | "license": "MIT", 29 | "dependencies": { 30 | "feathers-errors": "^2.0.1", 31 | "is-my-json-valid": "^2.13.1" 32 | } 33 | } 34 | --------------------------------------------------------------------------------