Registry / development / rescript-json-schema

rescript-json-schema

JSON →
library7.3.0jsnpmunverified

A type-safe JSON Schema library for ReScript, version 7.3.0 (actively maintained). It allows developers to define JSON schemas using ReScript's type system and convert between rescript-schema definitions and JSON Schema (Draft-07). Key differentiator: tight integration with rescript-schema for parsing, serialization, and code generation. Provides online tool for conversion. Alternatives like ajv or typical JSON Schema libraries lack ReScript-specific type safety.

npm install rescript-json-schema
INSTALL
IMPORT
SIG · RESCRIPT-JSON-SCHE
R
rescript-json-schema
developmentjavascriptv7.3.0
harness data pending
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)
Debug
Known issues
breakingMajor version 7 introduced breaking changes from previous versions.
fix
Upgrade to v7 and update code to use rescript-schema v9 compatible API.
affects: <7.0.0
breakingPeer dependency on rescript-schema ^9.0.0 is required; older versions of rescript-schema are incompatible.
fix
Ensure rescript-schema is at version 9 or later.
affects: >=7.0.0
gotchaJSON Schema output is Draft-07 only; does not support newer drafts like 2020-12.
fix
If you need a different draft, you must convert externally.
affects: all
gotchaThe open JsonSchema; directive may conflict with other modules named JsonSchema; use explicit module import if needed.
fix
Use module aliasing: module JsonSchema = RescriptJsonSchema.JsonSchema
affects: all
deprecatedVersion 7 is the latest stable; older versions are unmaintained.
fix
Upgrade to 7.3.0.
affects: <7.0.0
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.
fix
Ensure 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.
fix
Add "rescript-json-schema" to bs-dependencies and restart build.
Cannot find package rescript-schema
Missing npm install of rescript-schema.
fix
Run npm install rescript-schema.
Error: Unknown variant type
Using @as decorator incorrectly on variant constructors.
fix
Use @as("value") syntax directly on constructor, not on type.
Upgrade
Version history
7.3.0latest on npm
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.
Agent activity
39 hits · last 30 days
node
32
OpenAI (training)
1
Resources
rescript-json-schema — npm install rescript-json-schema · libregistry