Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

joi-json

vandium-io17.5kBSD-3-Clause4.0.0

Builds Joi schemas from JSON objects

validation, joi, json, schema, validator

readme

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
  • Lightweight with minimal dependencies
  • Compatible with most of the Joi API
  • Node.js 10.x compatible for use in AWS Lambda environments
  • Supports @hapi/joi implementation

Installation

Install via npm.

npm install joi-json --save

Note: @hapi/joi needs to be installed into your project

Getting Started

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( '@hapi/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 `feedback@vandium.io`

License

BSD-3-Clause

changelog

Change Log

4.0.0 (2019-12-04)

Breaking:

  • Support current version of Joi under @hapi organization. This version will no longer support the previous joi package. Be sure to include a dependency of @hapi/joi in your project dependencies.

3.0.0 (2019-11-04)

Updated:

  • Support for allow. Thanks @fabiogomessilva
  • Removed support for lov

Internal:

  • Code coverage now uses nyc
  • Updated dependencies
  • Refactored tests
  • Tests now build with node 10.x and use Joi 14.0.0

2.1.0 (2018-09-26)

Updated:

  • Removed support for lov
  • Support for allow. Thanks @fabiogomessilva

Changed:

  • Tests now build with node 10.x and use Joi 14.0.0

Internal:

  • Code coverage now uses nyc
  • Updated test packages

2.0.1 (2016-08-03)

Added:

  • uuid, guid, and email types

Changed:

  • string instances have trim property set by default
  • type property changed to @type

1.1.0 (2016-07-26)

Added:

  • Parser for parsing individual strings to bypass the builder

Updated:

  • vandium-utils dependency to 1.1.0

1.0.1 (2016-07-25)

Fixed:

  • Typos

1.0.0 (2016-06-22)

Initial release