obj-validate
Validate an object schema
Table of Contents
Install
$ npm install obj-validate# OR $ yarn add obj-validate
Usage
The module exports a single function that accepts an object
and a schema
as arguments. The object
is validated against the schema
, returning errors.
const foo = bar: 5 baz: 'foo' const result = console/*{ bar: [], foo: [ ReferenceError: Missing required property foo at Object.keys.forEach.key (/Users/Tiaan/Workspace/obj-validate/src/index.js:21:24) at Array.forEach (native) at validate (/Users/Tiaan/Workspace/obj-validate/src/index.js:16:23) at Object.<anonymous> (/Users/Tiaan/Workspace/obj-validate/src/index.js:51:1) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) ], baz: [ TypeError: Invalid type. Property baz should be Number at Object.keys.forEach.key (/Users/Tiaan/Workspace/obj-validate/src/index.js:30:26) at Array.forEach (native) at validate (/Users/Tiaan/Workspace/obj-validate/src/index.js:16:23) at Object.<anonymous> (/Users/Tiaan/Workspace/obj-validate/src/index.js:51:1) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) ] }*/
Possible validations:
required
- A property is requiredtype
- The required type of a property as aString
orArray
of possible types e.g.Object
or['Function', 'String']
pattern
- Regex pattern to match property value on e.g./foo/
Contribute
- Fork it and create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request
License
MIT