How to use the class-validator.validateOrReject 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 goparrot / geocoder / src / util / world-country-state / world-country-state-util.ts View on Github external
static async find(_query: WorldCountryStateQueryInterface): Promise {
        const query: WorldCountryStateQuery = plainToClass(WorldCountryStateQuery, _query);

        // clear undefined/empty values
        for (const key of Object.keys(query)) {
            if (!query[key]) {
                delete query[key];
            }
        }

        try {
            await validateOrReject(query, {
                whitelist: true,
                forbidNonWhitelisted: true,
                validationError: { target: false, value: false },
            });
        } catch (err) {
            return;
        }

        const stateData: WorldCountryStateInterface | undefined = (countryStates as WorldCountryStateInterface[]).find(
            (plainState: WorldCountryStateInterface) => {
                const state: WorldCountryState = plainToClass(WorldCountryState, plainState);

                return WorldCountryStateUtil.match(state, query);
            },
        );
github te-emprego / api / src / modules / Users / methods / UpdateInfo.method.ts View on Github external
private validateInput = async (): Promise => {
    try {
      const validation = new InputValidation()

      validation.name = this.newProps.name
      validation.phone = this.newProps.phone
      validation.address = this.newProps.address
      validation.avatar = this.newProps.avatar
      validation.birthdate = this.newProps.birthdate

      await validateOrReject(validation)
    } catch (error) {
      throw new this.HttpException(400, 'invalid inputs', error)
    }
  }
github typestack / routing-controllers / src / ActionParameterHandler.ts View on Github external
protected validateValue(value: any, paramMetadata: ParamMetadata): Promise|any {
        const isValidationEnabled = (paramMetadata.validate instanceof Object || paramMetadata.validate === true)
            || (this.driver.enableValidation === true && paramMetadata.validate !== false);
        const shouldValidate = paramMetadata.targetType
            && (paramMetadata.targetType !== Object)
            && (value instanceof paramMetadata.targetType);

        if (isValidationEnabled && shouldValidate) {
            const options = paramMetadata.validate instanceof Object ? paramMetadata.validate : this.driver.validationOptions;
            return validate(value, options)
                .then(() => value)
                .catch((validationErrors: ValidationError[]) => {
                    const error: any = new BadRequestError(`Invalid ${paramMetadata.type}, check 'errors' property for more info.`);
                    error.errors = validationErrors;
                    error.paramName = paramMetadata.name; 
                    throw error;
                });
        }

        return value;
    }
github slively / fullstack-typescript / server / src / todos / todosService.ts View on Github external
update(todo: Partial): Promise {
		return validateOrReject('todo', todo, { skipMissingProperties: true })
			.then(() => todosDao.update(todo));
	},
github te-emprego / api / src / modules / Users / methods / Login.method.ts View on Github external
private validateInput = async (): Promise => {
    try {
      const validation = new InputValidation()
      validation.email = this.credentials.email
      validation.password = this.credentials.password
      await validateOrReject(validation)
    } catch (error) {
      throw new this.HttpException(400, 'invalid inputs')
    }
  }
github te-emprego / api / src / modules / Users / methods / Create.method.ts View on Github external
private validateInput = async (): Promise => {
    this.validation.name = this.user.name
    this.validation.email = this.user.email
    this.validation.password = this.user.password

    await validateOrReject(this.validation)
      .catch((errors): void => {
        throw new this
          .HttpException(this.status, 'input validation error', errors)
      })
  }
github slively / fullstack-typescript / server / src / todos / todosService.ts View on Github external
create(todo: CreateTodoEntity): Promise {
		return validateOrReject('todo', todo)
			.then(() => todosDao.create(todo));
	},
github goparrot / geocoder / src / command / abstract.command.ts View on Github external
async execute(_query: GeocoderQueryType): Promise {
        const query: GeocoderQueryType = plainToClass(this.constructor.queryClass(), _query);

        try {
            await validateOrReject(query, {
                whitelist: true,
                forbidNonWhitelisted: true,
                validationError: { target: false, value: false },
            });
        } catch (err) {
            this.getLogger().error(err, query);

            throw new ValidationException(err);
        }

        if (query.accuracy && !this.constructor.isProvidesAccuracy(query.accuracy)) {
            throw new UnsupportedAccuracyException(
                `Command ${this.constructor.name} doesn't support "${query.accuracy}" accuracy (max accuracy is "${this.constructor.getMaxAccuracy()}")`,
            );
        }