rescript-json-schema

3.0.0 • Public • Published

CI codecov npm

ReScript JSON Schema

Typesafe JSON Schema for ReScript

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
}

Create rescript-struct from JSON schema

Online

ReScript JSON Schema Online

Just paste your JSON schemas here!

Install

npm i rescript-json-schema

DownloadsWeekly Downloads

137

Version

3.0.0

License

MIT

Unpacked Size

32.7 kB

Total Files

7

Last publish

Collaborators

  • dzakh