When adding a sequelize-typescript's decorator to the model field, which should check that the field is not empty, validation does not work and allows you to create a record without passing this field. DB is Postgres
Create function in client.repository.ts:
async create(values) {
return this.model.create(values, {
validate: true,
...options,
});
}
Client model:
import {
Table,
Column,
Model,
DataType,
PrimaryKey,
ForeignKey,
BelongsTo,
HasMany,
Unique,
IsEmail,
Length,
NotEmpty,
} from 'sequelize-typescript';
import { nanoid } from 'nanoid';
import { Company } from './company.model';
import { Invoice } from './invoice.model';
@Table
export class Client extends Model {
@PrimaryKey
@Column({
type: DataType.STRING,
defaultValue: nanoid,
})
id!: string;
@Unique
@NotEmpty
@IsEmail
@Column
email!: string;
@NotEmpty
@Length({ min: 3, max: 30 })
@Column
firstName!: string;
@NotEmpty
@Length({ min: 3, max: 30 })
@Column
lastName!: string;
@ForeignKey(() => Company)
@NotEmpty
@Column({
type: DataType.STRING,
})
companyId!: string;
@BelongsTo(() => Company)
company!: Company;
@HasMany(() => Invoice)
invoices?: Invoice[];
}
Why walidation doesn't work? P.S. If I send the firstName (for example) with a string length from 1 to 2 units, then the @Length({min: 3, max:30 }) validation works and an error is received.