Code Monkey home page Code Monkey logo

kuzzle-plugin-schema-validator's Introduction

Kuzzle data validation plugin

     ▄▄▄▄▄      ▄███▄      ▄▄▄▄
  ▄█████████▄▄█████████▄▄████████▄
 ██████████████████████████████████
  ▀██████████████████████████████▀
   ▄███████████████████████████▄
 ▄███████████████████████████████▄
▀█████████████████████████████████▀
  ▀██▀        ▀██████▀       ▀██▀
         ██     ████    ██
               ▄████▄
               ▀████▀
                 ▀▀

Simple data validation plugin for Kuzzle back-end. It's verifying input data before writing it to database or publishing. It is based on Joi schemas.

Installation

kuzzle plugins --install --npmVersion x.y.z kuzzle-plugin-schema-validator

Config

Schemas keys are collection names and values are object with path to Joi validator scheme. options will be passed to Joi validate().

{
  "schemas": {
    "users": {
      "path": "/absolute/path/to/schemas/user",
      "options": {"abortEarly": false}
    },
    "posts": {
      "path": "relative/path/to/nodejs/working/dir/post",
      "options": {"context": {"defaultAuthor": "NoNaMe"}},
      "activated": false
    }
  }
}

Importing config:

kuzzle plugins --importConfig config.json kuzzle-plugin-schema-validator

Simple schema

const Joi = require('joi');

module.exports = Joi.object().keys({
  title: Joi.string().required(),
  text: Joi.string().required(),
  likes: Joi.number().integer().min(0)
});

Schema with context generator

{
  "schemas": {
    "posts": {
      "path": "schemas/post",
      "options": {"context": {"defaultAuthor": "NoNaMe"}}
    }
  }
}
const Joi = require('joi');


const Schema = Joi.object().keys({
  title: Joi.string().required(),
  text: Joi.string().required(),
  author: Joi.string().default(Joi.ref('$defaultAuthor'))
});

Schema.getContext = (request, pluginContext) => {
  const repositories = pluginContext.accessors.kuzzle.repositories;
  
  const token = getUserToken(request.headers);
  if (!token) return Promise.resolve();

  return repositories.token.verifyToken(token)
    .then(tokenData => repositories.user.load(tokenData.userId))
    .then(user => ({defaultAuthor: user.username}));
};


function getUserToken(headers) {
  if (!headers || !headers.authorization) {
    return null;
  }

  const res = /^Bearer (.+)$/.exec(headers.authorization);
  if (!res) return null;

  const token = res[1].trim();
  return token || null;
}


module.exports = Schema;

F.A.Q

Can I use custom validation?

Yes, check Joi.extend() method. Some ready validators can be find on npm.

Changelog

0.2.0

  • getContext() to provide dynamic schema data

0.1.0

  • schemas are loading at plugin init now (optimization)
  • activated schema config option; true by default
  • throwing BadRequestError if validation failed

0.0.2

Initial version

kuzzle-plugin-schema-validator's People

Contributors

farwayer avatar

Watchers

 avatar

Forkers

filsh

kuzzle-plugin-schema-validator's Issues

Using class syntax

Hi,

i would like to know how are you achieving to use class syntax, i'm getting errors with kuzzle...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.