schema-validationdeprecated

javascript object schema defining/checking

Usage no npm install needed!

<script type="module">
  import schemaValidation from 'https://cdn.skypack.dev/schema-validation';
</script>

README

schema Build Status js-standard-style Supported Node.js version

Define JavaScript object schemas and check them.

Usage

Installation:

Use yarn or npm:

$ [sudo] npm install --save schema-validation

Example:

const Schema = require('schema-validation')

const hello = new Schema({hello: String})
const nesting = new Schema({nesting: hello})
const deep = new Schema({deep: nesting})
const users = new Schema({users: deep})

const mySchema = new Schema({
  key1: String,
  numKey: Number,
  obj: Object,
  key: users
})

const someObject = {
  key1: 'a string',
  numKey: 1.345678,
  obj: {},
  key: {
    users: {
      deep: {
        nesting: {
          hello_: 'world'
        }
      }
    }
  }
}

try {
  mySchema.verify(someObject)
} catch (err) {
  assert(err.message === 'Key "hello_" couldn\'t be found under key -> users -> deep -> nesting.')
}