json-schema-to-case-class icon indicating copy to clipboard operation
json-schema-to-case-class copied to clipboard

Support if-the-else

Open cchandurkar opened this issue 1 year ago • 0 comments

if-then-else compositions are currently ignored. Since we can't have types based on a condition, the expectation here is that the resulting case class will have all possible fields with validations where necessary.

Input

{
  "type": "object",
  "properties": {
    "street_address": {
      "type": "string"
    },
    "country": {
      "default": "United States of America",
      "enum": ["United States of America", "Canada"]
    }
  },
  "if": {
    "properties": { "country": { "const": "United States of America" } }
  },
  "then": {
    "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
  },
  "else": {
    "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
  }
}

Current Behaviour

object CountryEnum extends Enumeration {
    val United States of America, Canada = Value
}

case class MyCaseClass (
     street_address: Option[String],
     country: Option[CountryEnum.Value]
)

Expected Behaviour

object CountryEnum extends Enumeration {
    val United States of America, Canada = Value
}

case class MyCaseClass (
     street_address: Option[String],
     country: Option[CountryEnum.Value],
     postal_code: Option[String]
) {
    if (country.contains("United States of America")) {
        assert(postal_code.forall(_.matches("[0-9]{5}(-[0-9]{4})?")))
    } else {
        assert(postal_code.forall(_.matches("[A-Z][0-9][A-Z] [0-9][A-Z][0-9]")))
    }
}

cchandurkar avatar Apr 14 '23 03:04 cchandurkar