@amoutonbrady/object-validator
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

object-validator is minimalistic (400b uncompressed), elegant and pluggable object validation library

object-validator supports modern browser. You'll need to use polyfill to make it work for older ones.

Install

$ npm install @amoutonbrady/object-validator
$ yarn @amoutonbrady/object-validator

Usage

import { createSchema, isString } from '@amoutonbrady/object-validator';

const schema = createSchema({
	key1: [isString],
	_key2: [isString], // This key is marked optional because of the '_'
});

const [errors, pass] = schema.validate({ key1: 'test', key2: 'test' });
console.log({ errors, pass });
// { errors: [], pass: true }

const [errors, pass] = schema.validate({ key1: 'test' });
console.log({ errors, pass });
// { errors: [], pass: true }

const [errors, pass] = schema.validate({ key2: 'test' });
console.log({ errors, pass });
/**
 *  {
 *  	errors: [
 * 			"The key: key1 was found in the schema but not in the object",
 *    	],
 * 		pass: false,
 * 	}
 **/

const [errors, pass] = schema.validate({ key2: true });
console.log({ errors, pass });
/**
 *  {
 *  	errors: [
 * 			"The key: key1 was found in the schema but not in the object",
 * 			"The value: true for the key: key1 didn't pass the isString validator",
 *    	],
 * 		pass: false,
 * 	}
 **/

Readme

Keywords

none

Package Sidebar

Install

npm i @amoutonbrady/object-validator

Weekly Downloads

6

Version

0.3.0

License

MIT

Unpacked Size

23.4 kB

Total Files

21

Last publish

Collaborators

  • amoutonbrady