Skip to content

dszczyt/enjoi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

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

API

  • enjoi(schema [, options])
    • schema - a JSON schema.
    • 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);
});

About

Converts a JSON schema to a Joi schema.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%