-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IsOptional support for null #9
Comments
Hi! That's a good point, IsOptional should indeed add a null schema as well as remove the property from the I'll try to look into this more when there's time. For the time being you might want to override the default |
My temporary solution for now is to traverse the object and convert all |
Personally, I don't think This is especially relevant when I'm implementing a |
@loban that's totally reasonable, but the way
and this library should naturally match |
Taking into account the current @IsOptional behavior, what could be a workaround for what @loban stated? |
@xjuanc you could use the |
Hello, Any news on this? How can i validate or null or enum? This doesn't work.
|
mark |
Hello, I use the following code for enum validation: @IsIn(Object.values(ConversationStatusEnum)) |
You can replace Validator accepts {id: null} @IsOptional()
@IsNotEmpty()
readonly id: string; Validator blocks {id: null} @ValidateIf(o => 'id' in o)
@IsNotEmpty()
readonly id: string; |
I hope someone finds this helpful, I was able to get optionals working properly like so:
however, as you can see, we need to get
this mimics IsOptional except it gives us the ability to be able to call
Please let me know if you find a better way of doing this and hopefully this comes in helpful as it took me a few hours to get this working properly. Overall though, we do now have the appropriate schema:
|
I'm not sure whether this is part of as conditional decorator limitations in the readme. In any case...
The
@IsOptional
decorator should be addinganyOf: [{type: someType}, {type: 'null'}]
as well as removing the property from therequired
array. It doesn't seem to be doing the former.I note that internally, class validator uses
conditionalValidation
for theIsOptional
decorator. Am I correct that the limitation is that this doesn't work when there are multiple decorators for one field? For example:The functionality of
IsOptional
depends on the other decorators.The text was updated successfully, but these errors were encountered: