ReScript JSON Schema
Typesafe JSON Schema for ReScript
- Provides ReScript types to work with JSON schema
- Converts rescript-struct into JSON schemas
- Converts JSON schemas to rescript-struct
Install
npm install rescript-json-schema rescript-struct
Then add rescript-json-schema
and rescript-struct
to bs-dependencies
in your bsconfig.json
:
{
...
+ "bs-dependencies": ["rescript-json-schema", "rescript-struct"]
+ "bsc-flags": ["-open RescriptStruct"],
}
Create JSON schemas with type safety
One of the library's main features is the rescript-struct, which provides a way to describe the structure of a value. This structure contains meta information used for parsing, serializing, and generating JSON Schema. When working with the library, you will mostly interact with rescript-struct to define the structure of the values you are working with.
For example, if you have the following struct:
type author = {
id: float,
tags: array<string>,
isAproved: bool,
deprecatedAge: option<int>,
}
let authorStruct = S.object(o => {
id: o->S.field("Id", S.float()),
tags: o->S.field("Tags", S.option(S.array(S.string()))->S.default(() => [])),
isAproved: o->S.field(
"IsApproved",
S.union([S.literalVariant(String("Yes"), true), S.literalVariant(String("No"), false)]),
),
deprecatedAge: o->S.field("Age", S.int()->S.deprecate("Will be removed in APIv2")),
})
You can use it to generate JSON Schema for the value it describes:
JSONSchema.make(authorStruct)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"Age": {
"deprecated": true,
"description": "Will be removed in APIv2",
"type": "integer"
},
"Id": { "type": "number" },
"IsApproved": {
"anyOf": [
{
"const": "Yes",
"type": "string"
},
{
"const": "No",
"type": "string"
}
]
},
"Tags": {
"items": { "type": "string" },
"type": "array"
}
},
"required": ["Id", "IsApproved"],
"additionalProperties": true
}