-
Notifications
You must be signed in to change notification settings - Fork 936
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 apply different validation to each element of an array? #528
Comments
Not sure if it'd work but, did you try treating the array as an object? const schema = object().shape({
0: string(),
1: number(),
2: boolean(),
length: number()
}) |
What you seem to be looking for is basic |
@eddyw thanks for the suggestion. I tried it out but did not get the behavior that I was looking for. |
@jquense I am actually using Yup in the context of Formik validation. I was able to implement the behavior I was after using Yup's mixed.test() API. So the validation schema looks something like: const schema = Yup.object().shape({
values: Yup.array(),
'values[0]': Yup.number().test('test-0', 'test-msg', function() {
// test for values[0]
return this.parent && this.parent.values && this.parent.values[0] !== undefined
}),
'values[1]': Yup.number().test('test-1', 'test-msg', function() {
// test for values[1]
return this.parent && this.parent.values && this.parent.values[1] > this.parent.values[0]
}),
'values[2]': Yup.number().test('test-2', 'test-msg', function() {
// test for values[2]
return this.parent && this.parent.values && this.parent.values[2] > this.parent.values[1]
})
}) Basically, in Thanks for the help. |
@otori23 is there a mistake in your example ?
Thank you so much |
how about target path to last index maybe const schema = Yup.object().shape({
values: Yup.array(),
'values[0]': Yup.number(),
'values[lastIndex]': Yup.string()
}) |
I'm using this approach to validate file uploads but I'm having issues. The minimum amount of file expected is one and a maximum of five. This means I have to write validations for the maximum number of files. Things seem to not work out if the maximum number of files is not uploaded; it returns an error. For example
If only one file is uploaded and meet the criteria , errors for the second file is thrown. What do I need to do to make sure that validation only runs for the number of uploaded files.? |
did you solved it? I can not make it work neither :( |
Why was this closed? The above solutions do not work in Typescript. |
tuples are used by other frameworks though, like date/calendar inputs and adding an extra state to fit the validation framework even though the ui framework manages the state just fine is kinda smelly |
I had luck adding a new method to Yup.addMethod(Yup.array, 'tuple', function(schema) {
if (!this.isType(schema)) Yup.ValidationError();
return Yup.object({
tuple: Yup.array().min(schema.length).max(schema.length),
...Object.fromEntries(Object.entries(schema)),
}).transform((value, originalValue) => {
if (!this.isType(originalValue)) Yup.ValidationError();
return {
tuple: originalValue,
...Object.fromEntries(Object.entries(originalValue))
};
});
});
// Create your schema
const MySchema = Yup.array().tuple([
Yup.number(),
Yup.string(),
Yup.boolean(),
]);
// Validate your tuple
const { tuple } = await MySchema.validate([1999, "ja", true]);
console.log(tuple); // [1999, "ja", true] Check it out on RunKit - [here] |
fyi tuple has been released within the |
Hi thank you for this solution but it possible to apply this method in an older version but use objects within tuple? |
I have multiple such objects in array. But I am on an older version of Yup and not sure how to apply this
|
This doesn't work |
you also may to use the build-in method import { tuple, string, number, InferType } from 'yup';
let schema = tuple([
string().label('name'),
number().label('age').positive().integer(),
]);
await schema.validate(['James', 3]); // ['James', 3]
await schema.validate(['James', -24]); // => ValidationError: age must be a positive number
InferType<typeof schema> // [string, number] | undefined |
You can use
|
I have an array of three numbers: [min, mid, max], and I want to apply different validation function(s) to each element of the array. I don’t quite know how to achieve this with Yup.
Example use cases:
-or-
I know something like array().of(number()…) will apply the same validation to each element in the array, but this is not what I want. Each element in the array has its own validation requirements.
Thanks.
The text was updated successfully, but these errors were encountered: