0.0.6 • Published 7 years ago

schema-validator2 v0.0.6

Weekly downloads
28
License
-
Repository
github
Last release
7 years ago

schema-validator2

When I wrote this library, I didn't notice there had been already a similar library called schema-validator.

So I publish my own as "schema-validator2".

It requires node >= 7.x.

npm i -S schema-validator2

Schema validation

let { validate } = require('schema-validator2')
let { ObjectID } = require('mongodb')

let schema = {
  nested: { schema: anotherSchema, optional: true },
  arr: { type: Array, element: { type: Number } },
  _id: { type: ObjectID }
}

let data = {
  arr: [1, 5],
  _id: new ObjectID()
}

let rs = validate({
  schema,
  partial: true || false
  data
})
// Possible results:
// - rs = ''
// - rs = extra keys: 'a', 'b'
// - rs = 'a' is required
// - rs = 'a' is not a/an 'Date'
// - Error: dot keys are not supported: 'a.b'

Value validation

let schema = {
  name: { type: String, range: [2, 16] },
  price: { type: Number, range: [0.5, 99.5], step: 0.5 },
  quantity: { type: Number, range: [1, null], test: _.isInteger }
}
// Possible results:
// - rs = ''
// - rs = 'name' is not in range [2, 16]
// - rs = 'price' is not in range [0.5, 99.5]
// - rs = 'price' is not by step '0.5'
// - rs = 'quantity' does not match '_.isInteger'

Coverage:

Todo:

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago