jonasschmidt / ex_json_schema

An Elixir JSON Schema validator
MIT License
366 stars 98 forks source link

I cant see why I'm getting this error #72

Closed kairos0ne closed 2 years ago

kairos0ne commented 2 years ago

I have the following JSON schema


%ExJsonSchema.Schema.Root{
  custom_format_validator: nil,
  definitions: %{},
  location: :root,
  refs: %{
    "lib/schemas/column.schema.json" => %{
      "$schema" => "http://json-schema.org/draft-07/schema#",
      "additionalProperties" => false,
      "anyOf" => [
        %{
          "properties" => %{
            "maxLength" => %{
              "maximum" => 64,
              "minimum" => 3,
              "type" => "number"
            },
            "type" => %{"pattern" => "^varchar$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^smallint$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^integer$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^bigint$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^decimal$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^numeric$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^real$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^double precision$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^serial$", "type" => "string"}
          },
          "type" => "object"
        },
        %{
          "properties" => %{
            "type" => %{"pattern" => "^bigserial$", "type" => "string"}
          },
          "type" => "object"
        }
      ],
      "description" => "Comment describing your JSON Schema",
      "properties" => %{
        "name" => %{"maxLength" => 64, "minLength" => 3, "type" => "string"},
        "required" => %{"type" => "boolean"}
      },
      "required" => ["name"],
      "type" => "object"
    },
    "lib/schemas/incident.schema.json" => %{
      "$schema" => "http://json-schema.org/draft-07/schema#",
      "additionalProperties" => false,
      "allOf" => [
        %{
          "additionalProperties" => false,
          "properties" => %{
            "name" => %{"type" => "string"},
            "stuff" => %{"type" => "number"}
          },
          "type" => "object"
        }
      ],
      "items" => false,
      "properties" => %{},
      "type" => "object"
    },
    "lib/schemas/table.schema.json" => %{
      "$schema" => "http://json-schema.org/draft-07/schema#",
      "additionalProperties" => false,
      "description" => "Comment describing your JSON Schema",
      "properties" => %{
        "name" => %{"maxLength" => 64, "minLength" => 5, "type" => "string"},
        "parent" => %{"type" => "string"},
        "schema" => %{
          "items" => %{"$ref" => ["lib/schemas/column.schema.json"]},
          "type" => "array"
        }
      },
      "required" => ["name"],
      "type" => "object"
    }
  },
  schema: %{
    "$schema" => "http://json-schema.org/draft-07/schema#",
    "description" => "Comment describing your JSON Schema",
    "properties" => %{
      "action" => %{
        "description" => "The action you wish to perform",
        "enum" => ["create", "read", "update", "delete"],
        "type" => "string"
      },
      "body" => %{
        "oneOf" => [
          %{"$ref" => ["lib/schemas/table.schema.json"]},
          %{"$ref" => ["lib/schemas/incident.schema.json"]}
        ],
        "type" => "object"
      },
      "id" => %{
        "description" => "The id of the object on which you woud like to perform the action",
        "type" => "string"
      }
    },
    "required" => ["body"],
    "type" => "object"
  },
  version: 7
}```

And I have the following payload

```iex(86)> payload
%{
  "action" => "create",
  "body" => %{
    "name" => "incident",
    "parent" => "e5cb0b35-7b30-4613-83f9-31f5d7007b12",
    "schema" => [
      %{
        "maxLength" => 64,
        "name" => "name",
        "required" => true,
        "type" => "varchar"
      },
      %{"name" => "stuff", "type" => "smallint"}
    ]
  }
}
iex(87)>```

Im getting this error when I try validate. 

```{:error,
 [
   {"Expected exactly one of the schemata to match, but none of them did.",
    "#/body"}
 ]}
iex(89)>```