Options
All
  • Public
  • Public/Protected
  • All
Menu

Superstruct API Reference

Index

Classes

Interfaces

Type aliases

Variables

Functions

Object literals

Type aliases

Branch

Branch: Array<any>

Branch arrays contain each value following a path down from the root.

[root, ..., parent, value]

Failure

Failure: object

Failure objects represent a specific failure in validation. They are plain objects that can be turned into real StructError when needed.

{
  type: 'number',
  value: 'invalid',
  path: [1],
  branch: [
    [1, 'invalid', 2],
    'invalid',
  ]
}

Type declaration

  • [key: string]: any

    Failures can also be augmented with any of your on custom properties.

  • branch: Branch

    The branch of values following a path down from the root.

  • path: Path

    The path of indices to retrieve the failing value from the root.

  • type: string | undefined

    The expected type description of the failing value, or undefined if it didn't have an expected type.

  • value: any

    The failing value.

Path

Path: Array<number | string>

Path arrays specify a nested value's location in a root object or array.

['user', 'address', 'city']
['nodes', 1, 'nodes', 0, 'text']

Validator

Validator: function

Validator functions allow developers to define their own scalar types for Superstruct to validate against, and return an indication of what is invalid.

import { superstruct } from 'superstruct'
import isEmail from 'is-email'

const struct = superstruct({
  types: {
    email: value => isEmail(value) && value.length < 256,
  }
})

Type declaration

Variables

Const struct

struct: Superstruct = superstruct()

The singleton instance of Superstruct that is exported by default, configured with types for all of the JavaScript built-in data types. The singleton instance of Superstruct that is exported by default, configured with types for all of the JavaScript built-in data types.

You can use it if you don't need any custom types. However, if you do want to define custom types, use the superstruct factory to configure your own Superstruct instance.

You can use it if you don't need any custom types. However, if you do want to define custom types, use the superstruct factory to configure your own Superstruct instance.

Functions

Const isStruct

  • isStruct(value: any): boolean
  • Check if a value is a Struct object.

    Parameters

    • value: any

    Returns boolean

Const superstruct

  • superstruct(settings?: Partial<SuperstructSettings>): Superstruct
  • Create a struct singleton with settings that include your own domain-specific data types, and an optional custom Error class.

    Parameters

    • Default value settings: Partial<SuperstructSettings> = {}

    Returns Superstruct

Object literals

Const Types

Types: object

Superstruct ships by default with an unopinionated set of scalar types that express all of the data types that are built-in to JavaScript.

any

  • any(value: any): boolean
  • Matches any value other than undefined.

    'anything'
    true

    Parameters

    • value: any

    Returns boolean

arguments

  • arguments(value: any): boolean
  • Matches an arguments object.

    arguments

    Parameters

    • value: any

    Returns boolean

array

  • array(value: any): boolean
  • Matches an Array.

    [1, 2, 3]

    Parameters

    • value: any

    Returns boolean

boolean

  • boolean(value: any): boolean
  • Matches a boolean.

    true
    false

    Parameters

    • value: any

    Returns boolean

buffer

  • buffer(value: any): boolean
  • Matches a Node.js Buffer.

    Buffer.from('string')

    Parameters

    • value: any

    Returns boolean

date

  • date(value: any): boolean
  • Matches a valid Date object.

    new Date()

    Note: Invalid Date objects that equal NaN are not matched.

    Parameters

    • value: any

    Returns boolean

error

  • error(value: any): boolean
  • Matches an error object.

    new Error()

    Parameters

    • value: any

    Returns boolean

float32array

  • float32array(value: any): boolean
  • Matches a Float32Array object.

    Parameters

    • value: any

    Returns boolean

float64array

  • float64array(value: any): boolean
  • Matches a Float64Array object.

    Parameters

    • value: any

    Returns boolean

function

  • function(value: any): boolean
  • Matches a function.

    () => {}
    function () {}

    Parameters

    • value: any

    Returns boolean

generatorfunction

  • generatorfunction(value: any): boolean
  • Matches a generator function.

    function* () {}

    Parameters

    • value: any

    Returns boolean

int16array

  • int16array(value: any): boolean
  • Matches a Int16Array object.

    Parameters

    • value: any

    Returns boolean

int32array

  • int32array(value: any): boolean
  • Matches a Int32Array object.

    Parameters

    • value: any

    Returns boolean

int8array

  • int8array(value: any): boolean
  • Matches a Int8Array object.

    Parameters

    • value: any

    Returns boolean

map

  • map(value: any): boolean
  • Matches a Map object.

    new Map()

    Parameters

    • value: any

    Returns boolean

null

  • null(value: any): boolean
  • Matches the null literal value.

    null

    Parameters

    • value: any

    Returns boolean

number

  • number(value: any): boolean
  • Matches a number.

    42

    Parameters

    • value: any

    Returns boolean

object

  • object(value: any): boolean
  • Matches a plain object.

    { key: 'value' }
    { something: true }

    Parameters

    • value: any

    Returns boolean

promise

  • promise(value: any): boolean
  • Matches a Promise object.

    Promise.resolve()

    Parameters

    • value: any

    Returns boolean

regexp

  • regexp(value: any): boolean
  • Matches a regular expression object.

    /a-z/g

    Parameters

    • value: any

    Returns boolean

set

  • set(value: any): boolean
  • Matches a Set object.

    new Set()

    Parameters

    • value: any

    Returns boolean

string

  • string(value: any): boolean
  • Matches a string.

    'text'

    Parameters

    • value: any

    Returns boolean

symbol

  • symbol(value: any): boolean
  • Matches a Symbol.

    Symbol()

    Parameters

    • value: any

    Returns boolean

uint16array

  • uint16array(value: any): boolean
  • Matches a Uint16Array object.

    Parameters

    • value: any

    Returns boolean

uint32array

  • uint32array(value: any): boolean
  • Matches a Uint32Array object.

    Parameters

    • value: any

    Returns boolean

uint8array

  • uint8array(value: any): boolean
  • Matches a Uint8Array object.

    Parameters

    • value: any

    Returns boolean

uint8clampedarray

  • uint8clampedarray(value: any): boolean
  • Matches a Uint8ClampedArray object.

    Parameters

    • value: any

    Returns boolean

undefined

  • undefined(value: any): boolean
  • Matches the undefined literal value.

    undefined

    Parameters

    • value: any

    Returns boolean

weakmap

  • weakmap(value: any): boolean
  • Matches a WeakMap object.

    new WeakMap()

    Parameters

    • value: any

    Returns boolean

weakset

  • weakset(value: any): boolean
  • Matches a WeakSet object.

    new WeakSet()

    Parameters

    • value: any

    Returns boolean