How to use the class-validator.ValidationError function in class-validator

To help you get started, we’ve selected a few class-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hackoregon / openelections / api / models / entity / Campaign.ts View on Github external
async validateGovernmentAsync() {
        const g = await this.government;
        if (!g) {
            const error = new ValidationError();
            error.property = 'governmentId';
            error.constraints = { isDefined: 'governmentId should not be null or undefined' };
            this.errors.push(error);
        }
    }
github hackoregon / openelections / api / models / entity / Contribution.ts View on Github external
validateName() {
        if (this.contributorType === ContributorType.INDIVIDUAL) {
            if (!this.contrLast || this.contrLast.trim() === '') {
                const error = new ValidationError();
                error.property = 'contrLast';
                error.constraints = { isDefined: 'contrLast should not be null or undefined' };
                this.errors.push(error);
            }

            if (!this.contrFirst || this.contrFirst.trim() === '') {
                const error = new ValidationError();
                error.property = 'contrFirst';
                error.constraints = { isDefined: 'contrFirst should not be null or undefined' };
                this.errors.push(error);
            }
        } else {
            if (!this.contrName || this.contrName.trim() === '') {
                const error = new ValidationError();
                error.property = 'contrName';
                error.constraints = { isDefined: 'contrName should not be null or undefined' };
                this.errors.push(error);
            }
        }
    }
github mgm-interns / team-radio / server / src / services / crud / UserCRUDService.ts View on Github external
name?: string,
    city?: string,
    country?: string,
    avatarUrl?: string,
    coverUrl?: string
  ): Promise {
    if (!email && !username) throw new ForbiddenException('Username OR email is required');

    const existedUser = await this.userRepository.findByUsernameOrEmail({ email, username });
    if (existedUser) throw new ForbiddenException('Username or Email has been taken.');

    const user = this.userRepository.create({ email, firstname, lastname, name, city, country, avatarUrl, coverUrl });
    if (password) {
      if (password.length >= 6 && password.length <= 32) user.generatePassword(password);
      else {
        const error = new ValidationError();
        error.property = 'password';
        error.constraints = {
          minLength: 'Password must be longer than 6 characters',
          maxLength: 'Password must not be longer than 32 characters'
        };
        error.value = password;
        error.children = [];
        throw new UnprocessedEntityException('Can not save User', [error]);
      }
    }
    if (username) user.username = username;
    else user.generateUsernameFromEmail();

    return this.userRepository.saveOrFail(user);
  }
github hackoregon / openelections / api / models / entity / Contribution.ts View on Github external
async validateGovernmentAsync() {
        const g = await this.government;
        if (!g) {
            const error = new ValidationError();
            error.property = 'governmentId';
            error.constraints = { isDefined: 'governmentId should not be null or undefined' };
            this.errors.push(error);
        }
    }
github hackoregon / openelections / api / models / entity / Activity.ts View on Github external
async validateUserAsync() {
        const u = await this.user;
        if (!u) {
            const error = new ValidationError();
            error.property = 'userId';
            error.constraints = {isDefined: 'userId should not be null or undefined'};
            this.errors.push(error);
        }
    }
}
github hackoregon / openelections / api / models / entity / Permission.ts View on Github external
async validateUserAsync() {
        const u = await this.user;
        if (!u) {
            const error = new ValidationError();
            error.property = 'userId';
            error.constraints = {isDefined: 'userId should not be null or undefined'};
            this.errors.push(error);
        }
    }
github mgm-interns / team-radio / server / src / services / crud / UserCRUDService.ts View on Github external
coverUrl?: string
  ): Promise {
    const user = await this.userRepository.findOneOrFail(id);
    if (username) user.username = username;
    if (email) user.email = email;
    if (firstname) user.firstname = firstname;
    if (lastname) user.lastname = lastname;
    if (name) user.name = name;
    if (city) user.city = city;
    if (country) user.country = country;
    if (avatarUrl) user.avatarUrl = avatarUrl;
    if (coverUrl) user.coverUrl = coverUrl;
    if (password) {
      if (password.length >= 6 && password.length <= 32) user.generatePassword(password);
      else {
        const error = new ValidationError();
        error.property = 'password';
        error.constraints = {
          minLength: 'Password must be longer than 6 characters',
          maxLength: 'Password must not be longer than 32 characters'
        };
        error.value = password;
        error.children = [];
        throw new UnprocessedEntityException('Can not save User', [error]);
      }
    }
    return this.userRepository.saveOrFail(user);
  }
github hackoregon / openelections / api / models / entity / Permission.ts View on Github external
async validateCampaignAsync() {
        const c = await this.campaign;
        if (!c && this.role !== UserRole.GOVERNMENT_ADMIN) {
            const error = new ValidationError();
            error.property = 'campaignId';
            error.constraints = {isDefined: 'campaignId should not be null or undefined'};
            this.errors.push(error);
        } else if (c && this.role === UserRole.GOVERNMENT_ADMIN) {
            const error = new ValidationError();
            error.property = 'campaignId';
            error.constraints = {notAllowed: 'campaignId cannot be set with GovernmentAdmin as a UserRole'};
            this.errors.push(error);
        }
    }