Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Struct

Struct validators encapsulate the validation logic for a specific type of data (either custom or built-in). They have a set of methods that allow you to validate input in various ways, while producing detailed errors.

They are created by the Superstruct factory functions. You can call them directly for the simple case, or use one of their validation methods.

const Struct = struct({
  id: 'number',
  name: 'string',
})

const result = Struct(data) // Throws if invalid!

const [error, result] = Struct.validate(data)

if (Struct.test(data)) {
   // ...
}

Hierarchy

  • Struct

Callable

  • __call(value: any): any
  • All structs are functions that are shorthand for calling Struct.assert.

    They are created by the Superstruct factory functions. You can call them directly for the simple case, or use one of their validation methods.

    const Struct = struct({
      id: 'number',
      name: 'string',
    })
    
    const result = Struct(data) // Throws if invalid!
    
    const [error, result] = Struct.validate(data)
    
    if (Struct.test(data)) {
       // ...
    }

    Parameters

    • value: any

    Returns any

Index

Properties

Methods

Properties

kind

kind: string

The struct's name.

'object'
'union'
'email'

type

type: string

A string representing the type of the struct. These strings are purely for user-facing error messages, and aren't canonical. They are similar to the syntax that TypeScript uses.

'{id,name,email}'
'string | number'
'email'

Methods

assert

  • assert(value: any): any
  • Validate a value, returning the resulting value, and throwing an error if validation fails.

    try {
      const result = Struct.assert(value)
      // ...
    } catch (e) {
      // ...
    }

    Parameters

    • value: any

    Returns any

check

  • check(value: any, branch: Branch, path: Path): [undefined | object[], any]
  • Run the low-level validation function a struct, returning a tuple that contains either a list of Failure objects, or a resulting value.

    This method is fairly low-level and not for normal use.

    const [failures, result] = Struct.check(value, branch, path)

    Parameters

    Returns [undefined | object[], any]

default

  • default(): any
  • Get the default value for a struct.

    const defaults = Struct.default()

    Returns any

fail

  • Create a low-level Failure object for the struct.

    const failure = Struct.fail({ value, branch, path })

    Parameters

    • obj: object
      • branch: Branch
      • path: Path
      • Optional type?: undefined | string
      • value: any

    Returns Failure

test

  • test(value: any): boolean
  • Validate a value, returning a boolean indicating whether it's valid.

    Note: Using this method does not give you access to the defaults that may be associated with a struct, so it doesn't guarantee that the value you have passes, just that the value with defaults passes.

    if (Struct.test(value)) {
      // ...
    }

    Parameters

    • value: any

    Returns boolean

validate

  • validate(value: any): [undefined | Error, any]
  • Validate a value returning a tuple containing an error if the validation fails, or the resulting value if it succeeds.

    const [error, result] = Struct.validate(value)

    Parameters

    • value: any

    Returns [undefined | Error, any]