1.1.1 • Published 12 months ago

joi-decorator v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

joi-decorator

Decorator based interface for Joi.

Installation

npm install joi-decorator reflect-metadata

The basics

First you need to declare an entity:

import { mustBe, a } from "joi-decorator";

export class User {
    @mustBe(a.string().alphanum().min(3).max(30).required())
    public username: string;
    @mustBe(a.string().regex(/^[a-zA-Z0-9]{3,30}$/))
    public password: string;
    @mustBe([a.string(), a.number()])
    public accessToken: string|number;
    @mustBe(a.number().integer().min(1900).max(2013))
    public birthyear: number;
    @mustBe(a.string().email())
    public email: string;
    public constructor(
        username: string,
        password: string,
        access_token: string|number,
        birthyear: number,
        email: string
    ) {
        this.username = username;
        this.password = password;
        this.accessToken = accessToken;
        this.birthyear = birthyear;
        this.email = email;
    }
}

Then you can validate the entity instances:

Example 1: Valid entity

import { validate } from "joi-decorator";
import { expect } from "chai";
import {  User } from "./entities/user";

const validUser = new User(
    "root",
    "secret",
    "token",
    1989,
    "test@test.com"
);

const result1 = validate(validUser);
expect(result1.error).to.eql(null);

License

Inspired by zafiro-validators.

MIT License

Copyright (c) Jean-Baptiste Pionnier

1.1.1

12 months ago

1.0.2

12 months ago

1.1.0

12 months ago

1.0.1

3 years ago

1.0.0

3 years ago