Registry / devops / joi-to-json-schema

joi-to-json-schema

JSON →
library5.1.0jsnpmunverified

Converts Joi validation objects to JSON Schema (draft-04) for compatibility with tools that consume JSON Schema. Version 5.1.0 is the latest stable release, maintained on GitHub under Mozilla Foundation, but sees infrequent updates. It provides best-effort conversion with support for Joi's conditional forms via oneOf clauses. Key differentiator: minimal dependency, no runtime Joi execution, and optional transformer function for post-processing. Suitable for migrating Joi schemas to JSON Schema-based validators.

npm install joi-to-json-schema
INSTALL
IMPORT
SIG · JOI-TO-JSON-SCHEMA
J
joi-to-json-schema
devopsjavascriptv5.1.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.

convert
import convert from 'joi-to-json-schema'
const convert = require('joi-to-json-schema');
Module uses ES module default export; CommonJS require works but the package is ESM-first.
default
import converter from 'joi-to-json-schema'
The default export is a function 'convert'. Can be imported with any name.
convert
const convert = require('joi-to-json-schema').default
const convert = require('joi-to-json-schema')
In CommonJS, the export is an object with a 'default' property (ES module interop).

Converts a Joi schema with required fields, defaults, regex, and conditionals to JSON Schema (draft-04).

import convert from 'joi-to-json-schema'; import Joi from 'joi'; const joiSchema = Joi.object({ name: Joi.string().required().regex(/^\w+$/), description: Joi.string().optional().default('no description provided'), a: Joi.boolean().required().default(false), b: Joi.alternatives().when('a', { is: true, then: Joi.string().default('a is true'), otherwise: Joi.number().default(0) }) }); const jsonSchema = convert(joiSchema); console.log(JSON.stringify(jsonSchema, null, 2));
Debug
Known issues
breakingOutput JSON Schema version is draft-04, not newer drafts (06, 07, 2019-09). Tools expecting newer drafts may reject or misinterpret the schema.
fix
Use a JSON Schema converter like @stoplight/spectral to upgrade the schema to a later draft.
affects: >=1.0.0
gotchaJoi's .when() conditionals are converted to oneOf with all possible branches, which might produce a schema that is logically less strict than the original Joi schema.
fix
Review the output oneOf clauses to ensure they meet your validation requirements.
affects: >=1.0.0
gotchaJoi extensions (e.g., joi-date, joi-objectid) are not supported; their schemas may be skipped or converted incorrectly.
fix
Manually define JSON Schema for unsupported Joi types or contribute support upstream.
affects: >=1.0.0
gotchaThe converter does not handle Joi.any() or Joi.alternatives().try() with multiple types beyond simple cases; may produce unexpected output.
fix
Inspected output and manually adjust if needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: joi.describe is not a function
Passing a Joi schema object that was created with a very old version of Joi (pre-v10) which used a different internal API.
fix
Upgrade Joi to a version that has the .describe() method (Joi >=10).
Cannot read property 'type' of undefined
The input Joi schema is null or undefined, or an invalid object was passed.
fix
Ensure the first argument to convert() is a valid Joi schema object.
Output schema is missing 'type' for some properties
Joi types like Joi.any() or Joi.alternatives() without explicit inner types cannot be mapped to JSON Schema types.
fix
Refine Joi schema to use specific types (string, number, etc.) wherever possible.
Upgrade
Version history
5.1.0latest on npm
Audit
Dependencies
lodashrequiredUsed for deep cloning and object utilities
Agent activity
9 hits · last 30 days
node
8
Resources
joi-to-json-schema — npm install joi-to-json-schema · libregistry