dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
424 stars 110 forks source link

Fix json-schema type of objects nested under arrays. Fixes #400 #462

Closed tomgi closed 6 months ago

tomgi commented 1 year ago

Fix for https://github.com/dry-rb/dry-schema/issues/400

Objects nested under arrays are not neccesailry arrays themselves - the :member option shouldn't be passed down the AST when generating json-schema.

For example for the test schema https://github.com/dry-rb/dry-schema/blob/5853103d6849490ae8ba94158f73631c1fd121a8/spec/extensions/json_schema/schema_spec.rb#L28-L34

the result of the fix is:

 {
   "$schema": "http://json-schema.org/draft-06/schema#",
   "type": "object",
   "properties": {
     "email": {
       "type": "string"
     },
     "age": {
       "type": "integer"
     },
     "roles": {
       "type": "array",
       "items": {
         "type": "object",
         "properties": {
           "name": {
             "type": "string",
             "minLength": 12,
             "maxLength": 36
           },
           "metadata": {
-            "type": "array",
-            "items": {
-              "type": "object",
-              "properties": {
-                "assigned_at": {
-                  "type": "string",
-                  "format": "time"
-                }
-              },
-              "required": [
-                "assigned_at"
-              ]
-            }
+            "type": "object",
+            "properties": {
+              "assigned_at": {
+                "format": "time",
+                "type": "string"
+              }
+            },
+            "required": [
+              "assigned_at"
+            ]
           }
         },
         "required": [
           "name",
           "metadata"
         ]
       }
     },
     "address": {
       "type": "object",
       "properties": {
         "street": {
           "type": "string"
         }
       },
       "required": [

       ]
     },
     "id": {
       "anyOf": [
         {
           "type": "string"
         },
         {
           "type": "integer"
         }
       ]
     }
   },
   "required": [
     "email",
     "roles",
     "id"
   ]
 }
cpgo commented 10 months ago

Hey @tomgi just asked about this issue on the zulip chat. Tried monkey patching your solution on my project and it seems to be working perfectly.

@solnic is there any plans to merge this PR?

shauns commented 6 months ago

Following up on this -- @solnic would merging it be possible? The bug is still in place and this fix is a good one.

solnic commented 6 months ago

I'm sorry it took so long, I'll push a release, thanks for addressing it

lukelex commented 6 months ago

Awesome work. Thanks for dedicating your time to fix this :heart: