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
  2. Validating Objects

Nested Objects

The composition ability for combining validators together on props and objects opens up complex possibilities. Another great example is nested objects.

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

const personValidator = objectProps({
    name: every([required(), length(5, 40)]),
    address: objectProps({
        street: every([required(), objectProps({
            number: every([required(), range(1, 99999)]),
            name: every([required(), length(2, 40)])
        })]),
        city: required(),
        state: every([required(), length(2, 2)])
    })
});

const person = {
    name: 'Marty McFly',
    address: {
        street: {
            number: 9303,
            name: 'Lyon Drive'
        },
        city: 'Hill Valley',
        state: 'CA'
    }
};

const result = validate(personValidator, person);

Objects can be nested without any limits on depth. And any type of validator can be used anywhere in the tree.

PreviousAdvanced Object ValidationNextArrays of Objects

Last updated 4 years ago

Was this helpful?