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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With