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

Deeply nested objects have the wrong JSON schema type #400

Open
ekampp opened this issue Mar 3, 2022 · 1 comment
Open

Deeply nested objects have the wrong JSON schema type #400

ekampp opened this issue Mar 3, 2022 · 1 comment

Comments

@ekampp
Copy link

ekampp commented Mar 3, 2022

Describe the bug

Given these two dry schemas:

A = Dry::Schema.JSON do 
  required(:attributes).maybe(:hash) do
    required(:name).filled(:string, min_size?: 1)
  end
end

B = Dry::Schema.JSON do 
  required(:data).array(A)
end

I would expect to se a JSON schema like this:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "attributes": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "name"
            ]
          }
        },
        "required": [
          "attributes"
        ]
      }
    }
  },
  "required": [
    "data"
  ]
}

But, in fact, I see JSON like this:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "attributes": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": []
            }
          }
        },
        "required": [
          "attributes"
        ]
      }
    }
  },
  "required": [
    "data"
  ]
}

There are two, principle differences:

  1. I would expect the attributes to be an object, since it's defined like this: required(:attributes).maybe(:hash). It is, in fact, an array.
  2. I would expect attributes to require the name since it's defined like this required(:name).filled(:string, min_size?: 1). It is, in fact, not required.
  • Affects my production application: NO
  • Ruby version: 3.1.1
  • OS: Mac OS Monteray (M1)
@ekampp
Copy link
Author

ekampp commented Mar 3, 2022

In other terms, it looks like the nested hash definition is overridden by the higher level array definition. I would expect to be able to change type as I build out the schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant