Skip to content

qostya/client-json-validation

Repository files navigation

Client JSON validation

Lightweight client side JSON validator with zero dependencies

Installation

npm i -S client-json-validation

Usage

// Import lib
import {object, string, array, number} from 'client-json-validation';
// Import target locale
import en from 'client-json-validation/lib/texts/en';

// Set localization once (en|ru)
setLocale(en);

// Create a schema
const schema = object({
  items: array(
    object({
      cost: number(),
      title: string().required()
    })
  ).min(1)
});

// Run validate function
schema.validate({
  items: [
    {cost: 100, title: 'T-Shirt'},
    {title: 'Bag'}
  ]
}); // Returns null

schema.validate({
  items: [
    {cost: 'one hundred', title: 'T-Shirt'},
    {title: 'Bag'}
  ]
}); // Returns {items: {0: {cost: 'Value is not a number'}}}}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published