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

simplejsonvalidator

v2.0.2

Published

A library that check if a json is valid or not.

Downloads

21

Readme

SimpleJsonValidator

BCH compliance

Json Validator for NodeJS with simple ruby-like syntax

Installation

You can install this library using npm:

npm i --save simplejsonvalidator

Usage

First, you need to create a new JsonValidator instance calling to the exported function:

const jsonValidator = require('simplejsonvalidator');
const validator = jsonValidator();

Then, you can create validators using the 'create' method, which accepts a callback and the validator name

validator.create(t => ({
  user: t.string.required,  
  age: t.number,
  text: t.string.max(10).required,
}), 'validatorName');

or you can omit the validator name and assign the returning validator to a variable:

const myValidator = validator.create(t => ({
  user: t.string.required,  
  age: t.number,
  text: t.string.max(10).required,
}));

Finally, you can validate any json using the 'validate' function:

const json = {
  user: 'user',  
  age: 22,
  text: '123456789',
};
myValidator.validate(json);

You can also nest objects like this:

const demoValidator = validator.create(t => ({
  user: t.string.required,
  age: t.number,
  text: t.string.max(10).required,
  data: {
    token: t.string,
  },
}));

And you can check why a json is invalid:

const errors = demoValidator.errors();

Errors look like these:

[
  {
    message: 'element "key" must be string',
    type: 'string',
  },
  {
    message: 'key "user.name" should be upper than 10',
    type: 'string',
  },
  {
    message: 'key "user.age" should be positive',
    type: 'number',
  },
  {
    message: 'element "user.connection" must be date',
    type: 'date',
  },
]

Types

String

Validates if type is string. Example:

validator.create(t => ({
  key: t.string,
}));

You can use these validators in string type:

| Validator | Explaination | Example | | ------------- | ---------------------------- |:-----------------------------------------:| | required | makes key required | t.string.required | | shouldBe | checks if the values matches | t.string.shouldBe('apples', 'oranges') | | max(number) | maximum lenght limit | t.string.max(10) | | min(number) | minimum lenght limit | t.string.min(1) | | matches(regex)| tests if string matches regex| t.string.matches(/Regex/) |

Number

Validates if type is number. Example:

validator.create(t => ({
  key: t.number,
}));

You can use these validators in number type:

| Validator | Explaination | Example | | ------------- | ---------------------------- |:-------------------------:| | required | makes key required | t.number.required | | shouldBe | checks if the values matches | t.number.shouldBe(22, 21) | | positive | checks if number is positive | t.number.positive | | negative | checks if number is negative | t.number.negative |

Boolean

Validates if type is boolean. Example:

validator.create(t => ({
  key: t.boolean,
}));

You can use these validators in boolean type:

| Validator | Explaination | Example | | ------------- | ---------------------------- |:-------------------------:| | required | makes key required | t.boolean.required | | shouldBe | checks if the value matches | t.boolean.shouldBe(false) |

Array

Validates if type is array. Example:

validator.create(t => ({
  key: t.array,
}));

You can use these validators in array type:

| Validator | Explaination | Example | | --------------------- | -------------------------------------------------------- |:-------------------------------------:| | required | makes key required | t.array.required | | shouldBe | checks if the values matches | t.array.shouldBe([22, 21], [1, 'hi']) | | exactLength(number) | check if array length is exactly the specified length | t.array.exactLength(20) | | lengthLowerTo(number) | check if array length is lower to the specified length | t.array.lengthLowerTo(9) | | lengthUpperTo(number) | check if array length is upper to the specified length | t.array.lengthUpperTo(1) | | notEmpty | check if array legnth is not empty | t.array.notEmpty |

Date

Validates if type is date. Example:

validator.create(t => ({
  key: t.date,
}));

You can use these validators in date type:

| Validator | Explaination | Example | | ----------------- | ------------------------------------------ |:------------------------------------:| | required | makes key required | t.date.required | | shouldBe | checks if the values matches | t.date.shouldBe(new Date()) | | beforeDate(Date) | check if the date is before desired date | t.date.beforeDate(new Date()) | | afterDate(Date) | check if the date is after desired date | t.date.afterDate(new Date()) |

Express Framework Integration

You can use an express middleware to check jsons. You can do it calling to createMiddleware function to create a middleware:

const myValidator = validator.create(t => ({
  user: t.string.required,  
  age: t.number,
  text: t.string.max(10).required,
}));

app.post('/', validator.createMiddleware(myValidator), (req, res) => res.send(req.body););

The middleware, when a json is not valid, returns found errors. You can customize the returning status code:

const middleware = validator.createMiddleware(myValidator, 401);