Strickland
  • Readme
  • Introduction
    • Validators
    • Validation
    • Validation Results
  • Extensibility
    • Validator Factories
    • Validation Context
    • Validation Result Props
    • Extensibility Pattern
    • formatResult
  • Built-In Validators
    • required
    • compare
    • min
    • max
    • range
    • minLength
    • maxLength
    • length
  • Composition
    • Arrays of Validators
      • every
      • all
      • some
    • Validating Array Elements
      • arrayElements
    • Validating Objects
      • objectProps
      • Advanced Object Validation
      • Nested Objects
      • Arrays of Objects
    • Composition Conventions
    • Composition and formatResult
  • Async Validation
    • Resolving Async Validation
    • Deferred Async Validation
    • Async Validator Arrays and Objects
    • Two-Stage Sync/Async Validation
    • Race Conditions
    • Automatic Race Condition Handling
    • Async Validation and formatResult
  • Form Validation
    • form
    • Async Form Validation
    • validateFields
    • emptyResults
    • updateFieldResults
  • Inspiration
  • Design Goals
  • Wrap-Up
  • Change Log
  • NPM
  • GitHub
Powered by GitBook
On this page

Was this helpful?

  1. Async Validation

Resolving Async Validation

Because validate returns synchronously, your application must recognize when async validation needs to be resolved. The validation result returned from validate will only include the validateAsync property when a async validation needs to be resolved. If the validateAsync result property exists, it will be a function that returns a Promise.

const result = validate(usernameIsAvailable, 'marty');

// The application defines a `handleValidationResult` function
// to handle validation results when the are completed

if (result.validateAsync) {
    result.validateAsync().then((asyncResult) => handleValidationResult(asyncResult));
} else {
    handleValidationResult(result);
}

Async Validation Helper: validateAsync

To avoid boilerplate code, Strickland provides a validateAsync function as a named export. This function always returns a Promise, regardless of whether validation completed synchronously or needs to be resolved asynchronously.

import {validateAsync} from 'strickland';

const result = validateAsync(usernameIsAvailable, 'marty');
result.then((asyncResult) => handleValidationResult(asyncResult));
PreviousAsync ValidationNextDeferred Async Validation

Last updated 4 years ago

Was this helpful?