kristianmandrup / schema-to-yup

Schema to Yup validation
Other
283 stars 50 forks source link

Build properties only #118

Closed albertcito closed 2 years ago

albertcito commented 2 years ago

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);
kristianmandrup commented 2 years ago

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

albertcito commented 2 years ago

I figure out I can do it as well:

const schema = Yup.object().shape({ name: Yup.string().required() }); 
const { name } = schema.fields
kristianmandrup commented 2 years ago

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