ArraySchema

Convenience methods for creating array schemas.

Extends

Methods

items(schema)

Require array items to match a given schema.
Parameters:
Name Type Description
schema AnySchema Schema that items must match.
Examples
const schema = vdn.array().items(vdn.string().digits())

vdn.attempt([], schema)    // Result == [] (valid)
vdn.attempt(['4'], schema) // Result == ['4'] (valid)
vdn.attempt(['x'], schema) // Throws ValidationError

Using data:

const schema = {
  type: 'array',
  items: {
    value: {
      type: 'string',
      digits: true
    }
  }
}

length(length)

Require an exact array length.
Parameters:
Name Type Description
length number The length required.
Examples
const schema = vdn.array().length(1)

vdn.attempt([4], schema) // Result == [4] (valid)
vdn.attempt([], schema)  // Throws ValidationError

Using data:

const schema = {
  type: 'array',
  length: 1
}

maxLength(length)

Set a maximum array length.
Parameters:
Name Type Description
length number The maximum length allowed.
Examples
const schema = vdn.array().maxLength(1)

vdn.attempt([], schema)    // Result == [] (valid)
vdn.attempt([4], schema)   // Result == [4] (valid)
vdn.attempt([4,5], schema) // Throws ValidationError

Using data:

const schema = {
  type: 'array',
  maxLength: 1
}

minLength(length)

Set a minimum array length.
Parameters:
Name Type Description
length number The minimum length allowed.
Examples
const schema = vdn.array().minLength(1)

vdn.attempt([4], schema)   // Result == [4] (valid)
vdn.attempt([], schema)    // Throws ValidationError

Using data:

const schema = {
  type: 'array',
  minLength: 1
}