nhovratov / content-blocks-json-schema

1 stars 1 forks source link

Make property type required in if/then/else conditions, as in case of… #8

Closed woemar closed 1 month ago

woemar commented 2 months ago

… undefined property type the then case will always be executed.

I think there is a misunderstanding regarding if/then/else in json schema.

Consider these examples:

"if": {
    "properties": {
      "country": {"const": "select"}
    }
},
"then": {
    "properties": {
      "thisWillAlwaysBeDefined": { type: string }
    }
}

It looks like THEN will only be executed if country == "select" but this is NOT true! As country is not a required field the if part will always evaluate to true and the then branch will be "executed".

To make the then branch active only if country equals "select" you have to require the country property:

"if": {
    "properties": {
        "country": {"const": "select"}
    },
    "required": ["country"]
},
"then": {
    "properties": {
        "onlyIfCountryDefinedAndValueIsSelect": { type: string }
    }
}

This is what I changed in the schema deinition to make it work like expected. Not sure if I catched all the cases but at first glance it looks way better than before ;-)

PKuhlmay commented 1 month ago

Fixes #7