Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

joi-decorators

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

joi-decorators

Decorator based goodies for sophisticated validation support in your ES7 classes

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

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

Keywords

decorators

FAQs

Package last updated on 08 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts