English 中文(简体)
Nest.js 验证字符串阵列(如果只有定义的字符串)
原标题:Nest.js validate array of strings if there are defined strings only
In the nest.js application on controller level I have to validate DTO. I ve faced with difficulty to check if item is not null (request should be rejected if any list item is null or undefined) Code bellow demonstrates my configured verifications. import { ArrayMinSize, IsArray } from class-validator export class ReminderPayload { // ... @IsArray() @ArrayMinSize(1) recipients: string[] } Question I m looking for help to reject requests with body data like { "recipients": [ null ] } How to validate if array items are string only (it should reject handling if object is in the array item position)? P.S. class-validator injected successfully, and it produces some validation results for my API.
最佳回答
You need to tell class-validator to run the validations on each item of the array. Change your payload DTO to the following: import { ArrayMinSize, IsArray, IsString } from class-validator ; export class ReminderPayloadDto { // ... @IsArray() @IsString({ each: true }) // "each" tells class-validator to run the validation on each item of the array @ArrayMinSize(1) recipients: string[]; } Link to the docs on this.
问题回答
For someone who wants to validate specific strings in array: class MyDto { @IsIn([ monday , tuesday , wednesday , thursday , friday ], { each: true }) weekdays: string[]; // regex mask validation @Matches( ^[a-zA-Z\s]+$ , undefined, { each: true }) words: string[]; @Contains( hello , { each: true }) greetings: string[]; } For custom validation: import { ArrayNotEmpty, IsArray, Validate, ValidateNested, ValidatorConstraint, ValidatorConstraintInterface } from class-validator @ValidatorConstraint({ name: arrayPrefixValidator }) export class ArrayPrefixValidator implements ValidatorConstraintInterface { validate(values: string[] = []): boolean { if (values.length) { return values.every((value) => value.startsWith( user- )) } return false } } class MyDto { // Each item contains a prefix str- @Validate(ArrayPrefixValidator, { message: No user- prefix }) accounts: string[]; } For more information go to official docs




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签