Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

JavaScript library for data type & data structure validations.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 docs
Octocat-spinner-32 tests
Octocat-spinner-32 .gitignore
Octocat-spinner-32 History.md
Octocat-spinner-32 README.md
Octocat-spinner-32 core.js
Octocat-spinner-32 guards.js
Octocat-spinner-32 package.json
README.md

guards

JavaScript library for data type & data structure validations providing a runtime analog of types. Check out docs for more details.

Install

npm install guards

Require

// Full version inculding String / Number / Function guards.
var guards = require('https!raw.github.com/Gozala/guards/v0.2.0/guards.js')

// Core version without dependencies and type specific guards.
var guards = require('https!raw.github.com/Gozala/guards/v0.2.0/core.js')

Example

var guards = require('https!raw.github.com/Gozala/guards/v0.2.0/guards.js')
var Point = guards.Schema({
  x: guards.Number(0),
  y: guards.Number(0)
})

function color(value) {
  if (typeof value === "number" && value <= 255 && value >= 0) return value
  throw new TypeError("Color is a number between 0 and 255")
}

var RGB = guards.Tuple([ color, color, color ])
var Segment = guards.Schema({
  start: Point,
  end: Point,
  color: RGB,
})

var segment = Segment({ end: { y: 23 }, color: [17, 255, 0] })
// { start: { x: 0, y: 0 }, end: { x: 0, y: 23 }, color: [ 17, 255, 0 ] }

Prior art

Something went wrong with that request. Please try again.