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

Multiple groups using same tests function #1131

Open
mellondev opened this issue Jan 3, 2024 · 2 comments
Open

Multiple groups using same tests function #1131

mellondev opened this issue Jan 3, 2024 · 2 comments

Comments

@mellondev
Copy link

Is it possible to reuse a validation suite multiple times using groups.

The scenario I have is a company details model has 2 different addresses, the validation is the same for each address so rather than duplicating the tests I tried to reuse as below:

import { create, enforce, group, only, test } from 'vest';

export const companyValidationSuite = create(
  'companyValidationSuite',
  (model, field?) => {
    only(field); // if field defined, limit to tests of this field

    test('name', 'Name is required', () => {
      enforce(model.name).isNotEmpty();
    });

    group('billingAddress', () =>
      companyAddressValidations(model.billingAddress!)
    );

    group('officeAddress', () =>
      companyAddressValidations(model.officeAddress!)
    );
  }
);

export const companyAddressValidations = (model: any) => {
  test('line1', 'Line 1 is required', () => {
    enforce(model.line1).isNotEmpty();
  });

  test('city', 'City is required', () => {
    enforce(model.city).isNotEmpty();
  });

  test('postCode', 'Post Code is required', () => {
    enforce(model.postCode).isNotEmpty();
  });
};

If I then execute the suite as below, it only returns errors for the first group a total of 4 errors but there is 7 errors as both address groups have errors, it shows the field name and groupname in the errors array:

    const result = companyValidationSuite({});
    console.log(result);

If I then supply a value for one of the fields in one of the address (as below), the result is the same number of errors (4) but this time there are 2 errors for the billingAddress group and 1 error for the officeAddress group.

    const result = companyValidationSuite({ billingAddress: { line1: 'test' }});
    console.log(result);
@mellondev
Copy link
Author

As soon as I posted this I remembered about the v5 changes regarding Eager().

If I switch back to the previous mode using mode(Modes.ALL); it now works as expected.

Should the Eager execution be scoped within the group maybe?

@ealush
Copy link
Owner

ealush commented Jan 3, 2024

I wouldn't make it behave differently just for groups by default as it would break the principle of least astonishment.

Two alternatives I can see:

  1. We can possibly make the mode function work within a scope, similar to the 'skip' function. That would not solve your case specifically as these would be two different groups. To combat that, nothing stopping us from putting both named groups in one parent unnamed group.
group(() => {
	group("named_1", () => {
		test(...);
	});

	group("named_2", () => {
		test(...);
	})
})
  1. We could make a new execution mode that would provide this behavior. Need to think what its full specification could be.

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