New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-to-joi-translator

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-to-joi-translator

Extracts schema from mongoose and generates an equivilent JOI schema.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by25%
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-to-joi-translator Build Status codecov

This project aims at reducing the amount of work needed when validation is required for the database and another location, e.g. API. It also aims at unifying the way validation errors are handled. This is a proof of concept that works only on mongoose's validation.

Prerequisites

  • node >= 8.9.1
  • npm >= 5

Supported validations

  1. All types
    • required
    • valid (enum validation)
  2. Strings
    • min
  3. Arrays
    • items (element types)
  4. Numbers
  5. Objects
  6. Dates
  7. ObjectID

Deeply nested document validation is supported, i.e. Objects within Objects, Arrays within Objects etc.

Testing

npm test

Usage

const getJoiSchema = require('mongoose-to-joi-translator');
const joiSchema = getJoiSchema(new Schema({ word: { type: String } }));
const { error, value } = Joi.validate({ word: 'hello' }, joiSchema);

Usage when combined with generic-joi-validator

generic-joi-validator

const { JoiValidator } = require('generic-joi-validator');
const joiValidator = new JoiValidator();

// Use a translator to extract Joi schema from your database
const mongoose = require('mongoose');
const { Schema } = mongoose;
const getJoiSchema = require('mongoose-to-joi-translator');
joiValidator.schemata.stores = getJoiSchema(new Schema({
    name: {
        type: String,
        required: true
    },
    location: new Schema({
        latitude: {
            type: String,
            required: true
        },
        longitude: {
            type: String,
            required: true
        }
    })
}));


// or add your schema manually
joiValidator.schemata.stores = {
    name: Joi.string().required(),
    location: {
        latitude: Joi.string().required(),
        longitude: Joi.string().required()
    }
};

// With koa
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');

const router = new Router();
const app = new Koa();

app.use(bodyParser());

const koaValidator = async (ctx, next) => {
    const { error, value } = joiValidator.prepare(ctx.url.substr(ctx.url.lastIndexOf('/') + 1), ctx.request.body);
    ctx.assert(!error, 400, value);
    ctx.state.data = value;
    return next();
};

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

app.use(router.allowedMethods({ throw: true }));
app.use(router.routes());

app.listen(3000);

FAQs

Package last updated on 05 Dec 2017

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