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. Composition

Validating Objects

We've demonstrated quite a bit of flexibility validating single values with Strickland, including how arrays of validators can be composed together. But every application needs to validate objects. Let's see how Strickland can do this.

We will start by illustrating what object validation looks like with no additional Strickland features. This example will validate a person's first name, last name, and birth year.

import validate, {
    required, length, range, every
} from 'strickland';

// Define the rules for first name, last name, and birthYear
const personValidator = {
    firstName: every([
        required(),
        length(2, 20)
    ]),
    lastName: every([
        required(),
        length(2, 20)
    ]),
    birthYear: range(1900, 2018)
};

// Create a person
const person = {
    firstName: 'Stanford',
    lastName: 'Strickland',
    birthYear: 1925
};

// Validate the person's properties
const personResult = {
    firstName: validate(personValidator.firstName, person.firstName),
    lastName: validate(personValidator.lastName, person.lastName),
    birthYear: validate(personValidator.birthYear, person.birthYear)
};

With this example, we have very primitive object property validation. The personResult output includes the validation results for each property, but there isn't anything providing a top-level isValid prop on the results. Let's add that in.

// Validate the person's properties
const personResult = {
    firstName: validate(rules.firstName, person.firstName),
    lastName: validate(rules.lastName, person.lastName),
    birthYear: validate(rules.birthYear, person.birthYear)
};

// Create a top-level result including the results from personResult
const result = {
    personResult,
    isValid: (
        personResult.firstName.isValid &&
        personResult.lastName.isValid &&
        personResult.birthYear.isValid
    ),
    value: person
};

The top-level result also includes the value to be consistent with the output of other validators.

At this point, we can see a pattern where we would want a validator to iterate over the properties that have validators, validate each of those properties, and compose a final validation result for all props. Indeed, Strickland has such a validator built-in called objectProps.

PreviousarrayElementsNextobjectProps

Last updated 4 years ago

Was this helpful?