Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
JSONSchema
✓ open JSONSchema; JSONSchema.make(schema)
JSONSchema module is part of the package. Use open to bring functions into scope.
S (from rescript-schema)
✓ open RescriptSchema; let s = S.object(...)
✗ let s = RescriptSchema.S.object(...)
Recommended to add -open RescriptSchema in bsc-flags for ergonomics.
rescript-json-schema module
✓ open JsonSchema;
✗ let jsonSchema = require('rescript-json-schema')
The package is a ReScript library; used via open/import in ReScript files, not require.
Demonstrates defining a ReScript type, creating a rescript-schema, and generating a JSON Schema object with deprecation markers.
// Install: npm install rescript-json-schema rescript-schema
// In rescript.json add: "bs-dependencies": ["rescript-json-schema", "rescript-schema"], "bsc-flags": ["-open RescriptSchema"]
type rating =
| @as("G") GeneralAudiences
| @as("PG") ParentalGuidanceSuggested
| @as("PG13") ParentalStronglyCautioned
| @as("R") Restricted
type film = {
id: float,
title: string,
tags: array<string>,
rating: rating,
deprecatedAgeRestriction: option<int>,
}
let filmSchema = S.object(s => {
id: s.field("Id", S.float),
title: s.field("Title", S.string),
tags: s.fieldOr("Tags", S.array(S.string), []),
rating: s.field(
"Rating",
S.union([
S.literal(GeneralAudiences),
S.literal(ParentalGuidanceSuggested),
S.literal(ParentalStronglyCautioned),
S.literal(Restricted),
]),
),
deprecatedAgeRestriction: s.field("Age", S.option(S.int)->S.deprecate("Use rating instead")),
})
// Generate JSON Schema
let jsonSchema = JSONSchema.make(filmSchema)
// Print it
Js.log(jsonSchema)
Errors
Common errors & fixes
This expression has type 'a but an expression was expected of type 'b' when using S.field
Mismatch between schema field type and inferred ReScript type.
fixEnsure the field value type matches the schema e.g., S.float for float fields.
Unbound module JSONSchema
Missing open or incorrect bs-dependencies in rescript.json.
fixAdd "rescript-json-schema" to bs-dependencies and restart build.
Cannot find package rescript-schema
Missing npm install of rescript-schema.
fixRun npm install rescript-schema.
Error: Unknown variant type
Using @as decorator incorrectly on variant constructors.
fixUse @as("value") syntax directly on constructor, not on type. Audit
Dependencies
rescriptrequiredPeer dependency: ReScript compiler v11.x required.
rescript-schemarequiredPeer dependency: rescript-schema ^9.0.0 provides the schema definitions that are converted to/from JSON Schema.