我想对请求有效负载应用验证,例如有字符串类型的字段名称。但名称不是必填字段,但如果存在,则必须执行@IsNotEmpty()
我试过这样的事情
@IsNotEmpty() name?: string//它不考虑?可选约束
我正在尝试使用 aValidationPipe但无论我如何编写代码,在发送请求时都会收到以下警告:No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.
我的路线看起来像这样:
@Get()
@UsePipes(new ValidationPipe({ transform: true }))
async findAll(@Query() queryDto: QueryDto) {
return await this.myService.findAll(queryDto);
}
Run Code Online (Sandbox Code Playgroud)
我的 DTO 看起来像这样:
export class queryDto
{
@ApiModelProperty({
description: 'Maximum number of results',
type: Number,
example: 50,
default: 50,
required: false
})
readonly limit: number = 50;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用ValidationPipe多种方法,遵循doc,但没有任何效果对我有用。我知道这是行不通的,因为虽然请求得到响应,默认值,我在我的DTO写的财产limit,这是50,是不是当查询是空的使用。因此,当limit查询中提供no 时,limit …
我的客户将请求多个数字,我需要相互比较值。
我想要如下代码。
const validator: SomeValidator = new SomeValidator;
validator.first = 1000;
validator.second = 2000; // this will be greater than validator.first
validator.third = 3000; // this will be greater than validator.second
Run Code Online (Sandbox Code Playgroud)
我尝试将变量输入到 @Min() 验证器中,如下所示
import { IsInt, Min } from 'class-validator';
class SomeValidator {
@IsInt()
@Min(0)
public first: number;
@IsInt()
@Min(this.first) // it occurs TS7017
public second: number;
@IsInt()
@Min(this.second) // it occurs TS7017
public third: number;
}
Run Code Online (Sandbox Code Playgroud)
我收到TS7017 错误。
TS7017: Element implicitly has an 'any' type …Run Code Online (Sandbox Code Playgroud) If I want to validate the role which user post to api to create if is unique of relation enterprise
@Injectable({ scope: Scope.REQUEST })
@ValidatorConstraint({ name: 'Ddd', async: true })
export class IsUnqiueForEnterpriseConstraint implements ValidatorConstraintInterface {
constructor(@Inject(REQUEST) private request: Request) {}
async validate(value: any, args: ValidationArguments) {
const { enterprise } = this.request.user as any
const { model } = args.constraints[0] as IParams;
if(!enterprise) return false;
if (!model) return false;
const repo = getManager().getRepository(model as ObjectType<any>);
const item = await …Run Code Online (Sandbox Code Playgroud) 我想验证地址字段,它可能包含数字或字符串,但不应接受连续的空格
@IsAlphaNUMereic() 地址:字符串;
我想要,地址可以是数字或字母...但它不应该接受继续空格
我的“AppState”枚举具有以下可能的枚举值:
export enum AppState {
SUCCESS,
ERROR,
RUNNING
}
Run Code Online (Sandbox Code Playgroud)
我有一个UpdateAppStateDTO,appState它应该接受除RUNNING之外的每个枚举值。
export class UpdateAppStateDTO {
@IsEnum(AppState)
@NotEquals(AppState.RUNNING) // Doesn't work properly
public appState: AppState;
}
Run Code Online (Sandbox Code Playgroud)
对于路线我有这个例子
@Patch()
public setState(@Body() { appState }: UpdateAppStateDTO): void {
console.log(appState);
}
Run Code Online (Sandbox Code Playgroud)
如果请求有一个空的正文或一个无效的枚举值,比如“foobar”,appState因为我得到了 400,这很好。
问题是当我发送“RUNNING”时,我仍然得到 200 而不是 400。
我怎样才能防止这种行为?
这是类验证器抛出的错误。这是我的 dto 的代码:
export class UpdateEntryBodyDto {
@ApiProperty()
@Type(() => Number)
@IsNumber()
id: number;
@ApiProperty()
@IsString()
@IsOptional()
@Validate(IsUniqueEntryTitle)
title?: string;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了更多不同的配置,但我遇到了同样的该死的错误。我对这个错误感到完全困惑,无法弄清楚这段代码到底出了什么问题,我按照这里推荐的方式进行操作,但无济于事......
有人可以帮忙吗?
嘿,我想创建一个具有唯一电子邮件的用户。我正在使用类验证器进行额外验证。我在这里找到了很多建议来实现这样的独特性:
@Schema()
export class User {
@Prop()
firstName!: string;
@Prop()
lastName!: string;
@Prop()
email!: {
type: String,
required: true,
index: true,
unique: true
};
@Prop({ nullable: true })
password?: string;
}
Run Code Online (Sandbox Code Playgroud)
但我抛出了一个错误:
Type 'UserDocument | null' is not assignable to type 'UserInput | null'.
Run Code Online (Sandbox Code Playgroud)
...我认为总的来说,这在 NestJS 中是不可能的。
我还通过添加唯一的道具找到了解决方案:
@Prop({
unique: true,
})
email!: string;
Run Code Online (Sandbox Code Playgroud)
...这有效,但随后我得到了完全不同的错误结构,并且我无法设置自定义错误。
我在 git 上看到的任何可行的解决方案都是测试服务中的唯一性并抛出错误。
为什么NestJS没有解决方案自动验证唯一性如预期?
NestJS 项目使用带有类验证器的 ValidationPipe来验证 POST 请求。在(react)前端使用相同的类验证器 DTO 会很好。
DTO 中的实体如何链接到反应元素?
这可能类似于如何同步前端和后端验证,但更侧重于特定工具。
在 NestJS 中使用和包描述 DTO 类时,有没有办法设置装饰器的执行顺序?class-validatorclass-transformer
当 的值foo设置为时,以下代码会失败null并出现错误:
需要一个字符串,但收到一个 null
@IsOptional()
@IsString()
@IsByteLength(1, 2048)
@Transform(({ value }) => validator.trim(value))
@Transform(({ value }) => validator.stripLow(value))
foo: string;
Run Code Online (Sandbox Code Playgroud)
即使我有一个isString装饰器应该检查是否确实传递了一个字符串,并且必须已经失败而不将执行传递给装饰@Transform器,但它并没有失败。
decorator typescript class-validator nestjs class-transformer
class-validator ×10
nestjs ×8
node.js ×4
typescript ×4
javascript ×2
validation ×2
decorator ×1
mongodb ×1
mongoose ×1
reactjs ×1
typeorm ×1
unique ×1