Accessing Vest's Result
Vest validations return a results object that holds all the information regarding the current run and methods to interact with the data.
A result object would look somewhat like this:
{
'valid': false, // Whether the suite as a whole is valid or not
'errorCount': 0, // Overall count of errors in the suite
'warnCount': 0, // Overall count of warnings in the suite
'testCount': 0, // Overall test count for the suite (passing, failing and warning)
'pendingCount': 0, // Overall count of unresolved async tests in the suite
'tests': { // An object containing all non-skipped tests
['fieldName']: { // Name of each field
'errorCount': 0, // Error count per field
'errors': [], // Array of error messages fer field (may be undefined)
'warnings': [], // Array of warning messages fer field (may be undefined)
'warnCount': 0, // Warning count per field
'testCount': 0, // Overall test count for the field (passing, failing and warning)
'pendingCount': 0, // Overall count of unresolved async tests in the current field
'valid': false, // Field specific validity
},
'groups': { // An object containing groups declared in the suite
['fieldName']: { // Subset of res.tests[fieldName]
/*... */ // only containing tests that ran within the group
}
}
}
'errors': [ // An array containing all the errors occurred in order
{
fieldName: "fieldname",
groupName: undefined, // or whatever group we're in
message: "validation message"
},
],
'warnings': [{ // An array containing all the warnings occurred in order
fieldName: "fieldname",
groupName: undefined, // or whatever group we're in
message: "validation message"
}]
}
Suite Result Methods
Along with this data, our result object also contains a few other methods that can be used to interact with the data. All these methods can be accessed in the following ways:
- Directly via the result object returned by the suite.
- Calling the method on the suite itself.
- Via the
suite.get()
method.
All the following examples are valid and equivalent:
const result = suite(formData);
// 1 - Directly via the result object
result.hasErrors();
// 2 - Calling the method on the suite itself
suite.hasErrors();
// 3 - Via the `suite.get()` method
suite.get().hasErrors();
isValid
isValid
returns whether the validation suite as a whole or a single field is valid or not.
Suite validity
A suite is considered valid if the following conditions are met:
- There are no errors (
hasErrors() === false
) in the suite - warnings are not counted as errors. - All non optional fields have passing tests.
- There are no pending async tests.
result.isValid();
suite.isValid();
suite.get().isValid();
Field validity
A field is considered valid if the following conditions are met:
- The field has no errors (
hasErrors() === false
) or the field is omitted via the functional "optional" API. - All non-optional tests for the field are passing.
- The field has no pending tests.
result.isValid('username');
suite.isValid('username');
suite.get().isValid('username');
When isValid
equals false
it does not necessarily mean that the form is inValid. It only means that might not be valid yet. For example, if not all the fields are filled, the form is not valid yet, even though it may not be strictly invalid.
hasErrors
and hasWarnings
If you only need to know if a certain field has validation errors or warnings but don't really care which they are, you can use hasErrors
or hasWarnings
functions.
resultObject.hasErrors('username');
// true
resultObject.hasWarnings('password');
// false
In case you want to know whether the whole suite has errors or warnings (to prevent submit, for example), you can use the same functions, just without specifying a field
resultObject.hasErrors();
// true
resultObject.hasWarnings();
// true
isValidByGroup
Similar to isValid
, but returns the result for a specified group. Providing a group name that doesn't exist will return false
. When adding a fieldName, only the field within that group will be checked.
resultObject.isValidByGroup('groupName', 'fieldName');
resultObject.isValidByGroup('groupName');