Skip to content

feat: nested type proposal #103

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

Closed
wants to merge 2 commits into from

Conversation

ibellver
Copy link

@ibellver ibellver commented Mar 28, 2023

Currently without using the class-transformer there is no option to work with the nested elements and also with troubles related with this typestack/class-transformer#563

This is a proposal to create a decorator for that purpose without depending on class-transformer, when you are using only class-validator decorators

Usage

@ValidateNested()
@NestedType(() => NestedClass)
nestedTypeWithObject: NestedClass | null | undefined;

@ValidateNested({ each: true })
@NestedType(() => NestedClass)
nestedTypeWithArray: NestedClass[]

The decorator is not altering the previous behavior, so, no regressions or breaking changes are expected. The decorator is only taken into account when present in a class attribute, in that case, it will have preference when detecting the nested type.

The name of the decorator is only a proposal

I hope you find this useful
Greetings 😄

@ibellver
Copy link
Author

There is also an alternative, problably cleaner and more aligned with the library. Instead of a new decorator we can just add a new property to the current options of the @JSONSchema decortator.

Something like

@ValidateNested({ each: true })
@JSONSchema({
    nestedType: () => NestedClass
})
nestedTypeWithArray: NestedClass[]

If you see it's interesting I can implement that approach.

@ibellver ibellver closed this May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants