qe-json-to-yup

1.0.5 • Public • Published

qe-json-to-yup

Quick start

Install

npm install qe-json-to-yup --save

Use

import { buildYupSchema } from 'qe-json-to-yup';
 
const yupSchema = buildYupSchema({
  id: {
    type: 'number',
    positive: true,
  },
  username: {
    type: 'string',
    required: true,
    min: 8,
    max: 12,
  },
  password: {
    type: 'string',
    required: true,
    matches: /^(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*])[\w!@#$%^&*]{8,}$/,
  },
  active: {
    type: 'boolean',
  },
  created: {
    type: 'date',
    min: parse('2020-04-15', 'yyyy-MM-dd', new Date()),
    max: parse('2020-04-20', 'yyyy-MM-dd', new Date()),
  },
  tags: {
    type: 'array',
    min: 3,
  },
});

This would generate the following Yup validation schema:

const schema = yup.object().shape({
  id: yup.number()
    .positive(),
  username: yup.string()
    .required()
    .min(8)
    .max(12),
  password: yup.string()
    .required()
    .matches(/^(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*])[\w!@#$%^&*]{8,}$/),
  active: yup.boolean(),
  created: yup.date()
    .min(parse('2020-04-15', 'yyyy-MM-dd', new Date()))
    .max(parse('2020-04-20', 'yyyy-MM-dd', new Date())),
  tags: yup.array()
    .min(3),
});

Package Sidebar

Install

npm i qe-json-to-yup

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

11 kB

Total Files

4

Last publish

Collaborators

  • quinta-essentia