Skip to content

Latest commit

 

History

History
79 lines (61 loc) · 2.56 KB

Conditions.md

File metadata and controls

79 lines (61 loc) · 2.56 KB

Conditional logic

Basic support for when conditions as requested and outlined in this issue is now included.

Work will continue in the when-condition branch.

Sample schema using simple when constraint:

const biggyJson = {
  title: "biggy",
  type: "object",
  properties: {
    isBig: {
      type: "boolean",
    },
    count: {
      type: "number",
      when: {
        isBig: {
          is: true,
          then: {
            min: 5,
          },
        },
      },
    },
  },
};

Sample valid and invalid values with respect to biggyJson schema

const bigJson = {
  valid: {
    isBig: true,
    count: 5, // since isBig is set, must be >= 5
  },
  invalid: {
    isBig: true,
    count: 4, // since isBig is set, must be >= 5
  },
};

Currently basic support is included in schema-to-yup@1.9.0 on npmjs

More advanced conditionals support will likely be included the next major release: 2.0.

You are welcome to continue the effort to support more conditional schema logic by continuing on this branch and making PRs.

Support for if then and else conditional JSON schema constraints can likely be added using an approach like the when condition (perhaps by transalating to equivalent: when, then and otherwise).

  • if - This keyword's value MUST be a valid JSON Schema
  • then - This keyword's value MUST be a valid JSON Schema
  • else - This keyword's value MUST be a valid JSON Schema

See also json-schema-spec

Customizing conditional logic

You can now also override, extend or customize the when condition logic by passing in your own factory method for the config object entry createWhenCondition

const myWhenConditionFactoryFn = (opts = {}) => {
  const { type, key, value, when, schema, properties, config } = opts;
  // ...
};

const config = {
  createWhenCondition: myWhenConditionFactoryFn,
};
const schema = buildYup(nameJsonSchema, config);

The best and easiest way to do this is to extend the WhenCondition class which contains most of the necessary infrastructure you can further build on.

See the src/conditions/legacy folder for the legacy 1.9.0 logic that works but has limited functionality.