Skip to content

✅ A utility for converting a JSON schema into Yup schema.

License

Notifications You must be signed in to change notification settings

shipwell/json-yup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f5c503b · Jan 16, 2020

History

12 Commits
Jan 14, 2020
Jan 14, 2020
Jan 14, 2020
Dec 9, 2019
Dec 9, 2019
Dec 9, 2019
Dec 9, 2019
Jan 3, 2020
Jan 3, 2020
Dec 9, 2019
Jan 3, 2020
Dec 9, 2019
Dec 9, 2019
Jan 3, 2020
Jan 3, 2020
Dec 9, 2019

Repository files navigation

json-yup

npm npm

A simple utility for converting a JSON schema into Yup schema.

Setup

npm install -S json-yup
import createValidationSchema from 'json-yup';

API

createValidationSchema(schema, options);

Arguments

Argument Type Description
schema Object A valid JSON schema conforming to JSON schema specifications.
customValidationFields Object Custom Yup mappings for schema properties

Options

Property Type Description
blackList Array A list of fields to omit from the schema.
customValidationFields Object Custom Yup mappings for schema properties
validationTypes Object Custom Yup mappings for schema types.

Returns

Yup validation object

Usage

// Valid JSON Schema
const jsonSchema = {
  "type": "object",
  "required": [
    "first_name"
  ],
  "properties": {
    "create_at": {
      "type": "string"
    },
    "first_name": {
      "type": "string"
    },
    "age": {
      "type": "number",
      "min": 1,
      "max": 200
    }
  }
};

// Build Yup Schema
const validationSchema = createValidationSchema(jsonSchema, {
  blackList: [
    'create_at'
  ],
  validationTypes: {
    string: yup.string().nullable()
  },
  customValidationFields: {
    first_name: yup.string().test(
      'isWilly',
      'Oops! You\'re not Willy',
      value => value === 'Willy'
    )
  }
});

// Check validity
validationSchema.isValid({
  first_name: 'Willy',
  age: 24
})