kristianmandrup / schema-to-yup

Schema to Yup validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build properties only

albertcito opened this issue · comments

I'm working with a library for my forms. So I have to register the input in this way

const name = register('name', Yup.string().trim().required());

Is there a way to build the properties only? In this way for instance:

const { buildYupProp } = require("schema-to-yup");
const nameValidator = buildYupProp({
  description: "Name of the person",
  type: "string",
  required: true,
  trim: true,
  ...
});
const name = register('name', nameValidator);

I believe there is a shape property on the builder where you can get the Yup shape object with props if that is what you mean

I figure out I can do it as well:

const schema = Yup.object().shape({ name: Yup.string().required() }); 
const { name } = schema.fields

You can do sth like this using shapeConfig

const { buildYup } = require("json-schema-to-yup");
const name = {
  description: "Name of the person",
  type: "string",
  required: true,
  trim: true,
  ...
}
const json = {
  properties: {
    name
  }
} 

const { shapeConfig } = buildYup(json, config);
const schema = yup.object().shape({
  ...shapeConfig,
  ...customShapeConfig,
});
const { name } = schema.fields