JAVA SCRIPT TO VALIDATE INPUTS FOR API THROW 400* ERRORS FROM SAP APIM
fault raise
var startDate = context.getVariable("request.header.startDate");
var endDate = context.getVariable("request.header.endDate");
function dateDifferenceInDays(start, end) {
var startDate = new Date(start);
var endDate = new Date(end);
var timeDiff = endDate.getTime() - startDate.getTime();
var diffDays = timeDiff / (1000 * 3600 * 24);
return diffDays;
}
if (startDate && endDate) {
var diff = dateDifferenceInDays(startDate, endDate);
if (diff > 7) {
context.setVariable("validation.isErrorFound", true);
}
}
else{
context.setVariable("validation.isErrorFound", true);
}
Target endpoint route rule execution
Using conditional statements, you can control the target endpoint launched by proxy endpoint configuration. A route rule forwards a request to a particular target endpoint. When more than one target endpoint is available, the route rule is evaluated for its condition. If it is true, the request is forwarded to the named target endpoint. For example, if you want to restrict the service access to specific country, then you can add a route rule which has NONE as the target endpoint and the following condition string: request.queryparam.country = "IN" Or request.queryparam.country = "DE".
For more information on how to define multiple target endpoints using Route Rule, see Enable Dynamic Routing.
Path Expressions
Pattern | Sample URI paths matched |
---|---|
/*/a/ | /x/a/ or /y/a/ |
/*/a/* | /x/a/b or /y/a/foo |
/*/a/** | /x/a/b/c/d |
/*/a/*/feed/ | /x/a/b/feed/ or /y/a/foo/feed/ |
/a/**/feed/** | /a/b/feed/rss/1234 |
- Operators: Wildcard patterns used in conditions.
- Relational operands: Evaluates whether a quantity is equal to, greater than, or less than another.
- Operands: The value of the conditions is used to determine the overall values.
- Literals: Literal values in a condition.
Operators
When using operators, observe the following restrictions:
- Operators cannot be used as variable names.
- A space character is required before and after an operator.
- To include an operator in a variable, a variable name must be enclosed in single quotes. For example, "request.header.help!me".
- Arithmetic operators (+, *, -, /, %) are not supported.
- Java precedence is used for operators.
Symbol | In words (case insensitive) | Description |
---|---|---|
! | Not, not | Unary operator (takes a single input) |
= | Equals, Is | Equals to |
!= | NotEquals, IsNot | Not equals |
:= | EqualsCaseInsensitive | Equals but is case insensitive |
> | GreaterThan | Greater than |
>= | GreaterThanOrEquals | Greater than or equal to |
< | LesserThan | Lesser than |
<= | LesserThanOrEquals | Lesser than or equal to |
&& | And, and | And |
|| | Or | Or |
( | Groups an expression | |
) | Closes an expression group | |
~~ | JavaRegex | Matches a javax.util.regex compliant regular expression. |
~ | Matches, Like | Matches a glob-style pattern using the "*" wildcard character. |
~/ | MatchesPath, LikePath | Matches a path expression. |
=| | StartsWith | Starts with |
Behavior of null operands in conditional statements
Operator | LHS null | RHS null | LHS and RHS null |
---|---|---|---|
=, ==, := | false | false | |
=| | false | false | false |
!= | true | false | |
> | false | false | |
>= | false | ||
< | false | false | |
<= | false | ||
~ | false | N/A | false |
~~ | false | N/A | false |
!~ | false | false | |
~/ | false | N/A | false |
Operands
API Management adapts operands to a common data type before comparing them. For example, if the response status code is 404, the expression response.status.code = "400" and the response.status.code = 400 are equivalent. For numeric operands, the data type is interpreted as integer unless the value is terminated as follows:
- f" or "F" (float, for example, 3.142f, 91.1F)
- "d" or "D" (double, for example, 3.142d, 100.123D)
- "l" or "L" (long, for example, 12321421312L)
RHS LHS | Boolean | Integer | Long | Float | Double | String | Comparable | Object |
---|---|---|---|---|---|---|---|---|
Boolean | Boolean | Integer | Long | Float | Double | String | - | |
Integer | Integer | Integer | Long | Float | Double | String | Comparable | - |
Long | Long | Long | Long | Float | Double | String | Comparable | - |
Float | Float | Float | Float | Float | Double | String | Comparable | - |
Double | Double | Double | Double | Double | Double | String | Comparable | - |
String | String | String | String | String | String | String | Comparable | - |
Comparable | Comparable | Comparable | Comparable | Comparable | Comparable | Comparable | Comparable | - |
Object | - | - | - | - | - | - | - | - |
Literals
null, true, and false are the literals available in conditions. For example: request.header.host is null and flow.cachehit is true.