Create dynamic Yup validation schema.

PHOTO EMBED

Fri Feb 25 2022 21:10:50 GMT+0000 (Coordinated Universal Time)

Saved by @antistructure #javascript

import * as yup from "yup";

export function createYupSchema(schema, config) {
  const { id, validationType, validations = [] } = config;
  if (!yup[validationType]) {
    return schema;
  }
  let validator = yup[validationType]();
  validations.forEach(validation => {
    const { params, type } = validation;
    if (!validator[type]) {
      return;
    }
    console.log(type, params);
    validator = validator[type](...params);
  });
  schema[id] = validator;
  return schema;
}
content_copyCOPY

Using yup.js with vue.js

https://github.com/jquense/yup/issues/559