json-blueprint
JSON validation library
Quick look
; const person = 'Test' name: String age: Number verified: Boolean phone: ext: type: String maxLength: 5 number: Any intrests: String planet: season: Is songs: Array contacts: Object data: Any; person;
Installation
npm:
npm install json-blueprint
yarn:
yarn add json-blueprint
Usage
// Import the library; // Create new blueprintconst bp = 'BlueprintName' ...schema; // orconst bp = Blueprint; // Validate databp
Table of contents
String
; const bp = 'Test' // Basic string name: String // String with options username: type: String minLength: 3 maxLength: 5 // Regex match match: /[^A-Za-z0-9]+/g // Additional validation funciton { if !value throw 'Clowns are scary'; } // Property is required by default required: false ;
Number
; const bp = 'Test' // Basic number age: Number // Number with options poolNumber: type: Number min: 0 max: 15 // Additional validation funciton { if value === 8 throw `8-Ball not allowed for the property `; } // Property is required by default required: false ;
Boolean
; const bp = 'Test' // Basic boolean loggedIn: Boolean // Boolean with options verified: type: Boolean // Additional validation funciton { if !value throw 'User is not verified'; } // Property is required by default required: false ;
Object
; const bp = 'Test' // Basic object roles: Object // Object with defined properties hours: partTime: Boolean fullTime: Boolean preferredHours: Array // Object with options permissions: type: Object // Additional validation funciton { const keys = Object; if !keys throw ` is missing read permission`; } // Property is required by default required: false // Object with options and defined properties salary: type: Object required: false items: hourlyRate: Number currency: String ;
Array
; const bp = 'Test' // Any array favoriteSongs: Array // Array of strings favoriteMovies: String // Array of objects accounts: id: String name: String // Array of string arrays tasksTable: String // Array with options hobies: type: Array minLength: 1 maxLength: 5 // Additional validation funciton { if !value throw 'User is not verified'; } // Property is required by default required: false // Array with options and defined items technologies: type: Array required: false items: String addresses: type: Array minLength: 1 items: street: String number: String ;
Tuple
; const bp = 'Test' /** * Phone number tuple with extension and number * @param { string } [0] * @param { number } [1] */ number: ;
Any and Any.of
; const bp = 'Test' // Any data: Any // Any of the provided types apartmentNumber: Any phone: Any;
Is and Is.oneOf
; const bp = 'Test' // Any solarSystem: // Any of the provided types planet: Is city: Is;;
Custom types
; // Define custom type which checks is provided value truthy { if !value throw ` is not truthy`; } // Create blueprint whcich implements custom typeconst bp = 'Test' person: happy: Truthy ; // Validate objectbp
Custom type with options
; // Define custom type { const a // true b // 1 c // 'hello' d // { test: [] } } = options; if !value throw ` is not truthy`; } // Create blueprint whcich implements custom typeconst bp = 'Test' person: happy: type: Truthy a: true b: 1 c: 'hello' d: test: ;