All Projects → joiful-ts → Joiful

joiful-ts / Joiful

TypeScript Declarative Validation for Joi

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Joiful

Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (-29.94%)
Mutual labels:  schema, validation, validator
Schema Utils
Options Validation
Stars: ✭ 162 (-8.47%)
Mutual labels:  schema, validation, validator
Class Validator
Decorator-based property validation for classes.
Stars: ✭ 6,941 (+3821.47%)
Mutual labels:  validation, validator, decorators
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+948.02%)
Mutual labels:  schema, validation
Aura.filter
Validate and sanitize arrays and objects.
Stars: ✭ 134 (-24.29%)
Mutual labels:  validation, validator
Pandasschema
A validation library for Pandas data frames using user-friendly schemas
Stars: ✭ 135 (-23.73%)
Mutual labels:  schema, validation
Rdfunit
An RDF Unit Testing Suite
Stars: ✭ 117 (-33.9%)
Mutual labels:  schema, validation
Ozzo Validation
An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
Stars: ✭ 2,471 (+1296.05%)
Mutual labels:  validation, validator
Nope Validator
A small, simple and fast JS validator. Like, wow thats fast. 🚀
Stars: ✭ 142 (-19.77%)
Mutual labels:  schema, validation
Credit Card
Credit Card Validation
Stars: ✭ 150 (-15.25%)
Mutual labels:  validation, validator
Property Validator
✅ Easy property validation for JavaScript, Node and Express.
Stars: ✭ 153 (-13.56%)
Mutual labels:  validation, validator
Apvalidators
Codeless solution for form validation in iOS!
Stars: ✭ 130 (-26.55%)
Mutual labels:  validation, validator
Class Transformer Validator
A simple plugin for class-transformer and class-validator which combines them in a nice and programmer-friendly API.
Stars: ✭ 151 (-14.69%)
Mutual labels:  validation, validator
Formhelper
ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)
Stars: ✭ 155 (-12.43%)
Mutual labels:  validation, validator
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-23.16%)
Mutual labels:  schema, validation
Laravel Zip Validator
Laravel ZIP file content validator
Stars: ✭ 120 (-32.2%)
Mutual labels:  validation, validator
Validation
🔒 Laravel farsi/persian validation
Stars: ✭ 142 (-19.77%)
Mutual labels:  validation, validator
Validator
A tool to validate text inside TextInputLayout
Stars: ✭ 117 (-33.9%)
Mutual labels:  validation, validator
React Form With Constraints
Simple form validation for React
Stars: ✭ 117 (-33.9%)
Mutual labels:  validation, constraints
Schema Typed
Schema for data modeling & validation
Stars: ✭ 150 (-15.25%)
Mutual labels:  schema, validator

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.

Installation

npm add joiful

Or

yarn add joiful.

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

tsconfig.json

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

Basic Usage

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;
}

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].