npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generic-joi-validator

v1.1.9

Published

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

Downloads

25

Readme

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

Functions

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

| Param | Type | Description | | --- | --- | --- | | resourceName | String | resource name | | sourceObject | Object | location of attributes to validate | | [attributes] | String | list of attributes to be checked in space-separated value format, defaults to all. | | [areOptional] | Boolean | specifies whether the attributes specified are optional |