context.swagger.readAsJSON(function (error, schema) {
if (error) {
console.error("error reading swagger: " + error);
} else {
console.log(Number.isInteger(1));
schema.paths[context.get("api.operation.path")].parameters.forEach((x) => {
console.log(x);
switch (x.type) {
//maybe add try catch to fill body as wanted
case "float":
case "double":
if (
isNumeric(context.get(`request.parameters.${x.name}.values`)[0])
) {
context.set("message.body", "true");
}else{
throw new RangeError("Parameter" + x.name + " is not of the type " + x.type);
}
case "integer":
if (
isInteger(context.get(`request.parameters.${x.name}.values`)[0])
) {
context.set("message.body", "true");
}else{
throw new RangeError("Parameter" + x.name + " is not of the type " + x.type);
}
//set error
//break apic assembly
break;
case "boolean":
if (
isInteger(context.get(`request.parameters.${x.name}.values`)[0])
) {
context.set("message.body", "true");
}else{
throw new RangeError("Parameter" + x.name + " is not of the type " + x.type);
}
//set error
//break apic assembly
break;
}
});
}
});
function isNumeric(str) {
if (typeof str != "string") return false; // we only process strings!
return (
!isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
!isNaN(parseFloat(str))
); // ...and ensure strings of whitespace fail
}
function isInteger(str) {
var n = Math.floor(Number(str));
return n !== Infinity && String(n) === str && n >= 0;
}
function isBoolean (str){
return str === "true"
}
------------------------------
Tom van Oppens
------------------------------
Original Message:
Sent: Wed May 31, 2023 04:29 AM
From: Tom van Oppens
Subject: validating path and query parameters
A customer of mine wanted to validate the path and query parameter format based on the swagger definition. In order to achieve this I wrote a little code (that doesn't validate all data types yet but can be easiliy extended)
The requirements are as follows
- read swagger of API definition (this means we always need to parse the swagger which does have a performance impact)
- have the same piece of code that can be wrapped in a user defined policy easiliy for all APIs
------------------------------
Tom van Oppens
------------------------------