<commit_before>// Template InputValidation.js

// include api.js


var bundle = context.getVariable('bundle');
var target;
var path = context.getVariable("proxy.pathsuffix").replace(/\/$/, '');
var resource = bundle.policify.getResourceForPath(api, path);
print('path', path, 'resource', resource);
var verb = context.getVariable("request.verb").toLowerCase();
var ParameterException = {};
var parameters;

if (resource) {

  if(!('parameters' in api["paths"][resource][verb])) {
      parameters = [];
  } else {
      parameters = api["paths"][resource][verb]["parameters"];
  }

  try {
      // loop through all defined parameters, throw exception when something fails
      parameters.forEach(function(parameter) {
        
          if (!(parameter["in"] === "query" || parameter["in"] === "path")) {
              // we only parse query and path params for now
              return false;
          }

          // Create schema from parameter definition
          var pSchema = bundle.policify.paramToSchema(parameter);
          var pathParamValue = context.getVariable('pathParam.' + parameter.name);
          print(parameter.name, pathParamValue, JSON.stringify(pSchema));

          var wrappedParamValue = {};
          if (pathParamValue) {
            wrappedParamValue[parameter.name] = pathParamValue;
          } else {
            wrappedParamValue[parameter.name] = context.proxyRequest.queryParams[parameter.name][0] || '';
          }

          // Validate against the schema.
          var isValid = bundle.policify.validateSchema(wrappedParamValue, pSchema);
          print('valid?', isValid, JSON.stringify(wrappedParamValue));
          if (isValid) {
            print("Parameter value is conform the parameter schema");
          } else {
            var err = bundle.policify.getLastError();
            print("Parameter does not validate with the parameter schema");
            print("Error " + JSON.stringify(err));
            throw ParameterException;
          }


          // check if the parameter is required
          // if("required" in parameter && parameter['required'] === true && !(parameter['name'] in context.proxyRequest.queryParams)) {
          //     print("Parameter was required, but not found");
          //     throw ParameterException;
          // }
          //
          // // check if the param is set
          // if(!(parameter['name'] in context.proxyRequest.queryParams)) {
          //     print("Parameter was not required, and also not set, skipping");
          //
          //     // no further processing needed, param was not set
          //     return false
          // }
          //
          // // check if the param type is correct
          // print("Value is: " + context.proxyRequest.queryParams[parameter.name]);
          // print("Checking parameter type, should be: " + parameter.type + ", is: " + typeof(context.proxyRequest.queryParams[parameter.name]));
          // switch(parameter.type) {
          //     case "integer":
          //         if(typeof(context.proxyRequest.queryParams[parameter.name]) !== "number") {
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case "null":
          //         // null is of type 'object':
          //         // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/typeof
          //         if(typeof(context.proxyRequest.queryParams[parameter.name]) !== 'object') {
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     // is this even possible?
          //     case "array":
          //         if(!context.proxyRequest.queryParams[parameter.name].isArray()) {
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case "string":
          //         // apigee casts these into a list, see:
          //         if(typeof(context.proxyRequest.queryParams[parameter.name][0]) !== parameter.type) {
          //             print("Expected " + parameter.type + ", got: " + typeof(context.proxyRequest.queryParams[parameter.name]))
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case "object":
          //     case "boolean":
          //     case "number":
          //         if(typeof(context.proxyRequest.queryParams[parameter.name]) !== parameter.type) {
          //             print("Expected " + parameter.type + ", got: " + typeof(context.proxyRequest.queryParams[parameter.name]))
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case 'default':
          //         // this is not a valid json schema type and/or covered by this parser
          //         print(parameter.name + " is not a valid json schema type");
          //         throw ParameterException;
          //     }
          // print("Parameter type is valid");
          //
          //
          // // if a pattern regex was set, check the input
          // if("pattern" in parameter && parameter["pattern"] !== "") {
          //     print("VALIDATE REGEX", context.proxyRequest.queryParams[parameter.name]);
          //     if (!checkRegex(context.proxyRequest.queryParams[parameter.name], parameter["pattern"])) {
          //         print("Regex check failed", parameter["pattern"]);
          //         throw ParameterException;
          //     }
          // }

      });
  } catch(e) {
      if(e !== ParameterException) {
          throw e;
      } else {
          // we got an error, let Apigee figure it out
          context.setVariable('INPUT.error', true);
      }
  }
} else {
  print('resource could not be found');
  context.setVariable('INPUT.error', true);
}
<commit_msg>use request.queryparam.{parameter name}<commit_after>// Template InputValidation.js

// include api.js


var bundle = context.getVariable('bundle');
var target;
var path = context.getVariable("proxy.pathsuffix").replace(/\/$/, '');
var resource = bundle.policify.getResourceForPath(api, path);
print('path', path, 'resource', resource);
var verb = context.getVariable("request.verb").toLowerCase();
var ParameterException = {};
var parameters;

if (resource) {

  if(!('parameters' in api["paths"][resource][verb])) {
      parameters = [];
  } else {
      parameters = api["paths"][resource][verb]["parameters"];
  }

  try {
      // loop through all defined parameters, throw exception when something fails
      parameters.forEach(function(parameter) {

          if (!(parameter["in"] === "query" || parameter["in"] === "path")) {
              // we only parse query and path params for now
              return false;
          }

          // Create schema from parameter definition
          var pSchema = bundle.policify.paramToSchema(parameter);
          var pathParamValue = context.getVariable('pathParam.' + parameter.name);
          print(parameter.name, pathParamValue, JSON.stringify(pSchema));

          var wrappedParamValue = {};
          if (pathParamValue) {
            wrappedParamValue[parameter.name] = pathParamValue;
          } else {
            wrappedParamValue[parameter.name] = context.getVariable('request.queryparam.'+[parameter.name])|| '';
          }

          // Validate against the schema.
          var isValid = bundle.policify.validateSchema(wrappedParamValue, pSchema);
          print('valid?', isValid, JSON.stringify(wrappedParamValue));
          if (isValid) {
            print("Parameter value is conform the parameter schema");
          } else {
            var err = bundle.policify.getLastError();
            print("Parameter does not validate with the parameter schema");
            print("Error " + JSON.stringify(err));
            throw ParameterException;
          }


          // check if the parameter is required
          // if("required" in parameter && parameter['required'] === true && !(parameter['name'] in context.proxyRequest.queryParams)) {
          //     print("Parameter was required, but not found");
          //     throw ParameterException;
          // }
          //
          // // check if the param is set
          // if(!(parameter['name'] in context.proxyRequest.queryParams)) {
          //     print("Parameter was not required, and also not set, skipping");
          //
          //     // no further processing needed, param was not set
          //     return false
          // }
          //
          // // check if the param type is correct
          // print("Value is: " + context.proxyRequest.queryParams[parameter.name]);
          // print("Checking parameter type, should be: " + parameter.type + ", is: " + typeof(context.proxyRequest.queryParams[parameter.name]));
          // switch(parameter.type) {
          //     case "integer":
          //         if(typeof(context.proxyRequest.queryParams[parameter.name]) !== "number") {
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case "null":
          //         // null is of type 'object':
          //         // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/typeof
          //         if(typeof(context.proxyRequest.queryParams[parameter.name]) !== 'object') {
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     // is this even possible?
          //     case "array":
          //         if(!context.proxyRequest.queryParams[parameter.name].isArray()) {
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case "string":
          //         // apigee casts these into a list, see:
          //         if(typeof(context.proxyRequest.queryParams[parameter.name][0]) !== parameter.type) {
          //             print("Expected " + parameter.type + ", got: " + typeof(context.proxyRequest.queryParams[parameter.name]))
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case "object":
          //     case "boolean":
          //     case "number":
          //         if(typeof(context.proxyRequest.queryParams[parameter.name]) !== parameter.type) {
          //             print("Expected " + parameter.type + ", got: " + typeof(context.proxyRequest.queryParams[parameter.name]))
          //             throw ParameterException;
          //         }
          //         break;
          //
          //     case 'default':
          //         // this is not a valid json schema type and/or covered by this parser
          //         print(parameter.name + " is not a valid json schema type");
          //         throw ParameterException;
          //     }
          // print("Parameter type is valid");
          //
          //
          // // if a pattern regex was set, check the input
          // if("pattern" in parameter && parameter["pattern"] !== "") {
          //     print("VALIDATE REGEX", context.proxyRequest.queryParams[parameter.name]);
          //     if (!checkRegex(context.proxyRequest.queryParams[parameter.name], parameter["pattern"])) {
          //         print("Regex check failed", parameter["pattern"]);
          //         throw ParameterException;
          //     }
          // }

      });
  } catch(e) {
      if(e !== ParameterException) {
          throw e;
      } else {
          // we got an error, let Apigee figure it out
          context.setVariable('INPUT.error', true);
      }
  }
} else {
  print('resource could not be found');
  context.setVariable('INPUT.error', true);
}

{'fraction_non_alphanumeric': 0.1344072489302794, 'fraction_numerical': 0.0002516989680342311, 'mean_word_length': 1.6518353726362625, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3008526', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2884, 'n_words': 674}