2

Given I have such JSON object having an array of various objects like:

{
  "array": [
    {
      "type": "type_1",
      "value": 5
    },
    {
      "type": "type_2",
      "kind": "person"
    }
  ]
}

According to JSON schema validation, I can validate this schema using this JSOM schema definition:

{
  "type": "object",
  "properties": {
    "array": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "enum": [
                  "type_1"
                ]
              },
              "value": {
                "type": "integer",
                "enum": [
                  5
                ]
              }
            }
          },
          {
            "type": "object",
            "properties": {
              "type": {
                "type": "string",
                "enum": [
                  "type_2"
                ]
              },
              "kind": {
                "type": "string",
                "enum": [
                  "person"
                ]
              }
            }
          }
        ]
      }
    }
  }
}

How can I validate the input JSON using the dry-schema gem? Do you have any ideas?

slip
  • 63
  • 6
  • You can reuse schemas so set up a Schema for the item you want to validate and then use that schema as the definition e.g. `required(:array).hash(YourSchema)` . [Docs](https://dry-rb.org/gems/dry-schema/1.5/reusing-schemas/) – engineersmnky Dec 09 '21 at 20:07
  • thanks for an answer, but that is not what I need. What I need is to validate array of various object schemas. I need something like `requred(:array).array(Type1Schema | Type2Schema)` – slip Dec 10 '21 at 05:21
  • And why does that not work? Docs show "`s1 | s2` - both or one of the schemas must pass" which appears to be your interest? If you need more complex rule sets I would recommend switching to `dry-validation` – engineersmnky Dec 10 '21 at 16:51

0 Answers0