Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Clean parameters and define defaults.
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore
.travis.yml
Makefile
README.md
index.js
package.json

README.md

caretaker

Clean parameters and define defaults.

Build Status

Installation

npm install caretaker
var caretaker = require('caretaker')
var cleaned = caretaker.clean(valid, params)

Options

Valid is an object containing keys with an options object. Each parameter is parsed by either opt.type field or typeof opt.default (supports string, number, boolean, or 'default'). If a parameter is undefined, opt.default will be used, otherwise not included in the cleaned object. Also available are opt.required and opt.match for strings.

The caretaker.clean() function returns an cleaned object with a status. If cleaned.status === 'ok', cleaned.params contains the resulting parameters. Otherwise cleaned.errors will contain errors genereated by either opts.required, opts.match or a non-parsing value.

var valid = {
  num: { default: 10 },
  str: { type: 'string', match: /^([a-z]{1,})$/ },
  boo: { type: 'boolean', required: true }
}

var cleaned = caretaker.clean(valid, { str: 'a', boo: 1 })
// cleaned = { status: 'ok', params: { num: 10, str: 'a', boo: true } }

cleaned = caretaker.clean(valid, { str: '9' })
// cleaned = { status: 'error', errors: { str: 'is not allowed', boo: 'is required' } }
Something went wrong with that request. Please try again.