AnySchema

Convenience methods for creating any value schemas.

Methods

default(deFault)

Set the default value to use when a value is 'undefined'.
Parameters:
Name Type Description
deFault * The default value.
Examples
const schema = vdn.any().default(42)

vdn.attempt('', schema)        // Result == '' (already valid)
vdn.attempt(undefined, schema) // Result == 42 (use default)

Using data:

const schema = {
  type: 'any',
  default: 42
}

invalid(values)

Declare an array of values that are invalid.
Parameters:
Name Type Description
values * The array of invalid values.
Examples
const schema = vdn.any().invalid(['a string', 5, 6.3])

vdn.attempt(7, schema)          // Result == 7 (valid)
vdn.attempt('a string', schema) // Throws ValidationError

Using data:

const schema = {
  type: 'any',
  invalid: ['a string', 5, 6.3]
}

required(requiredopt)

Require a value to exist. The default is to allow the value 'undefined'.
Parameters:
Name Type Attributes Default Description
required boolean <optional>
true 'true' if the value is required. Otherwise 'false'.
Examples
const schema = vdn.any().required()

vdn.attempt('', schema)        // Result == '' (valid)
vdn.attempt(undefined, schema) // Throws ValidationError

Using data:

const schema = {
  type: 'any',
  required: true
}

toObject() → {Object}

Return this schema as a vanilla javascript object.
Returns:
Type
Object
Example
vdn.any().required(false).toObject()

// Returns
{
  type: 'any',
  required: false
}

valid(values)

Declare an array of values that are valid.
Parameters:
Name Type Description
values * The array of valid values.
Examples
const schema = vdn.any().valid(['a string', 5, 6.3])

vdn.attempt(5, schema)          // Result == 5 (valid)
vdn.attempt('nope', schema)     // Throws ValidationError

Using data:

const schema = {
  type: 'any',
  valid: ['a string', 5, 6.3]
}