node package manager

Introducing npm Enterprise add-ons. Integrate third-party dev tools into npm…


A tiny validation library

_ = require 'underscore'

fail = (reason) ->
  ok: false, reason: reason

checkObject = (pathPrefix, object, schema) ->
  for key, type of schema

    optional = key[0] == '_'
    if optional
      key = key[1..]

    value = object[key]

    path = if pathPrefix then "#{pathPrefix}.#{key}" else key

    if not optional and not value?
      return fail "#{path} is required"

    if not value?

    if _.isFunction type
      vs =
        Number:  _.isNumber
        String:  _.isString
        Array:   _.isArray
        Boolean: _.isBoolean
        Object:  _.isObject

      if of vs
        if not vs[](value)
          return fail "#{path} is not a #{}"
        if not type value
          return fail "#{path} is invalid"

    if _.isRegExp type
      if not type.test value
        return fail "#{path} does not match #{type}"

    if _.isArray(type) and type.length == 1
      if _.some(value, (v) -> not check v, type[0])
        return fail "#{path} is invalid" # todo: better message

    if _.isArray(type) and type.length > 1
      if value not in type
        return fail "#{path} is invalid" # todo: better message

    if _.isObject type
      c = checkObject path, value, type
      if not c.ok
        return fail c.reason

  return ok: true

check = (object, schema) ->
  checkObject null, object, schema

module.exports = check