Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

generic-joi-validator

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

generic-joi-validator

Uses Joi schema to validate objects, can be used with express and koa routes, may be used with other frameworks.

  • 1.1.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
2
Weekly downloads
 
Created
Source

Generic-joi-validator Logo

Deprecated

This package is no longer maintained.

Generic Joi Validator

Build Status codecov dependencies Status devDependencies Status

Installation

npm install generic-joi-validator

Description

This package facilitates managing multiple Joi structures and running validation on them.

Current translators

mongoose-to-joi-translator

More are coming soon. Got a new translator? Create an issue and I'll include it.

Usage

Requiring methods from the package.

const { schemata, prepare } = require('generic-joi-validator');

Add domain models to your schemata object. You may use your mongoose database models directly with mongoose-to-joi-translator.

const getJoiSchema = require('mongoose-to-joi-translator');
schemata.stores = getJoiSchema(new Schema({
    name: {
        type: String,
        required: true
    },
    location: new Schema({
        latitude: {
            type: String,
            required: true
        },
        longitude: {
            type: String,
            required: true
        }
    })
}));

Add your schema manually

schemata.stores = {
    name: Joi.string().required(),
    location: {
        latitude: Joi.string().required(),
        longitude: Joi.string().required()
    }
};

Other domain models may also be included Examples:

  • schemata.storesQuery to contain what is queryable in stores and validates user input based on it.
  • schemata.pagination to contain the pagination structure.
schemata.pagination = {
    skip: Joi.number().optional().default(defaultSkip),
    limit: Joi.number().optional().default(defaultLimit)
};

An example of how to use this package with Koa

Create middleware

// You may also create middlewares for getQuery input, getPagination input, etc.

/**
* Koa-specific Wrapper for the prepare function, looks for data in the body, params, and query
* of ctx.request and assign the validated data to ctx.state.data
* @param {String} [params] list of strings in space-separated value format, example 'word location.latitude location.longitude'.
* By default it will check all properties found in the specified joi structure.
* @param {Boolean} [areOptional] specifies whether the attributes are optional
* (overrides required check) useful for partial update validation
*/
const koaValidator = (params, areOptional) => (ctx, next) => {
    // takes foo from '/foo/something/another'
    const resourceName = ctx.url.replace(/^\/([^/]*).*$/, '$1');
    // source the object the validator will look through
    const source = {
        ...ctx.request.body,
        ...ctx.params,
        ...ctx.request.query
    };
    const {error, value} = prepare(resourceName, source, params, areOptional);
    ctx.assert(!error, 400, {message: error ? error.message : error});
    ctx.state.data = {...ctx.state.data, ...value};
    return next();
};

Use in your routes

router.post(
    '/stores',
    koaValidator(),
    async (ctx, next) => {
        ctx.body = ctx.state.data;
        return next();
    }
);

Test

npm test

Documentation

Members

schemata

property that contains all schemata to check against, the property isJoi in each schema is reserved.

Functions

prepare(resourceName, sourceObject, [attributes], [areOptional])Object

validates the object based on the schema of the resource

schemata

property that contains all schemata to check against, the property isJoi in each schema is reserved.

Kind: global variable

prepare(resourceName, sourceObject, [attributes], [areOptional]) ⇒ Object

validates the object based on the schema of the resource

Kind: global function
Returns: Object - same as Joi.valdidate return value

ParamTypeDescription
resourceNameStringresource name
sourceObjectObjectlocation of attributes to validate
[attributes]Stringlist of attributes to be checked in space-separated value format, defaults to all.
[areOptional]Booleanspecifies whether the attributes specified are optional

Keywords

FAQs

Package last updated on 21 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc