Yup: Conditional strip

Created on 27 Jul 2018  路  13Comments  路  Source: jquense/yup

Is there any way to conditionally strip a field depending on the value of a field?

For example: strip the field after casting if a string is empty ("")

Most helpful comment

ok I think i see what you are trying to do, you want to strip the passwordConfirm if it matches the other password field? There isn't really a way to do that because stripping happens _before_ validating, so the field would be gone by the time you checked. You've also got the api wrong for when its when('theOtherField', { is: someSpecificValue: then: newSchema })

All 13 comments

yup.string().when('someFIeld', { is: '', then: s => s.strip() })

@jquense
Can't test this right now, but will it work with the same field? For example, I want to strip the password field if the same field is empty string (not based on another field)

Thanks for your input!

it should yeah

Just tried on Codesandbox and got the following error:

Cyclic dependency: "password"
https://zkrk5yldz.codesandbox.io/node_modules/toposort/index.js:29:13
https://zkrk5yldz.codesandbox.io/node_modules/toposort/index.js:43:9
Function.toposort [as array]
https://zkrk5yldz.codesandbox.io/node_modules/toposort/index.js:22:22
sortFields
https://zkrk5yldz.codesandbox.io/node_modules/yup/lib/util/sortFields.js:50:30
ObjectSchema.shape
https://zkrk5yldz.codesandbox.io/node_modules/yup/lib/object.js:252:44

My schema is defined as:

Yup.object().shape({
  email: Yup.string()
    .email('Invalid email address')
    .required('Email is required!'),
  password: Yup.string()
    .when('password', { is: '', then: s => s.strip() })
})

ah ok, try something like this then:

password: yup.lazy(value => value === '' ? yup.string().strip() : yup.string())

How would this work alongside a oneOf?

    password: yup.string(),
    passwordConfirm: yup
      .string()
      .oneOf([yup.ref('password')], 'passwords do not match')

Basically I want to validate that password is equals to passwordConfirm, and I want to strip passwordConfirm in the final output object.

Use when() in that case since the reference is another field you won't get the cyclical error

Uhm I tried it but it does complain about a cyclical error:

    passwordConfirm: yup
      .string()
      .oneOf([yup.ref('password')], 'passwords do not match')
      .when('passwordConfirm', { is: true, then: s => s.strip() })

I tried going through yup tests but couldnt find anything related :thinking:

ok I think i see what you are trying to do, you want to strip the passwordConfirm if it matches the other password field? There isn't really a way to do that because stripping happens _before_ validating, so the field would be gone by the time you checked. You've also got the api wrong for when its when('theOtherField', { is: someSpecificValue: then: newSchema })

ohh makes sense, thats why i couldnt even find a test for this use case. Thanks for helping :)

Hello guy anyone can help me with this. I was trying to validate my field if the use type something on the current password filled the newpass and confirmpass will show an error "Required" I am always getting this error "Error: Cyclic dependency, node was:"confirmPassword"".

Thank you in advance :)

 currentPassword: Yup.string().when(['newPassword', 'confirmPassword'], {
  is: (newPassword, confirmPassword) => newPassword && confirmPassword,
  then: Yup.string().required('Required')
}),

newPassword: Yup.string()
  .when('currentPassword', {
    is: currentP => currentP,
    then: Yup.string().required('Required')
  })
  .oneOf([Yup.ref('confirmPassword'), null], props.t('error:not_match'))
  .matches(/[a-zA-Z]/, 'Password must contain at least one letter.')
  .matches(/[0-9]/, 'Password must contain at least one numeral.')
  .matches(/^[a-zA-Z0-9]*$/, 'Password must contain only alphanumeric')
  .min(8, 'Password must contain 8 characters.'),

confirmPassword: Yup.string()
  .oneOf([Yup.ref('newPassword'), null], props.t('error:not_match'))
  .when('currentPassword', {
    is: currentP => currentP,
    then: Yup.string().required('Required')
  })

There are a few issues that cover cyclical errors and how to fix them, please check those 馃憤

Help pls

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rigids picture rigids  路  3Comments

jgcmarins picture jgcmarins  路  4Comments

cfteric picture cfteric  路  3Comments

laurazenc picture laurazenc  路  3Comments

ghost picture ghost  路  4Comments