sahilrajput03

Learn classvalidator

Learn NestJS: Click here

Performing mongodb id check on endpoints

image

How do we perform data-type conversions in params and query dto’s?

We can use Type decorator i.e, @Type from class-transformer.

From official docs:

image

image

Validating Arrays: Click here - If your field is an array and you want to perform validation of each item in the array you must specify a special each: true decorator option:

import { MinLength, MaxLength } from 'class-validator';

export class Post {
  @MaxLength(20, {
    each: true,
  })
  tags: string[];
}

Docs: Custom Validation Classes

import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';
import { LocationType } from '../types';

@ValidatorConstraint({ name: 'isValidMongoDbLocation', async: false })
export class IsValidMongoDbLocation implements ValidatorConstraintInterface {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
  validate(location: Partial<LocationType>, args: ValidationArguments) {
    return location?.type === 'Point'
    && location?.coordinates?.length === 2
    && location?.coordinates?.every((value) => typeof (value) === 'number');
  }

  // eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
  defaultMessage(args: ValidationArguments) {
    return "The 'location' field must have a shape of { type: 'Point', coordinates: [number, number] }";
  }
}