npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

skerla-json-schema

v0.5.2

Published

JSON Schema validation

Downloads

6

Readme

JSON Schema validation v0.3.0

Installation

$ npm install skerla-json-schema

Schema

Constructor

new V.Schema(schema: Object|Array|Rule): Schema

Methods

validate(object: Object|Array): ValidationResult

validates a given object and returns an instances of ValidationResult

getSpecs(): Object

Returns an object describing specification of the schema.

Usage example

General usecase

const V = require('skerla-json-validation');
const validation = new V.Schema({
  id: V(Number).min(0).required(),
  status: V(String).oneOf(['active', 'inactive']).required(),
  items: V(Array).required().schema({
    name: V(String).required(),
    amount: V(Number).min(0).required(),
    has_photo: V(Boolean).required()
  })
});

const validationResult = validation.validate({
  id: 76335,
  status: 'active',
  items: [{
    name: 'Chair',
    amount: 2,
    has_photo: true
  }]
});

validationResult.isValid(); //true

Advanced

const V = require('skerla-json-validation');
const validation = new V.Schema(V(Array).len(2).schema([
  V(String).required(),
  V(Number).required()
]));

validation.validate({}).isValid(); //false
validation.validate([]).isValid(); //false
validation.validate(['banana']).isValid(); //false
validation.validate(['banana', 5]).isValid(); //true

ValidationResult

Methods

isValid(): Boolean

Returns if the object has passed validation process

getErrors(): Array{errno: Number, path: string, message: String}

Returns validation errors

const V = require('skerla-json-schema');
const validation = new V.Schema({
  id: V(Number),
  name: V(String).min(3),
  surname: V(String).required(),
});

const validationResult = validation.validate({
  id: '123',
  name: 'Al'
});

const isValid = validationResult.isValid(); //false
const errors = validationResult.getErrors(); 
/*
[ 
  { errno: 2, path: 'id', message: 'type of the value must be Number, got String.' },
  { errno: 6, path: 'name', message: 'the length must be >= 3, got 2.' },
  { errno: 1, path: 'surname', message: 'required' } 
]
*/
cleanup(): Object

Removes all properties in a nested object which are not defined in the schema.

const V = require('skerla-json-validation');
const validation = new V.Schema({
  name: V(String),
  surname: V(String),
  address: {
    city: V(String)
  }
});

const validationResult = validation.validate({
  id: 123,
  name: 'Andrius',
  surname: 'Skerla',
  address: {
    country: 'UK',  
    city: 'London',
    street_name: 'Baker Street'
  }
});

const cleanedUp = validationResult.cleanup();
/* returns
{
  name: 'Andrius',
  surname: 'Skerla,
  address: {
    city: 'London'
  }
};
*/

Rule

Methods

required(value: Boolean = true): Rule

Defines if the property can not be undefined

min(value: Number): Rule

Defines min length of a String or an Array, or min Number value.

max(value: Number): Rule

Defines max length of a String or an Array, or max Number value.

len(value: Number): Rule

Defines a length of a String or an Array

match(value: RegExp): Rule

Defines a pattern of a string

const validation = new V.Schema({
  number: V(String).match(/\d+-\d+/)
});
oneOf(values: Array): Rule

Defines a set of possible values for String, Number and Array.

const validation = new V.Schema({
  type: V(String).oneOf(['request', 'response']),
  directions: V(Array).oneOf(['in', 'out'])
});

validation.validate({
  type: 'request',
  directions: ['in', 'in', 'in', 'out'] 
}).isValid(); //true
typeOf(constructor: Function): Rule

Defines a type of a item in an array

new V.Schema({
  items: V(Array).typeOf(String)
})
schema(schema: Object): Rule

Defines a schema of an array or a nested object

null(): Rule

Allows a property to have null value.

new V.Schema({
  address: V(String).null().required() //property must be defined and be either null either String
})
check(path: String, value: mixed): ValidationResult; throws RuleError

Usage Example

const Rule = require('skerla-json-schema/lib/rule');
const rule = new Rule(String).oneOf(['hi', 'hello']).required();

rule.check('hi'); //true
rule.check('bonjour'); //throws