Skip to content

dan-nl/validate-parameters

Repository files navigation

validate-parameters

Build Status Coverage Status

validate parameters

table of contents

installation

install npm dependencies

npm i dan-nl/validate-parameters

api

/**
 * @param {*} param
 * @param {string} [key]
 *
 * @returns {Validate}
 */
function validate( param, key )

use

basic

var validate = require( 'validate-parameters' )

// returns true
validate( 'test string' ).isString()

// returns `test string`
validate( 'test string' ).get().isString()

// returns fallback value `fallback string`
validate( {} ).get().isString() || 'fallback string'

// throws an Error
validate( {} ).required().isString()

// throws an Error stating the name of the parameter
validate( 0, 'mystring' ).required().isString()

object parameters

var validate = require( 'validate-parameters' )

var params = {
  mystring: 'my string',
  mydimmension: {
    mystring: 'my string'
  }
}

// returns true
validate( 'test string' ).isString()

// returns `test string`
validate( 'test string' ).get().isString()

// returns fallback value `fallback string`
validate( {} ).get().isString() || 'fallback string'

// throws an Error
validate( {} ).required().isString()

setters

.required()

sets whether or not the param is required. if required and the validator evaluates to false, an Error will be thrown.

.get()

sets whether or not to return the param value. returns the param value unless the validator evaluates to false in which case false will be returned.

validators

  • isArray
  • isBool
  • isDate
  • isFloat
  • isInt
  • isNull
  • isObject
  • isObjectLiteral
  • isString
  • isValidDate

license

MIT License

About

validate parameters

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •