Skip to content

akramkazmi71/ak-json-to-joi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ak-json-to-joi

NPM Module to convert predefined JSON into Joi validation schema dynamically.

Codacy Badge npm version Dependencies npm Downloads

How to install?

NPM

How to use


const JoiSchemaBuilder = require('ak-json-to-joi');
let JoiValidationSchema = JoiSchemaBuilder.build(yourJSON);
let result = Joi.validate(yourPayloadJSON, JoiValidationSchema)

Supported Features

Types

Use any of these as types of the key


string
number
email
array
object
boolean

Validations


minLength: {integer value}
maxLength: {integer value}
optional: {true / false}
required: {true / false}
regex: {regex pattern}
default: {default value}

Sample JSONs

ARRAY JSON


{
    "$array": {
        "name":{
            "$type":"string"
        },
        "number":{
            "$type":"number",
            "optional": true
        }
    }
}

OBJECT JSON


{
    "name": {
        "original": {
            "$type": "string",
            "required": true,
            "maxLength": 200
        },
        "nick": {
            "$type": "string",
            "optional": true,
            "default": "arshad"
        }
    },
    "phone": {
        "$type": "number",
        "required": true
    },
    "email": {
        "$type": "email",
        "optional": true
    },
    "date": {
        "$type": "date",
        "minDate": "1-1-2017",
        "maxDate": "10-05-2018"
    }
}

Note:


'type' and 'array' key in validation json should be prefixed with '$' symbol
on missing $ symbol your validation might not work.

Contributors

About

Convert JSON to Joi validator schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%