@join-com/typeorm-class-validator-is-uniq

uniq validator for typeorm and class-validator

Usage no npm install needed!

<script type="module">
  import joinComTypeormClassValidatorIsUniq from 'https://cdn.skypack.dev/@join-com/typeorm-class-validator-is-uniq';
</script>

README

IsUniq validator

Custom validator for class-validator and typeorm

It validates uniqueness of any value across all records in a database. The validation can be narrowed down to a scope based on another column. It doesn't consider nulls as unique values to be compatible with SQL specification

Installation

npm install @join-com/typeorm-class-validator-is-uniq --save

Usage

You can use the validator as any other class-validator:

@Entity()
class User {
  @PrimaryGeneratedColumn()
  public id: string;

  // Validates uniqueness of an email across all records
  @IsUniq()
  @Column()
  public email: string;

  // Validates uniqueness of a department in scope of a company
  @IsUniq({ scope: ['company'] })
  @Column({ nullable: true })
  public department: string;

  @Column({ nullable: true })
  public company: string;
}