Skip to content

Commit

Permalink
add type info to array schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Dec 2, 2024
1 parent 292bfcd commit cae7740
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/json/blueprint/schema.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,15 @@ fn schema_definition_to_json_fields(
}

Array(items) -> {
prepend_option([], items, "items", fn(schema) {
[#("type", schema_type_to_json(ArrayType))]
|> prepend_option(items, "items", fn(schema) {
schema_definition_to_json(schema)
})
}

Object(properties, additional_properties, required) -> {
[
#("type", json.string("object")),
#("type", schema_type_to_json(ObjectType)),
#(
"properties",
json.object(
Expand Down Expand Up @@ -294,7 +295,7 @@ fn schema_definition_to_json_fields(
min_contains,
max_contains,
) -> {
[]
[#("type", schema_type_to_json(ArrayType))]
|> prepend_option(items, "items", fn(schema) {
schema_definition_to_json(schema)
})
Expand All @@ -320,7 +321,7 @@ fn schema_definition_to_json_fields(
min_properties,
max_properties,
) -> {
[]
[#("type", schema_type_to_json(ObjectType))]
|> prepend_option(properties, "properties", fn(props) {
json.object(
list.map(props, fn(prop) {
Expand Down
3 changes: 2 additions & 1 deletion test/json_blueprint_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn list_decoder_test() {
json.object([
get_schema_header(),
#("items", json.object([#("type", json.string("integer"))])),
#("type", json.string("array")),
]),
)
}
Expand Down Expand Up @@ -664,6 +665,6 @@ pub fn drawing_test() {
|> blueprint.generate_json_schema
|> json.to_string
|> should.equal(
"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"box\"]},\"data\":{\"required\":[\"width\",\"height\",\"position\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"number\"},\"height\":{\"type\":\"number\"},\"position\":{\"maxItems\":2,\"minItems\":2,\"prefixItems\":[{\"type\":\"number\"},{\"type\":\"number\"}]},\"color\":{\"required\":[\"enum\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"enum\":{\"type\":\"string\",\"enum\":[\"red\",\"green\",\"blue\"]}}}}}}}",
"{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"required\":[\"type\",\"data\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\",\"enum\":[\"box\"]},\"data\":{\"required\":[\"width\",\"height\",\"position\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"number\"},\"height\":{\"type\":\"number\"},\"position\":{\"maxItems\":2,\"minItems\":2,\"prefixItems\":[{\"type\":\"number\"},{\"type\":\"number\"}],\"type\":\"array\"},\"color\":{\"required\":[\"enum\"],\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"enum\":{\"type\":\"string\",\"enum\":[\"red\",\"green\",\"blue\"]}}}}}}}",
)
}

0 comments on commit cae7740

Please sign in to comment.