435 questions
0
votes
0
answers
88
views
nestjs use i18n in decorators of class-validator without indicate one by one
I'm researching that can I use i18n for the messages of validation exceptions. For example:
create-user.dto.ts
export default class createUserDto {
@MinLength(5)
@IsString()
name: string;
}...
0
votes
0
answers
67
views
Issue with injecting services in a validator in nestjs
I am having an issue with NestJs DI class-validators.
In my main class I define the Validation pipe as such
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: ...
0
votes
1
answer
91
views
How to validate nested array of object by groups in Nestjs with class-validator
Consider following Dtos:
export class Child {
@ApiProperty({
type: String,
isArray: true,
})
name: string;
@ApiProperty({
type: Number,
isArray: true,
})
age: number;
}
...
2
votes
0
answers
64
views
Receiving objects in DTOs and converting them using class transformers
I'm trying to receive a plain object in my DTO and to convert it into a number using a hash algorithm. I use validation pipes with whitelisting, and wanted to use the transform pipes to convert it to ...
0
votes
0
answers
44
views
How to assign validation result to a field using class-validator in NestJS?
I am working on a NestJS project where I use class-validator to validate incoming DTOs. I have a custom validator @IsAccessPointExists() that checks if an access point exists in the database. The ...
0
votes
1
answer
74
views
NestJS ValidationPipe with ValidateIf not preventing conditional field validation
I'm building an accounting application using NestJS. I have a DTO called CreateTransactionDto, and I want certain fields to be required or forbidden based on the value of transactionType. For example:
...
1
vote
1
answer
89
views
class-validator whitelisting properties of object inside array
I´m trying to validate the body of a route on NestJS and i´m using the class-validator package, my route is receiving the data as multipat because this route also accepts a file upload. Here is the ...
2
votes
0
answers
509
views
Validating union type with class-validator?
In a NestJS application I bave multiple DTO classes that utilize class-validator.
The controller method expects POST body which can represent any of these DTOs.
class A {
// some validators
}
class B ...
1
vote
0
answers
111
views
Issue with IsEnum Decorator on an Optional Property in Nestjs
I am working with NestJS and trying to apply the IsEnum decorator to an optional property. However, I am encountering an error.
I am using "class-validator": "^0.14.1" and my DTO ...
1
vote
1
answer
95
views
It is not possible to access the database through the validator Nest JS
I'm trying to create an auto-check in nest js for the existence of a record in a prisma database using a validator class. I currently have the following code:
interface UniqueValidatorOptions {
...
1
vote
1
answer
204
views
Unable to POST data using Nuxt's useFetch()
I try to send login credentials to my NestJS API. API uses class-validator to validate incoming requests' bodies.
// auth.controller.ts
@Post('login')
async login(@Res({ passthrough: true }) response: ...
1
vote
2
answers
134
views
Optionally validating nested fields depending on database field
I working on NestJS app and trying to validate some field depending on other field.
export class CreateOrderDTO {
@IsDeliveryCodeValid() // Custom validator
delivery_code: string;
@...
1
vote
0
answers
91
views
Express middleware for DTO validation throws undefined TypeError on valid payload
I have a POST /login route in my Express application where I'm using a payload validation middleware before the controller, structured as follows:
profileRouter.post(
"/login",
...
0
votes
1
answer
90
views
Class-Validator: I want to apply custom validator only if built-in validator pass
class RegisterBody {
@IsEmail()
@IsEmailRegistered()
email: string;
@IsStrongPassword()
password: string;
}
I want to apply IsEmailRegistered only if IsEmail pass and always run ...
2
votes
1
answer
407
views
NestJs Class validation for generic class
I am building a NestJS application with a pagination API that includes filtering and searching capabilities. To handle this, I created an abstract class, PaginationAbstract, which accepts two generic ...
2
votes
1
answer
355
views
Nestjs Class Validator for Confirm Password
I am working on a change password endpoint using nestjs , I want to be able to validate if the two password match
The class-validator library doesn't seem to have that on how to validate two ...
0
votes
1
answer
110
views
ValidationPipe doesn't transform string to number
I use: @UsePipes(new ValidationPipe({ transform: true })) to transform query param criteria to specific types, but it doesn't work for numbers, can you explain me why?
Version of @bx/nestjs-commons - ...
0
votes
1
answer
113
views
Validation an object with at least one non-nullable field
Problem:
Backend will receive an object, and at least one field must to be non-nullable.
Example dto object:
export class MessageDto implements TMessage {
@IsString()
@MinLength(1)
roomId: ...
-1
votes
1
answer
293
views
NestJS: DTO transform default values not working if parameter is not passed in the payload
I've created a controller that receives a POST/ ... request.
one of the parameters is optional and it's for example called contentType.
in the service i check if the contentType is equal to something ...
0
votes
1
answer
64
views
Validate data coming from different sources (body params or Query) in a single DTO
In a nestjs APP, I am designing an API endpoint. While creating this endpoint I started having an issue in taking the right decision when it comes to validating incoming requests using a decent and ...
0
votes
2
answers
96
views
ValidateIf() is not working as expected . Not Validating (NestJS)
I am using validateIf() to check if the paymentService is equal to some enum value defined in my code. But it does not validate and throws an error .
@ApiProperty({ required: true })
@...
1
vote
1
answer
358
views
class validator @IsOptionalIf() or @IsRequiredIf()
I have this class
class A {
@IsString()
@MaxLength(99)
prop1: string
@IsBoolean()
prop2: boolean
}
I want it to be:
string.
required (if prop2 is true).
optional if prop2 is false.
In ...
0
votes
1
answer
139
views
How to set DTO props as optional, but never allow all of them null?
I'm trying to apply a validation using class-validator to make possible that some props can be @IsOptional, but if all of them is null, throw an exception.
Thinking in some approach like this:
@...
0
votes
1
answer
135
views
stopAtFirstError Validation Pipe is not working as expected in NestJS class-validators
I am trying to change the behavior of the errors in NestJS. Currently when it is encountering an error, it is not just sending that error but it is still continuing the validation and displaying other ...
0
votes
1
answer
77
views
NestJS validate null payload
I'm writing input validation for an event handler. The payload can be either an Order object or null. I need some help on how to allow null payloads in this setup, as they are currently rejected.
Here ...
0
votes
0
answers
72
views
Using class validator forbid same item from being present in 2 different arrays
Hello folks I am working on an University project wherein I need to manage the course taken by the students. The application has a form to add course and delete a course sometimes I observe that ...
1
vote
1
answer
229
views
Custom class validator in nest js, usage for complex validation
We generally work as, doing validation and doing all sort of validation in validator and we expect that if the request reaches controller it has already been validated againsts all the application ...
0
votes
1
answer
148
views
Disabling value validation without changing dto code | class-validator nest js
I have a dto class-validator, example attached below. Can I remove the userId from the validation without changing the code of the dto itself. For example to do this when using dto in controller
...
2
votes
0
answers
730
views
Payload validation with tsoa and class-validator in express js
I am using tsoa with class-validator in express js
Now when i define DTO with class-validator and define type for body in tsoa route, it only validates the datataype of the body object values & if ...
0
votes
1
answer
149
views
How to make more advanced conditional validation using class-validator?
My validation requirements for one varriable depends on the value of the other varriable. I don't know how to create my DTO properly.
Look for my example:
export class CreateItemDto {
@IsBoolean()
...
0
votes
0
answers
74
views
Is optional if other properties are undefined
I have the following DTO
export class UpsertSubscription {
@Expose()
@ValidateIf((o) => !o.monthly && !o.yearly)
@IsNumber({}, { message: 'tiers.priceMustBeANumber' })
@Min(0, { ...
1
vote
1
answer
324
views
Why Class-validator doesn't process fields without a decorator? | Nest js
I wrote a dto for my endpoint, but ran into the fact that it doesn't work correctly, if one of the fields the class-validator is supposed to handle doesn't have a decorator, it stops being checked at ...
2
votes
0
answers
615
views
NestJS: Handling Empty Objects in Array Validation with Class-Validator
I'm working with NestJS and using class-validator to validate data. I have an array of objects in my DTO, but I need to ensure that empty objects (objects with no properties) are eliminated from the ...
0
votes
1
answer
367
views
how to serialize body with user role in dto in nestjs
In NestJS, is it possible to conditionally modify the properties of a request body within controller methods based on the user’s role or access level?
For instance, if a user lacks the necessary ...
1
vote
1
answer
139
views
Custom validator via class-validator has null pointer
I have this:
import * as val from 'class-validator';
import {ObjectId as MongoId} from 'mongodb';
@val.ValidatorConstraint({async: false})
export class IsTraceValidConstraint ...
0
votes
1
answer
885
views
Get min constraints from class-validator in nestjs ValidationPipe exceptionFactory
I want to create a nestjs app and want to have the ability to customize the messages of class-validator, so I created a function to get keys I provide to messages and then returns the customized ...
0
votes
3
answers
6k
views
Nestjs + Class-validator: Validating decimal values
I just ran into a pretty simple issue, this definition normally should have worked perfectly and pass the validation as price field in JSON is already sent in decimal format as you can figure below, ...
2
votes
0
answers
133
views
Why isn't it possible for class-validator to validate @Body() of union type?
register-user.dto.ts:
export default class RegisterUserDTO {
@IsNotEmpty()
@IsString()
firstName: string;
@IsNotEmpty()
@IsString()
lastName: string;
@IsNotEmpty()
@IsString()
@...
0
votes
0
answers
95
views
How to use MongoDB query operators in TypeScript DTO with class-validator?
Here is what I want to achieve. My current QueryUserDto is:
import { ApiHideProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
IsEmpty,
IsEnum,
IsMongoId,
...
0
votes
1
answer
785
views
Class validator allow when undefined but not when null
@IsOptional()
@Transform(({ value }) => (value ? new Date(value) : null))
@IsDate()
doj: Date;
The above code works fine when doj is undefined. It has the same behavior when doj is also null....
1
vote
1
answer
3k
views
How to validate nested plain objects by a sub-DTO with class-validator, class-transformer in NestJS
I've been investigating the topic for a long time. I see the ready solution is
absent that works well in NestJS because transformation does not work before validation. If we enable transformation in ...
0
votes
1
answer
139
views
class-validator/class-transformer (nestjs): validating, then dropping a property
The context of the question is a nestjs app, where I'd like to transform a request payload to a different shape, and along with that, verify a certain property on the payload, but then remove it after ...
2
votes
1
answer
1k
views
class-validator with nestjs prevent creating missing fields with undefined when used with @IsOptional() decorator
I have a DTO that looks like this:
export class UpdateXXXDto {
@IsString()
@IsNotEmpty()
field1: string;
@IsOptional()
@IsString()
field2: string;
@IsOptional()
@IsString()
field3: ...
0
votes
1
answer
175
views
When creating a decorator using class-validator how do I set a default error message?
If the user using my decorator does not define "message" in validationOptions my constraint is reported with an empty string ''.
export function IsISO6391(validationOptions?: ...
1
vote
0
answers
270
views
How to validate object using class-validator again after receive from @Body() of nestjs?
I try this approach but when I enter input that shouldn't pass when validate it return []
import { validate } from 'class-validator';
import { plainToClass } from 'class-transformer';
import { MyDTO } ...
0
votes
1
answer
218
views
react hooks form + MUI X Date Pickerthrows an error when selecting a new date
I am trying to wrap MUI X Date Picker to a custom component.
Here is what I did:
export interface IMessage {
id?: string | undefined;
title?: string | undefined;
content?: string | ...
1
vote
1
answer
1k
views
Can @Headers in Nest.js be validated through DTOs?
I am new to Nest.js and have a good understanding of the overall syntax. However, while creating one of the controllers, I encountered an issue related to the validation of data parsed by the @Headers ...
0
votes
1
answer
82
views
Allow for undefined properties under @ValidateNested decorator
I'm using NesteJs, i want @ValidateNested to skip properties that are not defined in the class and not to throw an error:
property should not exists
This is my classes:
export default class ...
0
votes
1
answer
126
views
Nestjs Decorator check at least one attribute is accepted
Maybe I don't understand the DTO, but I want it to check if at least one attribute is sent by the request. How can I do it?
Here is my dto:
//import libs
export class WorkspaceDto {
@IsString()
...
0
votes
1
answer
292
views
How to solve the problem of query parameters validation in class validator
I use class-validator to validate query parameters in nest js,why validator does not understand such a syntax as a key ?
class Example {
@IsOptional()
@IsObject()
price[$...