How to define a Nullable Enum property? #2730
Answered by Romakita
KBroichhausen asked this question in Q&A
-
| Hey everyone, export enum BarEnum { Bar1 = "bar1", Bar2 = "bar2" } export class Foo { @Required(true, null) @Enum(BarEnum) public bar: BarEnum | null; }I get the following part in the "Foo": { "type": "object", "properties": { "bar": { "type": "string", "enum": ["bar1", "bar2"], "nullable": true, "minLength": 1 } }, "required": ["bar"] }but if I actually use this model with a I don't really see the reason why. A workaround is to use something like export class Foo { @Required(true, null) @Enum(BarEnum, null) public bar: BarEnum | null; }but that adds |
Beta Was this translation helpful? Give feedback.
Answered by Romakita Jun 20, 2024
Replies: 1 comment 6 replies
-
| You can try to use export enum BarEnum { Bar1 = "bar1", Bar2 = "bar2" } export class Foo { @Enum(BarEnum) @Any('string', 'null') public bar: BarEnum | null; } |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Hello @KBroichhausen
I tried that:
The json-schema is correctly generated, but the problem isn't related to Ts.ED.
{ "type": "object", "properties": { "bar": { "type": ["null", "string"], "enum": ["bar1", "bar2"], // and ["bar1", "bar2", null] } }, "required": ["bar"] }AJV doesn't seems to support this kind of rules (ajv-validator/ajv#824). I don't be able to fix that until ajv doesn't support correctly null value for an enum value.
I suggest you to add a default enum value like this: