This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

joi-decorators

0.0.2 • Public • Published

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

Package Sidebar

Install

npm i joi-decorators

Weekly Downloads

1

Version

0.0.2

License

MIT

Last publish

Collaborators

  • lorefnon