lib/type/typedef.js
function validate(key, values) {
  if(this.arity > 0 && values.length !== this.arity) {
    throw new TypeDefError(
      util.format('bad arity for \'%s\', expected %s got %s',
        key, this.arity, values.length));
  }
  var types = Array.isArray(this.type) ? this.type : [this.type]
    , i
    , type
    , val
    , num
    , coerced = []
    , enumerable = false;

  for(i = 0;i < types.length;i++) {
    type = types[i];
    val = values[i];
    enumerable = Array.isArray(type);
    if(enumerable) {
      if(!~type.indexOf(val)) {
        throw new TypeDefError(
          util.format('enum error on key \'%s\', got \'%s\' expected one of %s',
            key, val, type.join(', ')));
      }
    }
    switch(type) {
      case ByteSize:
        val = ByteSize.parse(val);
        break;
      case Boolean:
        if(val !== YES && val !== NO) {
          throw new Error(
            util.format('argument must be \'%s\' or \'%s\'', YES, NO));
        }
        val = val === YES ? true : false;
        break;
      case String:
        // strings can be quoted
        val = val.replace(/^"/, '').replace(/"$/, '');
        break;
      case Number:
        num = parseInt(val);
        if(isNaN(num) || !/^-?[0-9]+$/.test(val)) {
          throw new TypeDefError(
            util.format('number expected for \'%s\', got %s', key, val));
        }
        val = num;
        break;
    }

    coerced[i] = val;
  }
  if(this.arity === 1 && !this.repeats) return coerced[0];
  return Array.isArray(this.type) ? coerced : coerced[0];
}
function validate(key, values) {
  if(this.arity > 0 && values.length !== this.arity) {
    throw new TypeDefError(
      util.format('bad arity for \'%s\', expected %s got %s',
        key, this.arity, values.length));
  }
  var types = Array.isArray(this.type) ? this.type : [this.type]
    , i
    , type
    , val
    , num
    , coerced = []
    , enumerable = false;

  for(i = 0;i < types.length;i++) {
    type = types[i];
    val = values[i];
    enumerable = Array.isArray(type);
    if(enumerable) {
      if(!~type.indexOf(val)) {
        throw new TypeDefError(
          util.format('enum error on key \'%s\', got \'%s\' expected one of %s',
            key, val, type.join(', ')));
      }
    }
    switch(type) {
      case ByteSize:
        val = ByteSize.parse(val);
        break;
      case Boolean:
        if(val !== YES && val !== NO) {
          throw new Error(
            util.format('argument must be \'%s\' or \'%s\'', YES, NO));
        }
        val = val === YES ? true : false;
        break;
      case String:
        // strings can be quoted
        val = val.replace(/^"/, '').replace(/"$/, '');
        break;
      case Number:
        num = parseInt(val);
        if(isNaN(num) || !/^-?[0-9]+$/.test(val)) {
          throw new TypeDefError(
            util.format('number expected for \'%s\', got %s', key, val));
        }
        val = num;
        break;
    }

    coerced[i] = val;
  }
  if(this.arity === 1 && !this.repeats) return coerced[0];
  return Array.isArray(this.type) ? coerced : coerced[0];
}
function validate ( key , values ) { if ( this . arity > 0 && values . length !== this . arity ) { throw new TypeDefError ( util . format ( 'bad arity for \'%s\', expected %s got %s' , key , this . arity , values . length ) ) ; } var types = Array . isArray ( this . type ) ? this . type : [ this . type ] , i , type , val , num , coerced = [ ] , enumerable = false ; for ( i = 0 ; i < types . length ; i ++ ) { type = types [ i ] ; val = values [ i ] ; enumerable = Array . isArray ( type ) ; if ( enumerable ) { if ( ! ~ type . indexOf ( val ) ) { throw new TypeDefError ( util . format ( 'enum error on key \'%s\', got \'%s\' expected one of %s' , key , val , type . join ( ', ' ) ) ) ; } } switch ( type ) { case ByteSize : val = ByteSize . parse ( val ) ; break ; case Boolean : if ( val !== YES && val !== NO ) { throw new Error ( util . format ( 'argument must be \'%s\' or \'%s\'' , YES , NO ) ) ; } val = val === YES ? true : false ; break ; case String : // strings can be quoted val = val . replace ( / ^" / , '' ) . replace ( / "$ / , '' ) ; break ; case Number : num = parseInt ( val ) ; if ( isNaN ( num ) || ! / ^-?[0-9]+$ / . test ( val ) ) { throw new TypeDefError ( util . format ( 'number expected for \'%s\', got %s' , key , val ) ) ; } val = num ; break ; } coerced [ i ] = val ; } if ( this . arity === 1 && ! this . repeats ) return coerced [ 0 ] ; return Array . isArray ( this . type ) ? coerced : coerced [ 0 ] ; }
Validate an array of values against this type
definition.
Validate an array of values against this type definition .