Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!