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

JSON schema array length definition (min|maxItems) issue #481

Open
qd3v opened this issue Oct 18, 2024 · 0 comments
Open

JSON schema array length definition (min|maxItems) issue #481

qd3v opened this issue Oct 18, 2024 · 0 comments

Comments

@qd3v
Copy link

qd3v commented Oct 18, 2024

Describe the bug

This is probably more a feature request, or unexpected behavior detected.
The hash validated correctly, but JSON schema generated is incorrect.
For arrays there are special min|maxItems JSON schema rules, which dry-schema just replace with rules for strings

To Reproduce

Test = Dry::Schema.JSON do
  # As docs say: To add array predicates, use the full form:
  required(:users).value(:array?, min_size?: 5, max_size?: 5).each(:str?)
end

# spec

example "JSON schema match expected" do
  schema = <<~JSON.then { JSON.parse(it, symbolize_names: true) }
    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "type": "object",
      "properties": {
        "users": {
          "type": "array",
          "minItems": 5,
          "maxItems": 5,
          "items": {
            "type": "string"
          }
        }
      },
      "required": ["users"]
    }
  JSON

  expect(Test.json_schema).to eq(schema)
end

Output diff:

{
   $schema: "http://json-schema.org/draft-06/schema#",
   type: "object",
   properties: {
     users: {
       type: "array",
-       minItems: 5,
-       maxItems: 5,
       items: {
+         minLength: 5,
+         maxLength: 5,
         type: "string"
       }
     }
   },
   required: [
     "users"
   ]
}

Expected behavior

I'm using dry-schema for years, (thanks for great work!) and I understand its primary goals (validate ruby structures) and JSON schema gen is just a bonus, so this is the first time I found the missing feature would be nice to have.

My environment

  • Affects my production application: NO
  • Ruby version: 3.4.0p1
  • OS: macOS Sequoia
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