StringSchema

Convenience methods for creating string schemas.

Extends

Methods

digits()

Require a string to only be digits [0-9].
Examples
const schema = vdn.string().digits()

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

Using data:

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

email()

Require a valid email address.
Examples
const schema = vdn.string().email()

vdn.attempt('a@b.com', schema) // Result == 'a@b.com' (valid)
vdn.attempt('b.com', schema)   // Throws ValidationError

Using data:

const schema = {
  type: 'string',
  email: true
}

hex()

Require a hexadecimal string.
Examples
const schema = vdn.string().hex()

vdn.attempt('0fE', schema)   // Result == '0fE' (valid)
vdn.attempt('ghi', schema)   // Throws ValidationError

Using data:

const schema = {
  type: 'string',
  hex: true
}

length(length)

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

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

Using data:

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

maxLength(length)

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

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

Using data:

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

minLength(length)

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

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

Using data:

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

notEmpty()

Require a string to not be empty.

Note this still allows the value 'undefined', see AnySchema#required.

Examples
const schema = vdn.string().notEmpty()

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

Using data:

const schema = {
  type: 'string',
  notEmpty: true
}