0.0.2 • Published 8 years ago

joi-decorators v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

joi-decorators

npm version Build Status Coverage Status

Purpose

Use ES7 decorators to augment your javascript classes with declarative validation support.

Object validation:

import { ObjectSchema } from 'joi-validators'

@ObjectSchema({
  username: Joi.string().alphanum().min(3).max(30).required(),
  email: Joi.string().email()
})
class User {
  constructor(username, email) {
    this.username = username
    this.email = email
  }
}
let user = new User('lorefnon', 'lorefnon@example.com')
user.validate() // returns { error: null, ... }
user.isValid() // returns true
user.ensureValid() // does not throw exceptio

user = new User(10, 20)
user.validate() // returns { error: { name: 'ValidationError', ... }, ... }
user.isValid() // returns false
user.ensureValid() // throws ValidationError

Method argument validations:

import { ArgumentSchema } from 'joi-validators'

class User {

  @ArgumentSchema([
    Joi.string().required(),
    Joi.string().required()
  ])
  getFullName(firstName, lastName) {
    return `${firstName} ${lastName}`
  }

}
let user = new User
user.getFullName('lorem', 'ipsum') // returns 'lorem ipsum'
user.getFullName(10, 20) // throws ValidationError

Install

npm i -D joi-decorators

License

MIT © Lorefnon