How to use the class-validator.Validate 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 kazekyo / nestjs-graphql-relay / src / common / connection-paging.ts View on Github external
@Field(type => String, {
    nullable: true,
    description: 'Paginate before opaque cursor',
  })
  @ValidateIf(o => o.before !== undefined)
  @Validate(CannotUseWithout, ['last'])
  @Validate(CannotUseWith, ['after', 'first'])
  before?: Relay.ConnectionCursor;

  @Field(type => String, {
    nullable: true,
    description: 'Paginate after opaque cursor',
  })
  @ValidateIf(o => o.after !== undefined)
  @Validate(CannotUseWithout, ['first'])
  @Validate(CannotUseWith, ['before', 'last'])
  after?: Relay.ConnectionCursor;

  @Field(type => Int, { nullable: true, description: 'Paginate first' })
  @ValidateIf(o => o.first !== undefined)
  @Min(1)
  @Validate(CannotUseWith, ['before', 'last'])
  first?: number;

  @Field(type => Int, { nullable: true, description: 'Paginate last' })
  @ValidateIf(o => o.last !== undefined)
  // Required `before`. This is a weird corner case.
  // We'd have to invert the ordering of query to get the last few items then re-invert it when emitting the results.
  // We'll just ignore it for now.
  @Validate(CannotUseWithout, ['before'])
  @Validate(CannotUseWith, ['after', 'first'])
  @Min(1)
github EndyKaufman / ngx-dynamic-form-builder / apps / demo / src / app / shared / models / company.ts View on Github external
import { Validate, IsNotEmpty, MaxLength, Min, Max, IsOptional } from 'class-validator';
import { plainToClassFromExist } from 'class-transformer';
import { TextLengthMore15 } from '../utils/custom-validators';

export class Company {
  static strings = {
    id: 'Id',
    name: 'Name',
    regionNum: 'Region num'
  };

  id: number;

  @Validate(TextLengthMore15, {
    message: 'The company name must be longer than 15'
  })
  @IsNotEmpty()
  @MaxLength(20)
  name: string;

  @IsOptional()
  @Min(1)
  @Max(99)
  regionNum: number;

  toString() {
    const arr: string[] = [];
    if (arr.length === 0 && this.name) {
      arr.push(this.name);
    }
github EndyKaufman / ngx-dynamic-form-builder / apps / demo / src / app / shared / models / project.ts View on Github external
export class Project {
  id?: number = undefined;

  @IsNotEmpty({
    groups: [ProjectPanelStepsEnum.Step1]
  })
  name?: string = undefined;

  @IsNotEmpty({ always: true })
  description?: string = undefined;

  @ValidateNested({
    groups: [ProjectPanelStepsEnum.Step2]
  })
  @Validate(ObjectMustBeNotEmpty, [1, 3], {
    groups: [ProjectPanelStepsEnum.Step2],
    message: 'Tasks not initialized or min length = 1 and max length = 3, and all initialized tasks must be not empty'
  })
  @IsOptional()
  @Type(serializeModel(Task))
  tasks?: Task[] = [];

  toString() {
    return `Project #${this.id} ${this.name}`;
  }
}
github neoteric-eu / nestjs-auth / src / app / user / entity / user.entity.ts View on Github external
public id: string;

	@ApiModelProperty()
	@IsString()
	@Column()
	public first_name: string;

	@ApiModelProperty()
	@IsString()
	@Column()
	public last_name: string;

	@ApiModelProperty()
	@IsEmail()
	@ValidateIf(o => !o.id)
	@Validate(IsUserAlreadyExist, {
		message: 'User already exists'
	})
	@Column()
	public email: string;

	@ApiModelProperty()
	@IsString()
	@IsOptional()
	@Column()
	public phone_num: string;

	@ApiModelProperty()
	@IsOptional()
	@IsUrl()
	@Column()
	public profile_img: string;
github xmlking / ngx-starter-kit / apps / api / src / app / user / dto / update-user.dto.ts View on Github external
import { IsEmail, IsNotEmpty, IsString, Length, Validate } from 'class-validator';
import { IsEmailUnique } from '../validator/is-email-unique.validator';

export class UpdateUserDto {
  @IsString()
  @IsNotEmpty()
  readonly firstName: string;

  @IsString()
  @IsNotEmpty()
  readonly lastName: string;

  @IsEmail()
  @IsNotEmpty()
  @Length(10, 100)
  @Validate(IsEmailUnique)
  readonly email: string;
}
github EndyKaufman / ngx-dynamic-form-builder / apps / demo / src / app / shared / models / exp-company.ts View on Github external
import { Validate, IsNotEmpty, Min, Max, MaxLength, IsOptional } from 'class-validator';
import { TextLengthMore15 } from '../utils/custom-validators';

export class ExpCompany {
  id: number;

  @Validate(TextLengthMore15, {
    groups: ['user'],
    message: 'The company name must be longer than 15'
  })
  @IsNotEmpty({
    groups: ['user']
  })
  @MaxLength(20)
  name: string;

  @IsOptional()
  @Min(1)
  @Max(99)
  regionNum: number;

  constructor(data?: any) {
    if (data === undefined) {
github xmlking / ngx-starter-kit / apps / api / src / app / user / dto / create-user.dto.ts View on Github external
import { IsAscii, IsEmail, IsNotEmpty, IsString, Length, Validate } from 'class-validator';
import { IsEmailUnique } from '../validator/is-email-unique.validator';

export class CreateUserDto {
  @IsString()
  @IsNotEmpty()
  readonly firstName: string;

  @IsString()
  @IsNotEmpty()
  readonly lastName: string;

  @IsEmail()
  @IsNotEmpty()
  @Length(10, 100)
  @Validate(IsEmailUnique)
  readonly email: string;

  @IsAscii()
  @Length(8, 20)
  @IsString()
  @IsNotEmpty()
  readonly username: string;
}
github w3tecch / express-typescript-boilerplate / src / api / requests / user / UserUpdateRequest.ts View on Github external
* all criteria are given
 *
 * @export
 * @class UserUpdateRequest
 * @extends {RequestBody}
 */
export class UserUpdateRequest extends RequestBody {

    @IsNotEmpty()
    public firstName: string;

    @IsNotEmpty()
    public lastName: string;

    @IsEmail()
    @Validate(EndsWithValidator, ['@gmail.com', '@w3tec.ch'])
    public email: string;

    @IsNotEmpty()
    public picture: string;

    @IsNotEmpty()
    public auth0UserId: string;

    /**
     * We override the validate method so we can skip the missing
     * properties.
     */
    public validate(): Promise {
        return super.validate(true);
    }
github rucken / core / libs / rucken / core / src / lib / modules / auth-modal / auth-modal.model.ts View on Github external
@IsNotEmpty({
    groups: [AuthModalTypeEnum.SignUp.toString()]
  })
  username: string = undefined;
  @IsNotEmpty({
    groups: [AuthModalTypeEnum.SignIn.toString(), AuthModalTypeEnum.SignUp.toString()]
  })
  @IsEmail(undefined, {
    groups: [AuthModalTypeEnum.SignIn.toString(), AuthModalTypeEnum.SignUp.toString()]
  })
  email: string = undefined;
  @IsNotEmpty({
    groups: [AuthModalTypeEnum.SignIn.toString(), AuthModalTypeEnum.SignUp.toString()]
  })
  password: string = undefined;
  @Validate(EqualsToOtherProperty, ['password'], {
    groups: [AuthModalTypeEnum.SignUp.toString()]
  })
  @IsNotEmpty({
    groups: [AuthModalTypeEnum.SignUp.toString()]
  })
  rePassword: string = undefined;
}
github w3tecch / express-typescript-boilerplate / src / api / requests / user / UserCreateRequest.ts View on Github external
*
 * @export
 * @class UserCreateRequest
 * @extends {RequestBody}
 */
export class UserCreateRequest extends RequestBody {

    @IsNotEmpty()
    public firstName: string;

    @IsNotEmpty()
    public lastName: string;

    @IsNotEmpty()
    @IsEmail()
    @Validate(EndsWithValidator, ['@gmail.com', '@w3tec.ch'])
    public email: string;

    public picture: string;
    public auth0UserId: string;

}