Registry / serialization / schema-to-yup

schema-to-yup

JSON →
library1.12.18jsnpmunverified

schema-to-yup is a utility library for generating Yup validation schemas directly from declarative schema definitions such as JSON Schema or GraphQL type definitions. It helps automate the creation of validation logic, reducing boilerplate, especially in applications that consume or define data models externally. Currently at version 1.12.18, the package appears to be actively maintained, indicated by regular minor releases. A key differentiator is its configurability, allowing developers to customize error messages, enforce default `required` behavior, and integrate with existing schema ecosystems. While it supports common JSON Schema features, users should be aware that it does not automatically resolve `$ref` (JSON Pointers), requiring a pre-processing step with another library if `$ref`s are present in the input schema.

npm install schema-to-yup
INSTALL
IMPORT
SIG · SCHEMA-TO-YUP
S
schema-to-yup
serializationjavascriptv1.12.18
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

buildYup
import { buildYup } from 'schema-to-yup';
const { buildYup } = require('schema-to-yup');
While CommonJS `require` works, ESM `import` is preferred, especially in newer Node.js environments or projects using TypeScript.
YupBuilderConfig
import type { YupBuilderConfig } from 'schema-to-yup';
This is a TypeScript type import, used for type-checking the configuration object.
TypeHandlerConfig
import type { TypeHandlerConfig } from 'schema-to-yup';
This is a TypeScript type import, typically used for advanced configuration of type handling within the builder.

This quickstart demonstrates how to define a JSON schema, configure custom error messages, build a Yup schema using `buildYup`, and then use the generated schema to validate data, showing both valid and invalid scenarios.

import { buildYup } from 'schema-to-yup'; import * as yup from 'yup'; const jsonSchema = { $schema: "http://json-schema.org/draft-07/schema#", $id: "http://example.com/person.schema.json", title: "Person", description: "A person", type: "object", properties: { name: { description: "Name of the person", type: "string" }, email: { type: "string", format: "email" }, age: { description: "Age of person", type: "number", exclusiveMinimum: 0 } }, required: ["name", "email"] }; const config = { errMessages: { age: { required: "A person must have an age", exclusiveMinimum: "Age must be greater than zero" }, email: { required: "You must enter an email address", format: "Not a valid email address" } } }; const yupSchema = buildYup(jsonSchema, config); async function validateExample() { // Example of valid data const validData = { name: "Jane Doe", email: "jane@example.com", age: 30 }; const isValid1 = await yupSchema.isValid(validData); console.log('Valid data result:', isValid1); // Expected: true // Example of invalid data (missing required field, invalid email format) const invalidData = { name: "John", email: "invalid-email" }; try { await yupSchema.validate(invalidData, { abortEarly: false }); } catch (error) { if (error instanceof yup.ValidationError) { console.log('Invalid data errors:', error.errors); } else { console.error('Validation unexpected error:', error); } } } validateExample();
Debug
Known issues
gotchaThe library does not automatically resolve `$ref` (JSON Pointers) within the input schema. If your JSON schemas use `$ref`s, you must preprocess them with a separate library (e.g., `json-schema-ref-parser`) before passing them to `schema-to-yup`.
fix
Use a JSON Schema dereferencing library like `json-schema-ref-parser` to preprocess your schema: `const dereferencedSchema = await refParser.dereference(originalSchema); const yupSchema = buildYup(dereferencedSchema, config);`
affects: >=1.0.0
gotchaBy default, properties in the generated Yup schema are explicitly `notRequired` unless marked `required` in the input JSON Schema. This behavior can be confusing if you expect all properties to be required by default. You can override this through the `mode` configuration.
fix
To change the default behavior for properties not explicitly marked `required`, configure the `mode` option: `const yupSchema = buildYup(jsonSchema, { mode: { notRequired: false } });` Setting `notRequired: false` will make properties required by default if not specified otherwise.
affects: >=1.0.0
Upgrade
Version history
1.12.18latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
schema-to-yup — npm install schema-to-yup · libregistry