Reputation: 228
I am using TSeD version 6.38.1
I have the following model.
import {Any, CollectionOf, Enum, Property} from "@tsed/schema";
export enum Status {
status1,
status2,
...
}
export class FilterParams {
... Some other fields
@Any(Enum(Status), CollectionOf(Enum(Status)))
status: Status[] | Status
}
I struggling to annotate status
field of FilterParams
. I want it to be a Status
or an array of Status
I have tried the following annotations but none of them worked.
@Enum(Status) status: Status[] | Status
: This only accept accepts a single Status
but rejects the array of Status
@Enum(Status) status: Status
: This also only accept accepts a single Status
but rejects the array of Status
@Enum(Status) status: Status[]
: This only accept accepts list of Status
but rejects the array of Status
@Any(Enum(Status), CollectionOf(Enum(Status))) status: Status[] | Status
: This gives AJV_VALIDATION_ERROR
on runtime with message FilterParams.status.0 should be object.
What to achieve this validation in TSeD?
Upvotes: 2
Views: 447
Reputation: 91
You cannot do that with decorators (and mix decorators like you shown in the example is not possible).
The solution is to use the function API to describe correctly the schema and use OnDeserialize decorator to be ensure that you will have always an array of Status.
import {Any, CollectionOf, Enum, Property, string, array} from "@tsed/schema";
import {OnDeseriliaze} from "json-mapper";
export enum Status {
status1,
status2,
...
}
const StatusSchema = string().enum(Status)
export class FilterParams {
... Some other fields
@AnyOf(StatusSchema, array().items(StatusSchema))
@OnDeseriliaze(o => [].concat(o))
status: Status[];
}
See you Romain
Upvotes: 3