class-validator
  • Table of Contents
  • basics
    • Validating objects
  • introduction
    • Installation
Powered by GitBook
On this page
  1. basics

Validating objects

import {
  validate,
  validateOrReject,
  IsString,
  IsInt,
  IsDate,
  MaxLength,
  Min,
  Max,
  ValidationError,
} from 'class-validator';

export class Book {
  @IsString()
  @MaxLength(255)
  title: string;

  @IsString()
  @MaxLength(255)
  author: string;

  @IsInt()
  @Min(0)
  @Max(10)
  rating: number;

  @IsDate()
  publishDate: Date;
}

const book = new Book();
book.title = 'Don Quixote';
book.author = 'Miguel De Cervantes';
book.rating = 11;
book.publishDate = new Date();

validate(book).then((errors: ValidationError[]) => {
  if (errors.length > 0) {
    console.warn('validate() - Validation failed. Errors: ', errors);
  }
});

validateOrReject(book).catch((errors: ValidationError[]) => {
  console.warn('validateOrReject() - Validation failed. Errors: ', errors);
});

awaitExample();

async function awaitExample() {
  try {
    await validateOrReject(book);
  } catch (errors) {
    console.warn('Async validateOrReject() - Validation failed. Errors: ', errors);
  }
}
PreviousbasicsNextintroduction

Last updated 3 years ago

Run this example on

Stackblitz