jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

test and when not working together #2179

Open Shivampio opened 8 months ago

Shivampio commented 8 months ago

Describe the bug When I am trying to use both when and test validation only one of it is working.

To Reproduce

yup
        .mixed()
        .default(null)
        .when('includeAttachment', (includeAttachment, field) => {
          if (
            (includeAttachment || includeAttachment == 'true') &&
            !bannerData?.fileUrl
          ) {
            return yup.mixed().required('File is a required.')
          }
          return field
        })
        .test('filezise', 'error', (file, { createError }: yup.TestContext) => {
          if (file) {
            const type = file?.type.split('/')[1]
            const validTypes = ['pdf', 'jpeg', 'png', 'jpg', 'gif']
            const size = file.size / (1024 * 1024)
            if (size > 10) {
              return createError({ message: 'File exceeds 10MB' })
            }
            if (!validTypes.includes(type)) {
              return createError({ message: 'File is not of supported type' })
            }
          }
          return true
        })

Expected behavior Should both need to work so that I can handle multiple validations

Platform (please complete the following information):

Sinled commented 7 months ago

Also have this problem. Is there any ways to replicate old behaviour?

jquense commented 7 months ago

please provide a runnable reproduction using the issue template

Sinled commented 6 months ago

please provide a runnable reproduction using the issue template

@jquense here is reproduction with tests https://github.com/jquense/yup/issues/2205