Built-in Specs

import {spec, symbol, valid, conform} from 'js.spec'

and

Data must conform to every provided spec. For instance to match an even number greater than 1000:

const big_even = spec.and(spec.number, spec.even, function big(nr) {
  return big > 1000;
});

valid(big_even, 1000) // => false
valid(big_even, 1001) // => false
valid(big_even, 1002) // => true

or

Data must conform to at least one provided spec. The order in which they are validated is not defined. conform returns matched branches along with input data.

const big_or_even = spec.or({
  'even': spec.even,
  'big': nr => nr > 1000
})

valid(big_or_even, 999) // => false
valid(big_or_even, 1000) // => true
valid(big_or_even, 1001) // => true

conform(big_or_even, 1000) // => ['even', 1000]
conform(big_or_even, 1001) // => ['big', 1001]

nilable

By default no spec accepts null or undefined as valid input. Wrap your spec in nilable to change this.

const big = spec.and(spec.number, x => x > 1000)
valid(big, null) // => false
valid(big, 1002) // => true

const maybe_big = spec.nilable(big)
valid(maybe_big, null) // => true
valid(maybe_big, 1002) // => true

collOf

Used to define collections with items of the same type. Works with Arrays and Sets. Accepts an option map as optional second parameter.

const {minCount, maxCount} = symbol
const tag_cloud = spec.collOf(spec.string, {
  [minCount]: 1,
  [maxCount]: 10
});

valid(tag_cloud, []) // => false
valid(tag_cloud, ['blog']) // => true
valid(tag_cloud, new Set(['blog'])) // => true
valid(tag_cloud, [1, 2, 3]) // => false
valid(tag_cloud, ) // => false, use spec.nilable(spec.collOf(...))
valid(tag_cloud, [null]) // => false, use spec.nilable(spec.string)

Options

  • symbol.count: Collection must contain exactly n items.
  • symbol.minCount: Collection must contain at least n items.
  • symbol.maxCount: Collection must contain at most n items.

symbol.count takes precedence over the other options.

tuple

Used to define collections with items of possibly different types. Works only with Arrays as order is important. Does not accept options.

// team, goals
const team_score = spec.tuple(spec.string, spec.int)

valid(team_score, ['arsenal', 0]) // => true
valid(team_score, [0, 'arsenal']) // => false

// home team, away team
const match_score = spec.tuple(team_score, team_score)

valid(match_score, [
  ['arsenal', 0],
  ['manchester city', 1]
]) // => true

map

Used to define the shape of maps. By default all keys are required. Use spec.optional key to define optional keys.

const {optional} = symbol
const person = spec.map({
  email: spec.string,
  [optional]: {
    name: spec.string
  }
})

valid(person, { email: 'foo@bar.com' }) // => true
valid(person, { name: 'hans' }) // => false
valid(person, {
  email: 'foo@bar.com',
  name: 'hans'
}) // => true
valid(person, { email: false }) // => false

oneOf

Used to define "one out of these values", like an enum. (It's called oneOf because enum is a reserved word in Javascript.)

const state = oneOf("pending", "ready", "cancelled")

valid(state, "pending") // => true
valid(state, "unknown") // => false

results matching ""

    No results matching ""