Registry / testing / json-schema-to-yup

json-schema-to-yup

JSON →
library1.8.8jsnpmunverified

Converts JSON Schema (draft-07) and GraphQL type definitions into Yup validation schemas. Stable version 1.8.8, with monthly releases. Differentiators include support for custom schema extensions, enum titles, and non-standard required property on fields. Also supports GraphQL schema via graphSchemaToJson. Handles common JSON Schema keywords: string (pattern, format, min/maxLength), number (minimum, maximum, exclusive), array (minItems, maxItems), object (required, noUnknown), and mixed types (enum, nullable, oneOf).

npm install json-schema-to-yup
INSTALL
IMPORT
SIG · JSON-SCHEMA-TO-YUP
J
json-schema-to-yup
testingjavascriptv1.8.8
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.

buildYup
import { buildYup } from 'json-schema-to-yup'
const buildYup = require('json-schema-to-yup').buildYup
Default export is not available; must destructure the named export.
jsonSchemaToYup
import { jsonSchemaToYup } from 'json-schema-to-yup'
import jsonSchemaToYup from 'json-schema-to-yup'
This is an alias for buildYup, also named export.
YupSchemaConfig
import type { YupSchemaConfig } from 'json-schema-to-yup'
TypeScript users should import the type for config objects.

Shows how to convert a JSON Schema object into a Yup validation schema and validate data.

import { buildYup } from 'json-schema-to-yup'; const schema = { type: 'object', properties: { name: { type: 'string', required: true, minLength: 2 }, age: { type: 'number', exclusiveMinimum: 0, maximum: 150 }, email: { type: 'string', format: 'email' } }, required: ['name', 'email'] }; const config = { errMessages: { name: { required: 'Name is required' }, age: { exclusiveMinimum: 'Age must be positive' }, email: { format: 'Invalid email' } } }; const yupSchema = buildYup(schema, config); const valid = await yupSchema.isValid({ name: 'John', age: 30, email: 'john@example.com' }); console.log({ valid }); // { valid: true }
Debug
Known issues
breakingString pattern mapping changed: pattern keyword in JSON Schema now matches using Yup's matches() instead of deprecated regex()
fix
Update to v1.5.0+ and use 'matches' property or pattern keyword. Ensure regular expressions are compatible.
affects: >=1.0.0 <1.5.0
gotchaenum_titles property is not part of JSON Schema spec and may cause unexpected behavior if combined with enum
fix
Use enum_titles only if you intend to map enum values to display labels; they are not validated against the enum values.
affects: >=1.0.0
deprecatedThe 'mix' property (typo of 'min') is deprecated; use minLength for strings or mimimum for numbers
fix
Replace 'mix' with 'minLength' for strings, 'minimum' for numbers.
affects: >=1.0.0 <1.8.0
breakingDate format strings must be parseable by JavaScript Date constructor; locale-specific formats like '1-1-1900' may fail
fix
Use ISO 8601 format (e.g., '1900-01-01') or ensure format is unambiguous.
affects: >=1.0.0
gotchaThe 'required' property on individual fields is non-standard; it is parsed but may conflict with top-level 'required' array
fix
Use top-level 'required' array for clarity, and avoid mixing both approaches in the same schema.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: buildYup is not a function
Importing default export instead of named export
fix
Use `import { buildYup } from 'json-schema-to-yup'`
YupSchemaError: Unknown field type 'symbol'
JSON Schema property type is not supported
fix
Check supported types: string, number, integer, boolean, object, array, date. Remove or rename unsupported types.
ValidationError: age must be a positive number
Using exclusiveMinimum: 0 on number field without specifying type as number
fix
Ensure the field type is 'number' or 'integer' in JSON Schema, not missing or 'string'.
Error: Cannot find module 'yup'
yup peer dependency not installed
fix
Install yup: `npm install yup` or `yarn add yup`
Upgrade
Version history
1.8.8latest on npm
Audit
Dependencies
yuprequiredPeer dependency - generates yup schema objects for validation
Agent activity
4 hits · last 30 days
node
4
Resources
json-schema-to-yup — npm install json-schema-to-yup · libregistry