typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.87k stars 785 forks source link

Custom decorators: nested data #142

Open stelcheck opened 6 years ago

stelcheck commented 6 years ago

I would like to create a decorator that deals with nested values.

Essentially:

class Something {
  @IsKeyValueOf(NestedClass)
  public hello: { [key: string]: NestedClass }
}

How could I potentially approach that?

NoNameProvided commented 6 years ago

If you know the property names of hello, then make it a class.

class NestedParentClass {
  prop1: NestedClass;
  prop2: NestedClass;
}

class Something {
  public hello: NestedParentClass;
}
stelcheck commented 6 years ago

In this case, I would not know the names. I want to use an anonymous object as a key-value store but still validate the entries (and, eventually, the keys).

NoNameProvided commented 6 years ago

I am not an expert in class-validator but as much as I know this is not possible yet.

stelcheck commented 6 years ago

I already have a prototype that does just what I want, except that there is no documentation on how to create custom validators for nested data. From what I could see in the code, dealing with nesting is hard-coded in this code base.