Skip to content
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

Class-level validators #2390

Open
paulmis opened this issue Jan 24, 2024 · 0 comments
Open

Class-level validators #2390

paulmis opened this issue Jan 24, 2024 · 0 comments
Labels
type: question Questions about the usage of the library.

Comments

@paulmis
Copy link

paulmis commented Jan 24, 2024

Can we automatically apply @Allow to all class fields when interface merging? i.e.:

interface ICar {
  x: IWheel
  y: string
  z: number
}

interface Car extends ICar {}  // inherits all ICar fields

class Car implements ICar {
  @ValidateNested()
  x: Wheel

  // Also apply @Allow to y, z without explicitly typing it up

  constructor(data: ICar) {
     Object.assign(this, data, ...)
     validate(this, { whitelist: true, forbidNonWhitelisted: true })
  }
}

so that we can then assign & validate from an interface without duplicating code

const dto: ICar = JSON.parse(...)
const car: Car = new Car(dto) // can throw validation errors

I understand this is a use case for class-transformer, but it seems like that project is no longer maintained.

@paulmis paulmis added the type: question Questions about the usage of the library. label Jan 24, 2024
@paulmis paulmis changed the title question: <your-title-goes-here> Class-level validators Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

1 participant