2.0.2 • Published 6 years ago

obj-validate v2.0.2

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

obj-validate

package version package downloads standard-readme compliant package license make a pull request Greenkeeper badge

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.

import objValidate from 'obj-validate'

const foo = {
  bar: 5,
  baz: 'foo'
}

const result = objValidate(foo, {
  bar: {
    required: true,
    type: 'Number',
    pattern: /\d{1}/
  },
  foo: {
    required: true
  },
  baz: {
    type: 'Number'
  }
})

console.log(result)
/*{ 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 required
  • type - The required type of a property as a String or Array of possible types e.g. Object or ['Function', 'String']
  • pattern - Regex pattern to match property value on e.g. /foo/

Contribute

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am 'Add some feature'
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT

2.0.2

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago