Skip to content

How to define a Nullable Enum property? #2730

Answered by Romakita
KBroichhausen asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @KBroichhausen
I tried that:

	@Required()
	@Allow(null)
	@Enum(BarEnum)

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:

export enum BarEnum {
	Bar1 = "bar1",
	Bar2 = "bar2",
	UNKNOWN = "UNKNOWN"
}

export class Foo {
	@Enum(BarEnum)
        @Default(BarEnum.

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@KBroichhausen
Comment options

@Romakita
Comment options

@KBroichhausen
Comment options

@Romakita
Comment options

Answer selected by KBroichhausen
@KBroichhausen
Comment options

@Romakita
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants