Awesome Open Source
Awesome Open Source

TypeScript Declarative Validation for Joi

npm version CircleCI codecov Dependabot Status

Why Joiful?

This lib allows you to apply Joi validation constraints on class properties, by using decorators.

This means you can combine your type schema and your validation schema in one go!

Calling Validator.validateAsClass() allows you to validate any object as if it were an instance of a given class.


npm add joiful


yarn add joiful.

You must enable experimental decorators and metadata in your TypeScript configuration.


  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true

Basic Usage

import * as jf from 'joiful';

class SignUp {
  username: string;

  password: string;
  dateOfBirth: Date;

  subscribedToNewsletter: boolean;

const signUp = new SignUp();
signUp.username = 'rick.sanchez';
signUp.password = 'wubbalubbadubdub';

const { error } = jf.validate(signUp);

console.log(error); // Error will either be undefined or a standard joi validation error

Validate plain old javascript objects

Don't like creating instances of classes? Don't worry, you don't have to. You can validate a plain old javascript object as if it were an instance of a class.

const signUp = {
  username: 'rick.sanchez',
  password: 'wubbalubbadubdub',

const result = jf.validateAsClass(signUp, SignUp);

Custom decorator constraints

Want to create your own shorthand versions of decorators? Simply create a function like below.


import * as jf from 'joiful';

const password = () =>


import { password } from './customDecorators';

class ChangePassword {
  newPassword: string;

Validating array properties

class SimpleTodoList {
  @jf.array().items(joi => joi.string())
  todos?: string[];

To validate an array of objects that have their own joiful validation:

class Actor {
  name!: string;

class Movie {
  name!: string;

  @array({ elementClass: Actor }).required()
  actors!: Actor[];

Validating object properties

To validate an object subproperty that has its own joiful validation:

class Address {
  line1?: string;

  line2?: string;

  city!: string;

  state!: string;

  country!: string;

class Contact {
  name!: string;

  address?: Address;

Got a question?

The joiful API is designed to closely match the joi API. One exception is validating the length of a string, array, etc, which is performed using .exactLength(n) rather than .length(n). If you're familiar with the joi API, you should find joiful very easy to pickup.

If there's something you're not sure of you can see how it's done by looking at the unit tests. There is 100% coverage so most likely you'll find your scenario there. Otherwise feel free to open an issue.


Got an issue or a feature request? Log it.

Pull-requests are also very welcome.


  • class-validator: usable in both Node.js and the browser. Mostly designed for validating string values. Can't validate plain objects, only class instances.
  • joi-extract-type: provides native type extraction from Joi Schemas. Augments the Joi type definitions.
  • typesafe-joi: automatically infers type information of validated objects, via the standard Joi schema API.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (68,715
typescript (10,877
nodejs (3,696
validation (312
schema (173
validator (102
decorators (89
constraints (45
joi (15

Find Open Source By Browsing 7,000 Topics Across 59 Categories