Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to validate: when on array is length n #568

Closed
royipressburger opened this issue Jul 4, 2019 · 1 comment
Closed

How to validate: when on array is length n #568

royipressburger opened this issue Jul 4, 2019 · 1 comment

Comments

@royipressburger
Copy link

Hi

I have the next object:

{
   arrayA: [1,2,3],
   arrayB: [
     { a: 1, b: 2, c:3 },
   ],
}

I would like to validate that:

  1. On arrayB: a and b are required if arrayA includes 1.
  2. On arrayB: c is required if arrayB length is bigger then 1.
array().when('arrayA', { // this is working!
   is: arrayA => arrayA.includes(1),
   then: array().of(object().shape({a: string().required(), b: string().required() })),
}).when('arrayB', { // this is not working! How do I get the value of arrayB here?
   is: arrayB => arrayB.length > 1,
   then: array().of(object().shape({ c: string().required() })),
});
@royipressburger royipressburger changed the title how to do: when on array length n How to validate: when on array is length n Jul 4, 2019
@royipressburger
Copy link
Author

K just found out that I was running an old version 0.26 and version 0.27 has self reference.

pull request here;

so the solution:

array().when('arrayA', { // this is working!
   is: arrayA => arrayA.includes(1),
   then: array().of(object().shape({a: string().required(), b: string().required() })),
}).when({ // just pass the object as first argument and is function will get arrayB
   is: arrayB => arrayB.length > 1,
   then: array().of(object().shape({ c: string().required() })),
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant