You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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';exportconstcompanyValidationSuite=create('companyValidationSuite',(model,field?)=>{only(field);// if field defined, limit to tests of this fieldtest('name','Name is required',()=>{enforce(model.name).isNotEmpty();});group('billingAddress',()=>companyAddressValidations(model.billingAddress!));group('officeAddress',()=>companyAddressValidations(model.officeAddress!));});exportconstcompanyAddressValidations=(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:
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.
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:
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.
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:
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:
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 theofficeAddress
group.The text was updated successfully, but these errors were encountered: