Giter VIP home page Giter VIP logo

joi-json's Introduction

Build Status npm version

Joi-JSON

Creates Joi based object schemas from JSON.

Features

  • Create Joi schemas from JSON data
  • Express simple schemas using a single string
  • Works with LOV
  • Lightweight with minimal dependencies
  • Compatible with most of the Joi API
  • Node.js 6.10.0 compatible for use in AWS Lambda environments

Installation

Install via npm.

npm install joi-json --save

Note: joi or lov should also be installed

Getting Started

'use strict';

const builder = require( 'joi-json' ).builder();

let jsonSchema = {

    firstName: 'string:min=1,max=60,required',  // string using string-based notation

    lastName: { // string using object notation

        '@type': 'string',
        min: 1,
        max: 60,
        required: true
    },

    address: {  // address is an object (i.e. joi.object() )

        street: 'string:min=1,max=80,required',
        street2: 'string:min=1,max=80',
        city: 'string:min=1,max=40,required',
        state: 'string:min=1,max=40,required',
        postal: 'string:min=1,max=20,required',

        '@required': true   // needs the '@' to indicate that "required" is a property
    },

    // alternative values (i.e. joi.alternatives().try() )
    favNumberOrWord: [

        'string:min=1,max=10',
        'number:min=0,max=100'
    ]
};

let schema = builder.build( jsonSchema );

Which would yield the equivalent to the following joi schema:

const joi = require( 'joi' );

let schema = {

    firstName: joi.string().min(1).max(60).trim().required(),

    lastName: joi.string().min(1).max(60).trim().required(),

    address: Object.keys( {

            street: joi.string().min(1).max(80).trim().required(),
            street2: joi.string().min(1).max(80).trim(),
            city: joi.string().min(1).max(40).trim().required(),
            state: joi.string().min(1).max(40).trim().required(),
            postal: joi.string().min(1).max(20).trim().required()

        }).required(),

    favNumberOrWord: [

            joi.string().min(1).max(10).trim(),
            joi.number().min(1).max(100)
        ]
};

Documentation

For information on how to use Joi-JSON, please see our API documentation

Feedback

We'd love to get feedback on how to make this tool better. Feel free to contact us at [email protected]

License

BSD-3-Clause

joi-json's People

Contributors

richardhyatt avatar chrinor2002 avatar

Watchers

James Cloos 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.