Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support if-the-else #43

Open
cchandurkar opened this issue Apr 14, 2023 · 0 comments
Open

Support if-the-else #43

cchandurkar opened this issue Apr 14, 2023 · 0 comments

Comments

@cchandurkar
Copy link
Owner

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]")))
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant