4

例如,对于数据库行,我们可能需要不能未定义的可为空属性:

class DbRow {
  @IsNumber()
  id!: number;

  @IsNumber()
  numNullable!: number | null;
}

所以numNullable可以是一个数字或null- 但它绝不能是undefined

我们如何在类验证器中表达这一点?

  • 添加@Optional()不起作用,因为这也将允许undefined
  • 我对自定义验证器也没有运气
4

4 回答 4

9

事实证明,这可以通过使用条件验证来 ValidateIf实现:

class DbRow {
  @IsNumber()
  id!: number;

  @IsNumber()
  @ValidateIf((object, value) => value !== null)
  numNullable!: number | null;
}

这是一个stackblitz示例

于 2020-04-18T11:44:39.650 回答
0

这是库的限制,它不允许条件分支。最好的方法是编写自己的只允许空值的验证器。

于 2020-04-14T15:48:07.507 回答
0

这是我的解决方案:

import { ValidationOptions, ValidateIf } from 'class-validator';
export function IsNullable(validationOptions?: ValidationOptions) {
  return ValidateIf((_object, value) => value !== null, validationOptions);
}

用法

import { plainToClass } from 'class-transformer';
import { IsNumber, validateSync } from 'class-validator';
import { IsNullable } from 'src/common/utils/is-nullable.decorator';
class SampleDto {
  @IsNullable()
  @IsNumber()
  foo: number | null;
}
describe('IsNullable', () => {
  it('should disable other validators when given property is null', () => {
    expect(validateSync(plainToClass(SampleDto, { foo: null }))).toEqual([]);
  });
  it('should allow other validators to work when given property is not null', () => {
    expect(validateSync(plainToClass(SampleDto, { foo: 1 }))).toEqual([]);
    expect(validateSync(plainToClass(SampleDto, { foo: '1' }))[0].constraints.isNumber).toMatch('foo must be a number');
  });
  it('should not allow undefined', () => {
    expect(validateSync(plainToClass(SampleDto, { foo: undefined })).length).toBeGreaterThan(0);
  });
});
于 2022-02-10T04:29:15.083 回答
0

这是IsOptional从导出的扩展版本class-validator

import {
  ValidationOptions,
  ValidateIf,
  IsOptional as IsOptionalValidator,
} from 'class-validator';

/**
 * Checks if value is missing and if so, ignores all validators.
 *
 * @param nullable If `true`, all other validators will be skipped even when the value is `null`. `false` by default.
 * @param validationOptions {@link ValidationOptions}
 *
 * @see IsOptional exported from `class-validator.
 */
export function IsOptional(
  nullable = false,
  validationOptions?: ValidationOptions,
) {
  if (nullable) {
    return IsOptionalValidator(validationOptions);
  }

  return ValidateIf((ob: any, v: any) => {
    return v !== undefined;
  }, validationOptions);
}
于 2022-03-04T15:54:05.693 回答