Joiful

TypeScript Declarative Validation for Joi
Alternatives To Joiful
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Zod20,2828556 hours ago244September 11, 2022172mitTypeScript
TypeScript-first schema validation with static type inference
Yup19,9998,9122,4084 hours ago115August 20, 2022167mitTypeScript
Dead simple Object schema validation
Ajv12,307801,6067,9852 days ago349March 22, 2022252mitTypeScript
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Formily9,3341192 days ago209September 20, 202211mitTypeScript
📱🚀 🧩 Cross Device & High Performance Normal Form/Dynamic(JSON Schema) Form/Form Builder -- Support React/React Native/Vue 2/Vue 3
Marshmallow6,4659,0919696 days ago168June 26, 2022162mitPython
A lightweight library for converting complex objects to and from simple Python datatypes.
Superstruct6,41734829623 days ago75September 22, 202275mitTypeScript
A simple and composable way to validate data in JavaScript (and TypeScript).
Jsonschema4,06915,5302,4243 days ago74July 07, 202229mitPython
An implementation of the JSON Schema specification for Python
Json Schema3,3448,5014893 months ago46April 13, 202296mitPHP
PHP implementation of JSON schema. Fork of the http://jsonschemaphpv.sourceforge.net/ project
Kubeval3,04382017 days ago13April 26, 2021104otherGo
Validate your Kubernetes configuration files, supports multiple Kubernetes versions
Json Schema Spec2,834
18 hours ago105otherMakefile
The JSON Schema I-D sources
Alternatives To Joiful
Select To Compare


Alternative Project Comparisons
Readme

TypeScript Declarative Validation for Joi


npm version GitHub Actions Build Status codecov Dependabot Status

API Docs

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.

Installation

npm add joiful reflect-metadata

Or

yarn add joiful reflect-metadata.

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

tsconfig.json

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

Basic Usage

Ensure you import reflect-metadata as the first import in your application's entry point.

index.ts

import 'reflect-metadata';

...

Then you can start using joiful like this.

import * as jf from 'joiful';

class SignUp {
  @jf.string().required()
  username: string;

  @jf
    .string()
    .required()
    .min(8)
  password: string;

  @jf.date()
  dateOfBirth: Date;

  @jf.boolean().required()
  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.

customDecorators.ts

import * as jf from 'joiful';

const password = () =>
  jf
    .string()
    .min(8)
    .regex(/[a-z]/)
    .regex(/[A-Z]/)
    .regex(/[0-9]/)
    .required();

changePassword.ts

import { password } from './customDecorators';

class ChangePassword {
  @password()
  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 {
  @string().required()
  name!: string;
}

class Movie {
  @string().required()
  name!: string;

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

Validating object properties

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

class Address {
  @string()
  line1?: string;

  @string()
  line2?: string;

  @string().required()
  city!: string;

  @string().required()
  state!: string;

  @string().required()
  country!: string;
}

class Contact {
  @string().required()
  name!: string;

  @object().optional()
  address?: Address;
}

API Docs

joiful has extensive JSDoc / TSDoc comments.

You can browse the generated API docs online.

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.

Contributing

Got an issue or a feature request? Log it.

Pull-requests are also very welcome.

Alternatives

  • 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.
Popular Validation Projects
Popular Schema Projects
Popular Data Processing Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Javascript
Typescript
Validation
Validator
Schema
Decorators