Skip to content

Commit

Permalink
use AnyOf for schema of union types
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Dec 4, 2024
1 parent 4702ad8 commit 7b51811
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/json/blueprint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ pub fn union_type_decoder(
Some(["type", "data"]),
)
})
|> jsch.OneOf
|> jsch.AnyOf
}

let defs = list.flat_map(decoders, fn(dec) { { dec.1 }.defs })
Expand Down
4 changes: 2 additions & 2 deletions test/json_blueprint_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub fn constructor_type_decoder_test() {
let schema = blueprint.generate_json_schema(shape_decoder())

let expected_schema_str =
"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"oneOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"circle\"]},\"data\":{\"required\":[\"radius\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"radius\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"rectangle\"]},\"data\":{\"required\":[\"width\",\"height\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"number\"},\"height\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"void\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}"
"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"anyOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"circle\"]},\"data\":{\"required\":[\"radius\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"radius\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"rectangle\"]},\"data\":{\"required\":[\"width\",\"height\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"number\"},\"height\":{\"type\":\"number\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"void\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}"

schema
|> json.to_string
Expand Down Expand Up @@ -814,7 +814,7 @@ pub fn tree_decoder_test() {
schema
|> json.to_string
|> should.equal(
"{\"$defs\":{\"ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\":{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"node\"]},\"data\":{\"required\":[\"value\",\"right\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"value\":{\"type\":\"integer\"},\"left\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"right\":{\"anyOf\":[{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},{\"type\":\"null\"}]}}}}}},\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"oneOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"list\"]},\"data\":{\"required\":[\"head\",\"tail\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"head\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"tail\":{\"$ref\":\"#\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"no_trees\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}",
"{\"$defs\":{\"ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\":{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"node\"]},\"data\":{\"required\":[\"value\",\"right\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"value\":{\"type\":\"integer\"},\"left\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"right\":{\"anyOf\":[{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},{\"type\":\"null\"}]}}}}}},\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"anyOf\":[{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"list\"]},\"data\":{\"required\":[\"head\",\"tail\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"head\":{\"$ref\":\"#/$defs/ref_CEF475B4CA96DC7B2C0C206AC7598AFFC4B66FD2\"},\"tail\":{\"$ref\":\"#\"}}}}},{\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"no_trees\"]},\"data\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{}}}}]}",
)
}

Expand Down

0 comments on commit 7b51811

Please sign in to comment.