How to use the class-transformer.Transform function in class-transformer

To help you get started, we’ve selected a few class-transformer 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 EndyKaufman / ngx-dynamic-form-builder / apps / demo / src / app / shared / models / user.ts View on Github external
username: string;
  password: string;

  @IsEmail()
  @IsNotEmpty()
  email: string;
  isSuperuser: boolean;
  isStaff: boolean;

  @ValidateNested()
  @IsOptional()
  @Type(serializeModel(Department))
  department: Department = new Department();

  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  dateOfBirth: string;

  toString() {
    return this.username;
  }

  constructor(data?: any) {
    plainToClassFromExist(this, data);
  }
}
github rucken / todo-nestjs / frontend / libs / todo / core / src / lib / entities / models / task.ts View on Github external
description: string = undefined;
  @IsNotEmpty()
  @Type(serializeModel(Status))
  @Transform(transformStringToObject, { toPlainOnly: true })
  status: Status = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  openAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  closeAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  createdAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  updatedAt: Date = undefined;
  toString() {
    return this.title;
  }
  get range() {
    const arr: string[] = [];
    if (this.openAt) {
      arr.push(DateTime.fromJSDate(this.openAt).toLocaleString());
    }
    if (this.openAt && this.closeAt) {
      arr.push(' - ');
    }
    if (this.closeAt) {
      arr.push(DateTime.fromJSDate(this.closeAt).toLocaleString());
    }
    return arr.join('');
github EndyKaufman / ngx-dynamic-form-builder / apps / demo / src / app / shared / models / user.ts View on Github external
@IsNotEmpty()
  username: string;
  password: string;

  @IsEmail()
  @IsNotEmpty()
  email: string;
  isSuperuser: boolean;
  isStaff: boolean;

  @ValidateNested()
  @IsOptional()
  @Type(serializeModel(Department))
  department: Department = new Department();

  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  dateOfBirth: string;

  toString() {
    return this.username;
  }

  constructor(data?: any) {
    plainToClassFromExist(this, data);
  }
}
github rucken / core / libs / rucken / core / src / lib / entities / models / user.ts View on Github external
@IsEmail(undefined)
  email: string = undefined;
  isSuperuser = false;
  isStaff = false;
  isActive = false;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  lastLogin: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  dateJoined: Date = undefined;
  @IsOptional()
  @Type(serializeModel(Group))
  groups: Group[] = [];
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  dateOfBirth: Date = undefined;
  get permissionNames() {
    const permissions = [];
    if (this.groups) {
      this.groups.forEach(group => group.permissions.forEach(permission => permissions.push(permission.name)));
    }
    return permissions;
  }
  toString() {
    return `${this.firstName} ${this.lastName} (${this.username})`.trim();
  }
  get fullName() {
    return `${this.firstName} ${this.lastName}`.trim();
  }
  get rolesAsString() {
    const roles: string[] = [];
github rucken / core / libs / rucken / core / src / lib / entities / models / user.ts View on Github external
groups: Group
  };
  id: number = undefined;
  @IsNotEmpty()
  username: string = undefined;
  password: string = undefined;
  rePassword: string = undefined;
  firstName: string = undefined;
  lastName: string = undefined;
  @IsNotEmpty()
  @IsEmail(undefined)
  email: string = undefined;
  isSuperuser = false;
  isStaff = false;
  isActive = false;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  lastLogin: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  dateJoined: Date = undefined;
  @IsOptional()
  @Type(serializeModel(Group))
  groups: Group[] = [];
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  dateOfBirth: Date = undefined;
  get permissionNames() {
    const permissions = [];
    if (this.groups) {
      this.groups.forEach(group => group.permissions.forEach(permission => permissions.push(permission.name)));
    }
github rucken / todo-nestjs / frontend / libs / todo / core / src / lib / entities / models / task.ts View on Github external
id: number = undefined;
  @IsNotEmpty()
  @Type(serializeModel(Project))
  @Transform(transformStringToObject, { toPlainOnly: true })
  project: Project = undefined;
  @IsNotEmpty()
  title: string = undefined;
  description: string = undefined;
  @IsNotEmpty()
  @Type(serializeModel(Status))
  @Transform(transformStringToObject, { toPlainOnly: true })
  status: Status = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  openAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  closeAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  createdAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  updatedAt: Date = undefined;
  toString() {
    return this.title;
  }
  get range() {
    const arr: string[] = [];
    if (this.openAt) {
      arr.push(DateTime.fromJSDate(this.openAt).toLocaleString());
    }
github chanlito / nestjs-extensions / src / transform / transform.decorators.ts View on Github external
export function TransformBoolean(): any {
  return Transform(v => v === 'true' || v === 'True' || v === 1 || v === '1');
}
github chanlito / nestjs-extensions / src / transform / transform.decorators.ts View on Github external
export function TransformNumber(opt?: TransformNumberOptions): any {
  const { defaultValue, max, min } = opt || ({} as TransformNumberOptions);
  return Transform(v => (isNaN(v) ? defaultValue : max && v > max ? max : min && v < min ? min : +v));
}
github rucken / core / libs / rucken / core / src / lib / models / permission.ts View on Github external
name: translate('Name'),
    title: translate('Title'),
    contentType: translate('Content type'),
    createTitle: translate('Add new permission'),
    viewTitle: translate('Permission #{{id}}'),
    updateTitle: translate('Update permission #{{id}}'),
    deleteTitle: translate('Delete permission #{{id}}'),
    deleteMessage: translate('Do you really want to delete permission?'),
    selectTitle: translate('Select permission')
  };
  id: number = undefined;
  @IsNotEmpty()
  name: string = undefined;
  title: string = undefined;
  @Type(serializeModel(ContentType))
  @Transform(transformStringToObject, { toPlainOnly: true })
  contentType: ContentType = undefined;
  get contentTypeAsString() {
    if (this.contentType) {
      return this.contentType;
    }
    return undefined;
  }
  toString() {
    return this.title;
  }
}
github rucken / todo-nestjs / frontend / libs / todo / core / src / lib / entities / models / task.ts View on Github external
status: translate('Status'),
    openAt: translate('Open at'),
    closeAt: translate('Close at'),
    createdAt: translate('Created at'),
    updatedAt: translate('Updated at'),
    range: translate('Range'),
    createTitle: translate('Add new task'),
    viewTitle: translate('Task #{{id}}'),
    updateTitle: translate('Update task #{{id}}'),
    deleteTitle: translate('Delete task #{{id}}'),
    deleteMessage: translate('Do you really want to delete task?')
  };
  id: number = undefined;
  @IsNotEmpty()
  @Type(serializeModel(Project))
  @Transform(transformStringToObject, { toPlainOnly: true })
  project: Project = undefined;
  @IsNotEmpty()
  title: string = undefined;
  description: string = undefined;
  @IsNotEmpty()
  @Type(serializeModel(Status))
  @Transform(transformStringToObject, { toPlainOnly: true })
  status: Status = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  openAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })
  closeAt: Date = undefined;
  @Transform(transformStringToDate, { toClassOnly: true })
  @Transform(transformDateToString, { toPlainOnly: true })