npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

exonerate

v1.1.0

Published

Exonerate is a versatile validation decorator for NestJS that combines validation and automatic Swagger documentation. It supports various data types, nested objects, arrays, regex patterns, and custom validations such as unique and exist for database ent

Downloads

683

Readme

Exonerate

Exonerate is a customizable validation decorator package designed for use with NestJS and class-validator. It allows you to define multiple validation rules in one place, supports various types such as strings, numbers, arrays, objects, and more, with additional support for nested validation, regex patterns, and automatically adds Swagger documentation.

Features

  • Validate multiple rules in a single decorator.
  • Supports data types like string, number, array, and object.
  • Nested object validation.
  • Automatically integrates Swagger decorators.
  • Regular expression (regex) validation.
  • Custom decorators like IsInstance for custom instance validation.
  • Built-in support for unique and existence validations for database entities.

Installation

npm install exonerate

Usage

  1. Basic Setup

To use Exonerate, apply the @Exonerate decorator with your desired rules on your DTO properties.

Update your main.ts to enable global validation:

app.useGlobalPipes(new ValidationPipe());

.env

For the unique and exist validations to work properly, you need to provide database configurations in your .env file:

DB_TYPE=postgres
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USERNAME=
DB_PASSWORD=
DB_DATABASE=
ENTITIES_PATH="dist/**/**/*.entity{.ts,.js}" # Adjust path according to your project

AppModule Setup

Ensure you add the following to your AppModule for environment configuration:

ConfigModule.forRoot({
  isGlobal: true,
  envFilePath: '.env',
});

Example Usage

Below is an example DTO class that uses the @Exonerate decorator for validation and Swagger documentation:

import { Exonerate } from 'exonerate';

export class CreateUserDto {
  @Exonerate({ rules: 'required|string|max:20|min:4|exist:name', entity: User, example: 'JohnDoe' })
  name: string;

  @Exonerate({ rules: 'required|email|unique:email', entity: User, example: '[email protected]' })
  email: string;

  @Exonerate({
    rules: 'required|max:20|min:8|pattern',
    regexPattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/,
    example: 'Pxdmv48GQPnTfeEl4Jq0'
  })
  password: string;

  @Exonerate({ rules: 'required|array', arrayType: 'string', example: ['1234567890', '0987654321'] })
  phone: string[];

  @Exonerate({ rules: 'optional|date', classType: Date, example: '2024-09-17T04:29:28.529Z' })
  dob: Date;

  @Exonerate({ rules: 'required|enum', enumType: ROLE, example: 'ADMIN' })
  role: string;

  @Exonerate({ rules: 'optional|array', arrayType: AddressDto, example: [{ home: 'string', roadNumber: 'string' }] })
  addresses: AddressDto[];

  @Exonerate({ rules: 'optional|object', classType: AddressDto, example: { home: 'string', roadNumber: 'string' } })
  address: AddressDto;
}

Available Validation Rules

Here is a list of the validation rules that can be used with the @Exonerate decorator:

  • required: Ensures the field is not empty.
  • optional: Field is optional.
  • email: Validates an email format.
  • string: Ensures the field is a string.
  • min: Validates minimum length for strings or value for numbers.
  • max: Validates maximum length for strings or value for numbers.
  • int: Ensures the field is an integer.
  • float: Ensures the field is a float.
  • number: Ensures the field is a number.
  • decimal: Ensures the field is a decimal.
  • date: Validates a date field.
  • uuid: Ensures the field is a valid UUID.
  • enum: Ensures the field is one of the specified enum values.
  • array: Validates arrays and supports validation of array items (e.g., string[], number[]).
  • object: Validates nested objects using a specified class.
  • pattern: Validates a field against a regular expression.
  • unique: Ensures the field is unique in the database (requires an entity and field).
  • exist: Ensures the field exists in the database (requires an entity and field).

Contribution

Feel free to contribute to the development of this package by opening issues or submitting pull requests.


This README file now reflects the full capabilities of your @Exonerate decorator, including its use for both validation and Swagger integration.