1.0.0 • Published 7 years ago

specla-validator v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

Specla Validator

npm version Build Status Dependency Status devDependencies Status Standard - JavaScript Style Guide

Validate an object against a schema.

Install

npm install --save specla-validator

Usage

const Validator = require('specla-validator')

const data = {
  name: 'Frederik',
  age: 23,
  skills: ['js'],
}

const schema = {
  name: String,
  age: Number,
  skills: Array
}

const validator = new Validator(data, schema)

// check for errors
console.log(validator.errors)

Custom rules

Create a custom rule with the Validator.Rule class.

const rule = new Validator.Rule({
  name: 'my-rule',
  validate: value => {
    // Do something to validate the value
    // return true if valid and false if not
  }
})

const schema = {
  name: rule
}

Rules

The validator exposes some rules for easier schema creation.

const { string, number, array, boolean, object } = Validator

const Schema = {
  name: string({ required: true, min: 3, max: 40 }),
  age: number({ required: true, min: 0 }),
  skills: array({ min: 0, max: 10 })
}
1.0.0

7 years ago

0.0.1

7 years ago