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

334
Vistas
Typescript Class-Validator Contains multiple strings (IsEmail)

I have been using the class-validator decorator library to validate dto objects and would like to create a domain whitelist for the @IsEmail decorator. The library includes a @Contains decorator, which can be used for a single string (seed), but I would like to input an array of valid strings instead. Is this possible?

current

  @Contains('@mydomain.com')
  @IsEmail()
  public email: string;

desired

  @Contains(['@mydomain, @yourdomain, @wealldomain, @fordomain'])
  @IsEmail()
  public email: string;

If this is not possible, I would appreciate any direction on how to implement a custom decorator which serves this purpose.

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

COnsider this example:

const Contains = <Domain extends `@${string}`, Domains extends Domain[]>(domains: [...Domains]) =>
    (target: Object, propertyKey: string) => {
        let value: string;
        const getter = () => value
        const setter = function (newVal: Domain) {
            if (domains.includes(newVal)) {
                value = newVal;
            }
            else {
                throw Error(`Domain should be one of ${domains.join()}`)
            }
        };
        Object.defineProperty(target, propertyKey, {
            get: getter,
            set: setter
        });
    }

class Greeter {
    @Contains(['@mydomain', '@wealldomain'])
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }

}

const ok = new Greeter('@mydomain'); // ok
const error = new Greeter('234'); // runtime error

Playground

If you provide invalid email domain it will trigger runtime error. It is up to you how you want to implement validation logic. It might be not necessary a runtime error

about 4 years ago · Juan Pablo Isaza 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