API Reference
Below is a list of all the API functions exposed by Vest.
Vest's main export API​
create(callback, schema?)​
Creates a new validation suite. Returns a Suite Object.
-
callback: The validation logic. -
schema(Optional): Anenforceschema definition.
Suite Object Methods​
suite.run(...args)​
Runs the suite. Passes arguments to the suite callback.
- Returns: A
SuiteResultobject.- If the suite contains async tests, the result object also implements the Promise interface, allowing you to
awaitit. - You can always access synchronous result data immediately (e.g.,
result.hasErrors()), even if the promise is pending.
- If the suite contains async tests, the result object also implements the Promise interface, allowing you to
- Read more about
suite.run
suite.runStatic(...args)​
Runs the suite in stateless mode. Useful for server-side validation.
suite.reset()​
Resets the suite state (clears all results).
suite.remove(fieldName)​
Removes a specific field from the suite result state.
suite.resetField(fieldName)​
Resets the state of a specific field (clears errors/warnings but keeps it in the result).
suite.focus(config)​
Prepares a focused run.
config:{ only?: string | string[], skip?: string | string[] }- Read more about Focused Updates
suite.afterEach(callback)​
Registers a callback to run after each test completes (including the initial sync run and every async completion).
suite.afterField(fieldName, callback)​
Registers a callback to run when a specific field finishes execution.
suite.get()​
Returns the current result object of the suite without running it. Useful for accessing the state inside UI components or subscribers.
SuiteSerializer.serialize(suite)​
Returns a minified, serialized representation of the suite's state. Useful for SSR hydration.
SuiteSerializer.resume(suite, data)​
Hydrates the suite with a serialized state.
suite: The suite to resume.data: The serialized state object.- Read more about SSR Hydration
suite.validate(data)​
Runs the suite and returns a result compatible with the Standard Schema specification.
Top-Level Exports​
enforce.context()​
Retrieves the current validation context during a suite run. Useful within custom rules to access other fields in the data object.
- Returns:
{ data: Object, value: any, ... } - Read more about Context Aware Rules
enforce.extend(customRules)​
Extends Vest's enforce with custom validation rules.
- Tip: To add TypeScript support for your custom rules, see TypeScript Support.
memo(callback, deps)​
Memoizes a block of tests.
compose(...rules)​
Combines multiple enforce rules.
test(fieldName, message, callback)​
A single validation test inside your suite.
enforce(value)​
Asserts that a value matches your desired result.
warn()​
Sets the test's severity to warning.
only(fieldName)​
Makes Vest only run the provided field names.
skip(fieldName)​
Makes Vest skip the provided field names.
include(fieldName).when(condition)​
Link fields by running them together based on a criteria.
skipWhen(condition, callback)​
Skips a portion of the suite when the provided condition is met.
omitWhen(condition, callback)​
Omits a portion of the suite when the provided condition is met.
optional(fieldName)​
Allows you to mark a field as optional.
group(groupName, callback)​
Allows grouping multiple tests with a given name.
each(list, callback)​
Allows iteration over an array of values to dynamically run tests.
mode(mode)​
Determines whether Vest should continue running tests after a field has failed.
Suite Result API​
After running your suite, the results object is returned. It has the following functions:
-
hasErrors(fieldName?): Returns true if the suite or the provided field has errors. -
hasWarnings(fieldName?): Returns true if the suite or the provided field has warnings. -
getErrors(fieldName?): Returns an object with errors in the suite, or an array of objects for a specific field. -
getWarnings(fieldName?): Returns an object with warnings in the suite, or an array of objects for a specific field. -
hasErrorsByGroup(groupName): Returns true if the provided group has errors. -
hasWarningByGroup(groupName): Returns true if the provided group has warnings. -
getErrorsByGroup(groupName): Returns an object with errors in the provided group. -
getWarningsByGroup(groupName): Returns an object with warnings in the provided group. -
isPending(fieldName?): Returns true if the suite has pending async tests. -
isTested(fieldName): Returns true if the provided field has been tested. -
isValid(fieldName?): Returns true if the suite or the provided field is valid. -
isValidByGroup(groupName): Returns true if a certain group or a field in a group is valid or not.