Skip to content

kristianmandrup/schema-to-yup-mono

Repository files navigation

schema-to-yup

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 :)

Installation

yarn add @schema-to-yup/builder @schema-to-yup/types

Documentation

Usage

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.

Quick start

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(),
});

Async validation

const valid = await yupSchema.isValid({
  name: "jimmy",
  age: 24,
});

Sync yup validation

const valid = schema.isValidSync({
  type: "Person",
  name: "Administrator",
  level: 3,
  color: "blue",
});

Advanced usage guide

Refs

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:

Runtime Mode

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" },
  },
};

Mode examples

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",
});

Shape

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,
});

Similar projects

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!

Testing

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 :)

Ideas and suggestions

Please feel free to come with ideas and suggestions on how to further improve this library.

Author

2018-2021 Kristian Mandrup

License

MIT

About

Monorepo for schema-to-yup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published