lib/checkers.js
function checkParameter(validator, def, context) {
  var value = match.fromContext(def.name, def.in, context);

  // Check requirement
  if(def.required && !value) {
    throw new ValidationError(def.name + " is required");
  } else if(!value) {
    return def.default;
  }

  // Select the right schema according to the spec
  var schema;
  if(def.in === "body") {
    schema = def.schema;
    // TODO: clean and sanitize recursively
  } else {
    // TODO: coerce other types
    if(def.type === "integer") {
      value = parseInt(value);
    } else if(def.type === "number") {
      value = parseFloat(value);
    } else if(def.type === "file") {
      def.type = "object";
    }
    if (def.in === "query" && def.type === "array") {
      if(!def.collectionFormat || def.collectionFormat === "csv") {
        value = value.split(",");
      } else if (def.collectionFormat === "tsv") {
        value = value.split("\t");
      } else if (def.collectionFormat === "ssv") {
        value = value.split(" ");
      } else if (def.collectionFormat === "psv") {
        value = value.split("|");
      } else if (def.collectionFormat === "multi") {
        throw new ValidationError("multi collectionFormat query parameters currently unsupported");
      } else {
        throw new ValidationError("unknown collectionFormat " + def.collectionFormat);
      }
    }

    schema = def;
  }

  var err = validator(value, schema);
  if(err.length > 0) {
    throw new ValidationError(def.name + " has an invalid format: " +
      JSON.stringify(err));
  }

  return value;
}
function checkParameter(validator, def, context) {
  var value = match.fromContext(def.name, def.in, context);

  // Check requirement
  if(def.required && !value) {
    throw new ValidationError(def.name + " is required");
  } else if(!value) {
    return def.default;
  }

  // Select the right schema according to the spec
  var schema;
  if(def.in === "body") {
    schema = def.schema;
    // TODO: clean and sanitize recursively
  } else {
    // TODO: coerce other types
    if(def.type === "integer") {
      value = parseInt(value);
    } else if(def.type === "number") {
      value = parseFloat(value);
    } else if(def.type === "file") {
      def.type = "object";
    }
    if (def.in === "query" && def.type === "array") {
      if(!def.collectionFormat || def.collectionFormat === "csv") {
        value = value.split(",");
      } else if (def.collectionFormat === "tsv") {
        value = value.split("\t");
      } else if (def.collectionFormat === "ssv") {
        value = value.split(" ");
      } else if (def.collectionFormat === "psv") {
        value = value.split("|");
      } else if (def.collectionFormat === "multi") {
        throw new ValidationError("multi collectionFormat query parameters currently unsupported");
      } else {
        throw new ValidationError("unknown collectionFormat " + def.collectionFormat);
      }
    }

    schema = def;
  }

  var err = validator(value, schema);
  if(err.length > 0) {
    throw new ValidationError(def.name + " has an invalid format: " +
      JSON.stringify(err));
  }

  return value;
}
function checkParameter ( validator , def , context ) { var value = match . fromContext ( def . name , def . in , context ) ; // Check requirement if ( def . required && ! value ) { throw new ValidationError ( def . name + " is required" ) ; } else if ( ! value ) { return def . default ; } // Select the right schema according to the spec var schema ; if ( def . in === "body" ) { schema = def . schema ; // TODO: clean and sanitize recursively } else { // TODO: coerce other types if ( def . type === "integer" ) { value = parseInt ( value ) ; } else if ( def . type === "number" ) { value = parseFloat ( value ) ; } else if ( def . type === "file" ) { def . type = "object" ; } if ( def . in === "query" && def . type === "array" ) { if ( ! def . collectionFormat || def . collectionFormat === "csv" ) { value = value . split ( "," ) ; } else if ( def . collectionFormat === "tsv" ) { value = value . split ( "\t" ) ; } else if ( def . collectionFormat === "ssv" ) { value = value . split ( " " ) ; } else if ( def . collectionFormat === "psv" ) { value = value . split ( "|" ) ; } else if ( def . collectionFormat === "multi" ) { throw new ValidationError ( "multi collectionFormat query parameters currently unsupported" ) ; } else { throw new ValidationError ( "unknown collectionFormat " + def . collectionFormat ) ; } } schema = def ; } var err = validator ( value , schema ) ; if ( err . length > 0 ) { throw new ValidationError ( def . name + " has an invalid format: " + JSON . stringify ( err ) ) ; } return value ; }
Check if the context carries the parameter correctly
@param validator {function(object, Schema)} JSON-Schema validator function
@param def {Parameter} The parameter's definition
@param context {Context} A koa context
@return {*} The cleaned value
@throws {ValidationError} A possible validation error
Check if the context carries the parameter correctly