How to use the class-validator.ValidatorConstraint 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 jmcdo29 / zeldaPlay / src / server / app / decorators / containsNumbericCharacter.decorator.ts View on Github external
import {
  registerDecorator,
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator';

@ValidatorConstraint()
export class HasNumberConstraint implements ValidatorConstraintInterface {
  validate(value: string, args: ValidationArguments): boolean {
    return /\d+/.test(value);
  }
}

export function HasNumber(validationOptions?: ValidationOptions) {
  return (object: object, propertyName: string) => {
    registerDecorator({
      name: 'HasNumber',
      target: object.constructor,
      propertyName,
      options: validationOptions,
      validator: HasNumberConstraint
    });
  };
github jmcdo29 / zeldaPlay / src / server / app / decorators / containsLowerCase.decorator.ts View on Github external
import {
  registerDecorator,
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator';

@ValidatorConstraint()
export class HasLowerCaseConstraint implements ValidatorConstraintInterface {
  validate(value: string, args: ValidationArguments): boolean {
    return /[a-z]+/.test(value);
  }
}

export function HasLowerCase(validationOptions?: ValidationOptions) {
  return (object: object, propertyName: string) => {
    registerDecorator({
      name: 'HasLowerCase',
      target: object.constructor,
      propertyName,
      options: validationOptions,
      validator: HasLowerCaseConstraint
    });
  };
github jmcdo29 / zeldaPlay / src / server / app / decorators / containsSpecialCharacter.decorator.ts View on Github external
import {
  registerDecorator,
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator';

@ValidatorConstraint()
export class HasSpecialCharacterConstraint
  implements ValidatorConstraintInterface {
  validate(value: string, args: ValidationArguments): boolean {
    return /[!@#$%^&*\(\)\-\_+]+/.test(value);
  }
}

export function HasSpecialCharacter(validationOptions?: ValidationOptions) {
  return (object: object, propertyName: string) => {
    registerDecorator({
      name: 'HasSpecialCharacter',
      target: object.constructor,
      propertyName,
      options: validationOptions,
      validator: HasSpecialCharacterConstraint
    });
github jmcdo29 / zeldaPlay / apps / api / src / app / validators / isCustomId.ts View on Github external
import {
  registerDecorator,
  ValidationArguments,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface,
} from 'class-validator';

@ValidatorConstraint()
export class IsCustomIdConstrain implements ValidatorConstraintInterface {
  validate(value: string, args: ValidationArguments) {
    const id = value;
    return (
      id.startsWith(args.constraints[0]) &&
      /^[a-z0-9]{8}-[a-z0-9]{4}-4[a-z0-9]{3}-[ab89][a-z0-9]{3}-[a-z0-9]{12}$/.test(
        id.substring(args.constraints[0].length),
      )
    );
  }
}

export function IsCustomId(
  property: string,
  validationOptions?: ValidationOptions,
) {
github rucken / core / libs / rucken / core / src / lib / validators / equals-to-other-property.validator.ts View on Github external
import {
  ValidatorConstraint,
  ValidatorConstraintInterface,
  ValidationArguments
} from 'class-validator';
import { translate } from '../utils/translate';

@ValidatorConstraint()
export class EqualsToOtherProperty implements ValidatorConstraintInterface {
  validate(text: string, args: ValidationArguments) {
    const otherProperty =
      args.constraints && args.constraints.length ? args.constraints[0] : '';
    return args.object[otherProperty] === args.value;
  }
  defaultMessage(args: ValidationArguments) {
    return translate('$property must be equal to $constraint1');
  }
}
github bradymholt / koa-vuejs-template / api / models / Validators.ts View on Github external
export function IsEmail(
  options?: IsEmailOptions,
  validationOptions?: ValidationOptions
) {
  return classValidator.IsEmail(
    options,
    Object.assign(
      {
        message: "must be a valid email address"
      },
      validationOptions
    )
  );
}

@ValidatorConstraint({ async: true })
export class IsUserAlreadyExistByEmailConstraint
  implements ValidatorConstraintInterface {
  validate(email: any, args: ValidationArguments) {
    return getConnection().getRepository(User).findOne({ email }).then(user => {
      return !user;
    });
  }
}

export function IsUserAlreadyExistByEmail(
  validationOptions?: ValidationOptions
) {
  return function(object: Object, propertyName: string) {
    registerDecorator({
      target: object.constructor,
      propertyName: propertyName,
github kazekyo / nestjs-graphql-relay / src / common / connection-paging.ts View on Github external
} from 'class-validator';
import { FindManyOptions, Repository } from 'typeorm';

@ObjectType()
export class PageInfo implements Relay.PageInfo {
  @Field(type => Boolean, { nullable: true })
  hasNextPage?: boolean | null;
  @Field(type => Boolean, { nullable: true })
  hasPreviousPage?: boolean | null;
  @Field(type => String, { nullable: true })
  startCursor?: Relay.ConnectionCursor | null;
  @Field(type => String, { nullable: true })
  endCursor?: Relay.ConnectionCursor | null;
}

@ValidatorConstraint({ async: false })
class CannotUseWithout implements ValidatorConstraintInterface {
  validate(value: any, args: ValidationArguments) {
    const object = args.object as any;
    const required = args.constraints[0] as string;
    return object[required] !== undefined;
  }

  defaultMessage(args: ValidationArguments) {
    return `Cannot be used without \`${args.constraints[0]}\`.`;
  }
}

@ValidatorConstraint({ async: false })
class CannotUseWith implements ValidatorConstraintInterface {
  validate(value: any, args: ValidationArguments) {
    const object = args.object as any;
github NationalBankBelgium / stark / packages / stark-core / src / validation / decorators / is-kbo / is-kbo.validator.decorator.ts View on Github external
import { getFromContainer, registerDecorator, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";

import { StarkValidatorImpl } from "../../validator";
import { StarkValidator } from "../../validator.intf";
import { starkIsKBOValidatorName } from "../../validators/is-kbo";

/**
 * StarkIsKbo validator constraint
 * Validates that the KBO number provided is valid
 */
@ValidatorConstraint({ name: starkIsKBOValidatorName, async: false })
class StarkIsKBOConstraint implements ValidatorConstraintInterface {
	/**
	 * Validates that the given KBO number is valid
	 * @param kbo - the kbo to validate
	 * @returns boolean - true if the kbo has been validated
	 */
	public validate(kbo: string): boolean {
		const validator: StarkValidator = getFromContainer(StarkValidatorImpl);

		return validator.starkIsKBO(kbo);
	}

	/**
	 * Default message displayed if the KBO number is not valid
	 * @returns a default message
	 */
github balmbees / corgi / src / route_factories / presenter_route / class_validator / validate_primitive_array.ts View on Github external
import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";

@ValidatorConstraint({ name: "primitiveArrayValidation", async: false })
export class ValidatePrimitiveArray implements ValidatorConstraintInterface {
  public validate(value: any, args: ValidationArguments) {
    const [ elementClass ] = args.constraints;

    return value instanceof Array &&
      value.every((el) => el && el.constructor === elementClass);
  }
}
github kazekyo / nestjs-graphql-relay / src / common / connection-paging.ts View on Github external
}

@ValidatorConstraint({ async: false })
class CannotUseWithout implements ValidatorConstraintInterface {
  validate(value: any, args: ValidationArguments) {
    const object = args.object as any;
    const required = args.constraints[0] as string;
    return object[required] !== undefined;
  }

  defaultMessage(args: ValidationArguments) {
    return `Cannot be used without \`${args.constraints[0]}\`.`;
  }
}

@ValidatorConstraint({ async: false })
class CannotUseWith implements ValidatorConstraintInterface {
  validate(value: any, args: ValidationArguments) {
    const object = args.object as any;
    const result = args.constraints.every(propertyName => {
      return object[propertyName] === undefined;
    });
    return result;
  }

  defaultMessage(args: ValidationArguments) {
    return `Cannot be used with \`${args.constraints.join('` , `')}\`.`;
  }
}

@ArgsType()
export class ConnectionArgs implements Relay.ConnectionArguments {