Coder Social home page Coder Social logo

joiify's Introduction

joiify

Fast to write easy to read validation schemas, e.g.

{
  a: 'string',
  b: [],
  c: {
    d: 'integer',
    e: 'date'
  }
}

Joiify will convert the object above to a Joi schema. Why?

  • Semantically intuitive
  • Fast to write and easy to read
  • Portable - Joiify simply takes in a vanilla js/json object
  • Compact, especially if written inline e.g. {a: 'string', b: []}
  • Full power of Joi (see note below)

Note: Joiify will treat Joi objects as pass through so you can safely mix and match Joiify schemes with Joi schemas and do not have to worry about loss of functionality. E.G. Joiify(Joi.string()) compiles to Joi.string()

Note2: Portability is a big deal. You can trivially send a Joiify schema over http as JSON. However this will only work if you do not have any Joi objects in your scheme (re: note above).

Note3: Joiify does not cache the schema result, for optimal performance you should Joiify your scheme before validation time as you would with any Joi schema.

Usage

var Joiify = require('joiify')
var AccountSchema = Joiify({
  AccountID: 'string',
  lastLogin: 'date',
  //Any values that are already Joi objects will included verbatim
  role: Joi.any().valid(['user', 'manager', 'admin']),
  profile: {
    url: 'string',
    email: 'email',
    age: 'integer',
    //special rules for this object
    '*': {required: true, unknown: false, min: 1, max: 10}  
  })

AccountSchema.validate(data, function(err, data){
  //just a nornmal joi schema
})

//AccountSchema equivalent to:
Joi.object().keys({
  AccountID: Joi.string(),
  lastLogin: Joi.date(),
  role: Joi.any().valide(['user', 'manager', 'admin']),
  profile: Joi.object().requiredKeys(['url', 'email', 'age']).unknown(false).min(1).max(10).keys({
    url: Joi.string(),
    email: Joi.string().email(),
    age: Joi.number().integer(),
  })
})

API

Joiify accepts one argument the "scheme" which should be one of the following:

Joiify(scheme)

Convert the a joiify "scheme" into a Joi "schema" where the scheme argument is one of the following

  • string: A value which is converted to a corresponding Joi type
  • Object: An object. Object children will be recursively Joiified.
  • Array: An array. Array children will be used to validate array elements. See details below for conversion rules

Type Conversion

strings types

  • "string": Joi.string()
  • "email": Joi.string().email()
  • "hostname": Joi.string().hostname()
  • "alphanum": Joi.string().alphanum()
  • "hex": Joi.string().hex()
  • "token": Joi.string().token()

number types

  • "number": Joi.number()
  • "integer": Joi.number().integer()

other types

  • "date": Joi.date()
  • "boolean": Joi.boolean()
  • "binary": Joi.binary()
  • "func": Joi.func()
  • "array": Joi.array()
  • "object": Joi.object()
  • "forbidden": Joi.any().forbidden()

special (non strings)

  • undefined: Joi.any().forbidden()
  • null: Joi.any().valid(null)
  • []: Joi.array()
    • Array elements are added as valid types see Joi.array().includes()

objects

  • Joi Schema Object: will return the Joi schema verbatim.
  • {}: Joi.object()
    • Children will be recursively converted using the same rules above
    • '*' if set specifies special handlers for the object validation as follows
      • required: boolean, marks as required (true) or optional (false) all keys on the object
      • unknown: boolean, allows (true) or disallows (false) unknown keys on the object
      • max: integer, sets the maximum number of keys on the object
      • min: integer, sets the minimum number of keys on the object

joiify's People

Contributors

rt2zz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.