Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
Is there a class-validator decorator that rejects strings containing emojis?

So, i'm building a REST API and I need to verify if there is any emojis in the request body, and if there is I need to throw an expection.

I'm using class-validator lib to do all the verification:

export class CreateShopkeeperDto {
  @IsNotEmpty()
  @IsEmail()
  email: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(64)
  corporateName: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(64)
  comercialName: string;
}
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

You need to create a custom validation class

Here's how I think it would go, but this is not tested by me.

import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';

@ValidatorConstraint({ name: 'containsNoEmoji', async: false })
export class ContainsNoEmoji implements ValidatorConstraintInterface {
  validate(text: string, args: ValidationArguments) {
    // you probably want a library that has an updated version whenever the unicode specification adds more emoji
    const emojiRegex = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/gi; // taken from https://dev.to/melvin2016/how-to-check-if-a-string-contains-emojis-in-javascript-31pe
    return !emojiRegex.test(text);
  }

  defaultMessage(args: ValidationArguments) {
    // here you can provide default error message if validation failed
    return 'Text ($value) contains emoji!';
  }
}

Then you would use it like this:

import { ContainsNoEmoji } from "../path/to/ContainsNoEmoji";
import { Validate } from 'class-validator';

export class CreateShopkeeperDto {
  @IsNotEmpty()
  @IsEmail()
  @Validate(ContainsNoEmoji)
  email: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(64)
  @Validate(ContainsNoEmoji)
  corporateName: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(64)
  @Validate(ContainsNoEmoji)
  comercialName: string;
}
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda