This is a revision of schema-to-yup
using a monorepo approach.
This library can be used to build a Yup schema from a JSON Schema, GraphQL schema (type definition) or any other similar type/class and field/properties model or schema :)
yarn add @schema-to-yup/builder @schema-to-yup/types
- Architecture
- Customization
- Schemas
- Type handlers
- Conditional logic
- Examples
- Development
- Legacy readme
The type handlers have been completely decoupled from the builder.
You can choose to:
- use the default set of type handlers
- use your own or any combination/mix
This should make it much easier to customize the internals to fit your needs and allow the community an easy path to provide extensions.
import { types } from "@schema-to-yup/types";
import { buildSchema } from "@schema-to-yup/builder";
const jsonSchema = {
// ...
};
// builds yup schema from a JSON or GraphQL schema
const yupSchema = buildSchema(jsonSchema, { types });
Using createBuilder
to create a schema builder
import { types } from "@schema-to-yup/types";
import { createBuilder } from "@schema-to-yup/builder";
const jsonSchema = {
// ...
};
// create builder
const builder = createBuilder({ types });
// use the builder
const yupSchema = builder.build(jsonSchema);
const jsonSchema2 = {
// ...
};
// reuse the builder
const yupSchema2 = builder.build(jsonSchema2);
This would generate the following Yup validation schema:
const schema = yup.object().shape({
name: yup.string().required(),
age: yup.number().required().positive(),
});
const valid = await yupSchema.isValid({
name: "jimmy",
age: 24,
});
const valid = schema.isValidSync({
type: "Person",
name: "Administrator",
level: 3,
color: "blue",
});
Please note that this library does not currently resolve $ref
(JSON Pointers) out of the box. You can use another library for that.
You could f.ex use json-schema-ref-parser to preprocess your schema. Also see:
By default, any property will be explicitly notRequired
unless set to be required, either via required: true
in the property constraint object or via the required
list of properties of the object
schema definition (of the property).
You can override the notRequired
behavior by setting it on the new mode
object of the configuration which can be used to control and fine-tune runtime behaviour.
const jsonSchema = {
title: "users",
type: "object",
properties: {
username: { type: "string" },
},
};
const yupSchema = buildSchema(jsonSchema, {
mode: {
notRequired: true, // default setting
},
});
// will be valid since username is not required by default
const valid = yupSchema.validateSync({
foo: "dfds",
});
const yupSchema = buildYup(jsonSchema, {
mode: {
notRequired: false,
},
});
// will be invalid since username is required by default when notRequired mode is disabled
const valid = yupSchema.validateSync({
foo: "dfds",
});
You can access the internal Yup shape, via shapeConfig
on the yup schema returned by the buildYup
schema builder function.
This allows you to easily mix and match to suit more advanced requirements.
const { buildYup } = require("json-schema-to-yup");
const { shapeConfig } = buildYup(json, config);
const schema = yup.object().shape({
...shapeConfig,
...customShapeConfig,
});
- JSON schema to Elastic Search mapping
- JSON Schema to GraphQL types with decorators/directives
- JSON Schema to Mongoose schema
- JSON Schema to MobX State Tree types
- Convert JSON schema to mongoose 5 schema
The library JSON Schema model builder is a powerful toolset to build a framework to create any kind of output model from a JSON schema.
If you enjoy this declarative/generator approach, try it out!
Uses jest for unit testing.
- Have unit tests that cover most of the constraints supported.
- Please help add more test coverage and help refactor to make this lib even more awesome :)
Please feel free to come with ideas and suggestions on how to further improve this library.
2018-2021 Kristian Mandrup
MIT