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

Nested fields ? #356

Closed
mathieutu opened this issue Aug 31, 2020 · 1 comment
Closed

Nested fields ? #356

mathieutu opened this issue Aug 31, 2020 · 1 comment

Comments

@mathieutu
Copy link

Hi,
Thanks for your work and this package.

I would like to validate objects with a lot of nested structure, like this:

{
	foos: [{ bar: '...' }, { bar: '...' }, { bar: '...' }, ...]
}

Do you recommend a particular way to write the validation tests with vest?

I tought about something basic like this, but maybe is there a better way?

const validate = vest.create('form', (formData) => {
	formData.foos.forEach((fooObject) => {
		test('bar', 'Must be between 2 and 10 chars', () => {
	        enforce(fooObject.bar).longerThanOrEquals(2).shorterThan(10);
	    });
	})
});

Thanks,
Mathieu.

@ealush
Copy link
Owner

ealush commented Sep 1, 2020

Hey @mathieutu, thanks for reaching out!

The problem of dynamically generated fields, or fiels queried from the server does come up every once in a while, and the way you handle it is the most straightforward way of doing it. There aren't any drawbacks to using it. It's quite simple to maintain and debug, and I should probably add it to the docs as well. Thank you for bringing that up!

As a side note, in the future I plan on adding test.each() that would behave similarly to jest's test.each function. It would have a built-in integration with vest.only so that skipped fields won't be iterated over to begin with - so you can save some precious runtime perf.

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

2 participants