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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastest-validator-decorators

v2.2.1

Published

Fastest validator decorators

Readme

npm version npm downloads GitHub issues License

Fastest Validator Decorators

Fastest Validator Decorators is a TypeScript library providing a set of decorators that streamline the integration with the fastest-validator, a high-performance validation library for JavaScript and TypeScript. This package simplifies schema definition and validation by leveraging TypeScript's decorator feature.

:boom: Upgrading from version 1.x? Check the Migration Guide.

Key Features

  • Type-safe schema validation using TypeScript decorators.
  • Supports all validation types provided by fastest-validator.
  • Nested object and array validation made simple with decorators.
  • Async validation support, enabling you to run asynchronous validation tasks.

Installation

Install the package via npm:

npm install --save fastest-validator-decorators fastest-validator

:warning: Note: fastest-validator is a peer dependency, so ensure it's also installed.

Next, enable decorators in your tsconfig.json:

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

Usage Example

Here’s a basic example of how to use the decorators in your project:

import {
  Schema, Array, UUID, Enum, Email, Number, Nested, getSchema, validate, validateOrReject
} from "fastest-validator-decorators";

@Schema({ strict: true })
class Entity1 {
  @Array({ items: "string" })
  prop1: string[];
}

@Schema()
class Entity2 {
  @UUID()
  prop1: string;

  @Enum({ values: ["one", "two"] })
  prop2: "one" | "two";

  @Email()
  prop3: string;

  @Number({ positive: true })
  prop4: number;

  @Nested()
  prop5: Entity1;
}

// Generating a validation schema
const schema = getSchema(Entity2);

// Creating and validating instances
const entity = new Entity2();
entity.prop1 = "invalid-uuid";  // Validation error: invalid UUID
entity.prop2 = "one";
entity.prop3 = "[email protected]";
entity.prop4 = -10;  // Validation error: number is not positive

const result = validate(entity); // true or error list
await validateOrReject(entity);   // true or throws an error

Available Decorators

Fastest Validator Decorators supports the following decorators for schema definition. Each decorator corresponds to a validation type from fastest-validator.

  • @Schema: Defines the validation schema for a class.
  • @Field: Generic decorator for defining custom fields.
  • @String, @Boolean, @Number, @UUID, @ObjectId, @Email, @Date, @Enum, @Array, @Equal, @Instance, @Currency, @Func, @Luhn, @Mac, @Url, @Any, @Multi: Each of these applies a specific fastest-validator type with custom options.

You can also use @Nested and @NestedArray for validating complex objects and arrays of objects.

Decorator Stacking

The library allows stacking multiple decorators on a single property. For example:

@String()
@Number()
prop1: string | number;

This syntax resolves to the @Multi decorator, which validates the property against multiple types.

Async Validation Support

Async validation is supported by adding the async: true option to your schema. Here's an example of how to use asynchronous checks within a custom validation:

@Schema({ async: true, strict: true })
class User {
  @Custom({
    async check(value, errors) {
      const isUserInDB = await db.checkUserName(value);
      if (isUserInDB) errors.push({ type: "unique", actual: value });
      return value;
    }
  })
  username: string;
}

const user = new User();
user.username = "existingUser";
await validateOrReject(user);  // async validation

API Methods

  • getSchema(Class): Retrieves the fastest-validator schema for a given class.
  • validate(instance): Validates the provided instance and returns true or an error list.
  • validateOrReject(instance): Same as validate(), but throws errors if validation fails.

License

This project is licensed under the MIT License.