Filtering properties with PartialClassToPlain
See original GitHub issueHi Marc,
Thanks for this awesome lib! I have a feature request, I Thought I could use partialPlainToClass with @f.exclude but doesn’t really cover my need.
I would like to filter all nested params based on some filter name. Example:
class Book {
@f.filter('myFilter')
title:string
@f
isbn: string
}
class Reader {
@f.filter(['myFilter', 'myAgeFilter'])
name:string
@f.filter(['myAgeFilter'])
age: int
@f.array(Book).filter("myFilter")
books: Book[]
}
const reader:Reader = plainToClass(Reader, {
name: 'rafael',
age: 34,
books: [
{ title: "Lean Startup", isbn: "xxxx-yyyy-zzzz" },
{ title: "Traction", isbn: "zzzz-xxxx-yyyy" },
]
});
I need to convert this reader back into a plain object, but only containing some of its properties (and nested properties), based on the filter name. I would expect something like this:
const filteredReader = partialClassToPlain(Reader, reader, 'myFilter');
{
name: 'rafael',
books: [
{title: 'Lean Startup'},
{title: 'Traction'}
]
}
const filteredReader2 = partialClassToPlain(Reader, reader, 'myAgeFilter');
{
name: 'rafael',
age: 34
}
There is a way to achieve this with the current api? If there’s not, does this make sense to you?
I Want to use this to send json through an API, only sending the pertinent fields to each URL.
Issue Analytics
- State:
- Created 3 years ago
- Comments:33 (16 by maintainers)
Top Results From Across the Web
Serialization | NestJS - A progressive Node.js framework
Exclude properties#. Let's assume that we want to automatically exclude a password property from a user entity. We annotate the entity as follows:...
Read more >Filter array of objects whose any properties contains a value
Here is a version of the above which filters by a value which is derived from an array of object's property. The function...
Read more >typestack/class-transformer: Decorator-based ... - GitHub
Exposing getters and method return values; Exposing properties with different ... Now password property will be excluded only during classToPlain operation.
Read more >How to Use PowerShell Where-Object to Filter All the Things
When you work with PowerShell property values in a collection of objects, ... The Where-Object cmdlet is a handy way to filter objects....
Read more >class-transformer - Bountysource
But could I also transform Class with dictionary type properties? import {Type, classToPlain} from "class-transformer"; export interface PhotoList { [id: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rafaferry unfortunately, not that straightforward since
i
inpartial
can be a deep path, e.g.user.settings.color
, so that loop is not the right place. It needs to be inJitPropertyConverter.convert()
where we have the PropertySchema. PropertySchema needs to be extended to hold the group information plus the@f
decorator needs to be updated to support setting a group name. I’m going to implement it once I find the time.Yeah I had such a group mechanism already on my list but didn’t find the time yet. Totally makes sense! Indeed
@f.exclude
is on compile level, what you need is on runtime level. I’d implement it as you explained, except that to renamefilter
togroup
, and maybe change the third argument ofpartial*()
a bit (into a option object).