最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sequelize.js - sequelize-typescript validation - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论