client-json-validation
v0.1.4
Published
Lightweight client side JSON validator with zero dependencies
Downloads
46
Maintainers
Readme
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'}}}}