Skip to main content

ValidateAsync

The validateAsync() method allows you to validate data asynchronously. This is useful for cases where you need to validate data that is stored in a database or other remote data store.

To use the validateAsync() method, you can register rules that return a Promise. For example, the following rule will validate that the email field contains a valid email address by making an API request to a third-party email validation service. You can read more on how to register custom rules here.

register('valid_email', async function(value) {
// Make an API request to a third-party email validation service
const response = await fetch('https://api.example.com/validate-email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: value }),
});

// Parse the JSON response
const data = await response.json();

// Return a Promise that resolves to true if the email address is valid, or false otherwise
return data.valid;
});

Now let's use the validateAsync method to run the validation against our valid_email rule:

    const validator = make()
.setData({email: 'john.doe@example.com'})
.setRules({email: 'required|string|valid_email'});

const validationResult = await validator.validateAsync();

if (!validationResult) {
console.log(validator.errors().all());
}

The validationResult will contain the result of the validation. If the validation is successful, the property will be equal to true. Otherwise, the property will be equal to false and the validator.errors().all() object will return a list of validation errors.