Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a yup schema from another file?

I am wanting to organise my form code by placing schemas in separate files. I have noticed that when I export a yup schema, then import it into another schema it always fails. The schema data appears to be is correct (when I console log), however, the validation of the imported schema never runs.

Example:

myAdditionalSchema.js

export const otherSchema = object({
 someValue: string(),
})

myMainSchema.js

import { otherSchema } from "myAdditionalSchema"

export const constMainSchema = object({
  myValue: string(),
}).concat(otherSchema)

If I have all of these schemas in the same file I do not experience this issue, only when they are imported.

For example, this works:

export const constMainSchema = object({
  myValue: string(),
}).concat(object({
 someValue: string(),
}))

And this also works:

const otherSchema = object({
 someValue: string(),
})

export const constMainSchema = object({
  myValue: string(),
}).concat(otherSchema)

Is there something different I have to do when importing them? For context I am using this with Formik.

like image 933
Charklewis Avatar asked Nov 20 '25 06:11

Charklewis


1 Answers

Turns out this does work. There was an issue with number().transform() that was causing a bug (that I don't fully understand) when the schema is imported. That bug however does not persist when the schema is created in the same file.

like image 55
Charklewis Avatar answered Nov 24 '25 22:11

Charklewis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!