Git Product home page Git Product logo

enjoi's Introduction

Build Status NPM version

enjoi

Converts a JSON schema to a Joi schema for object validation.

Schema Support

enjoi is built against json-schema v4, but does not support all of json-schema (yet).

Here is a list of some missing keyword support still being worked on:

  • not
  • format
  • object:patternProperties
  • array:items (supports as single schema, not supported as array of schemas).
  • array:additionalItems

API

  • enjoi(schema [, options])
    • schema - a JSON schema or a string type representation (such as 'integer').
    • options - an (optional) object of additional options such as subSchemas and custom types.
      • subSchemas - an (optional) object with keys representing schema ids, and values representing schemas.
      • types - an (optional) object with keys representing type names and values representing a Joi schema.

Example:

var Joi = require('joi');
var Enjoi = require('enjoi');

var schema = Enjoi({
    'title': 'Example Schema',
    'type': 'object',
    'properties': {
        'firstName': {
            'type': 'string'
        },
        'lastName': {
            'type': 'string'
        },
        'age': {
            'description': 'Age in years',
            'type': 'integer',
            'minimum': 0
        }
    },
    'required': ['firstName', 'lastName']
});

Joi.validate({firstName: 'John', lastName: 'Doe', age: 45}, schema, function (error, value) {
    error && console.log(error);
});

Can also call validate directly on the created schema.

schema.validate({firstName: 'John', lastName: 'Doe', age: 45}, function (error, value) {
    error && console.log(error);
});

Sub Schemas

Example:

var schema = Enjoi({
    'title': 'Example Schema',
    'type': 'object',
    'properties': {
        'A': {
            '$ref': 'sub#/something'
        }
    }
}, {
    subSchemas: {
        'sub': {
            'something': {
                'type': 'string'
            }
        }
    }
});

Custom Types

var schema = Enjoi({
    type: 'file'
}, {
    types: {
        file: Enjoi({
            type: 'object',
            properties: {
                file: {
                    type: 'string'
                },
                consumes: {
                    type: 'string',
                    pattern: /multipart\/form-data/
                }
            }
        })
    }
});

schema.validate({file: 'data', consumes: 'multipart/form-data'}, function (error, value) {
    error && console.log(error);
});

enjoi's People

Contributors

bardzusny avatar jojow avatar justinmchase avatar normpng avatar pibi avatar tlivings avatar vh avatar

Watchers

 avatar  avatar

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.