question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filtering properties with PartialClassToPlain

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:33 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
marcjcommented, Jun 15, 2020

@rafaferry unfortunately, not that straightforward since i in partial can be a deep path, e.g. user.settings.color, so that loop is not the right place. It needs to be in JitPropertyConverter.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.

1reaction
marcjcommented, Jun 11, 2020

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 rename filter to group, and maybe change the third argument of partial*() a bit (into a option object).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found