JAVA SCRIPT TO VALIDATE INPUTS FOR API THROW 400* ERRORS FROM SAP APIM

 

fault raise 




https://signatov.com/handling-exceptions-when-triggering-the-sap-btp-api/

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);
}


----
https://help.sap.com/docs/sap-api-management/sap-api-management/condition-strings?version=Cloud&ref=signatov.com



Raise Fault policy

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

Path expressions are used for matching URI paths, using "*" to represent a single path element and "**" to represent multiple URI levels. For example:

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

% is treated as an escape character. The pattern %{user%} matches {user} but not user. Conditions can be categorized as follows:
  1. Operators: Wildcard patterns used in conditions.
  2. Relational operands: Evaluates whether a quantity is equal to, greater than, or less than another.
  3. Operands: The value of the conditions is used to determine the overall values.
  4. 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

The following table shows the behavior when operands evaluate to null:

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)

In these cases, the system performs the adaptations shown in the following table.

RHS LHS

Boolean

Integer

LongFloatDoubleStringComparableObject

Boolean

Boolean

Integer

LongFloatDoubleString -

Integer

Integer

Integer

LongFloatDoubleStringComparable-

Long

LongLongLongFloatDoubleStringComparable-

Float

FloatFloatFloatFloatDoubleStringComparable-

Double

DoubleDoubleDoubleDoubleDoubleStringComparable-

String

String

String

StringStringStringStringComparable-

Comparable

Comparable

Comparable

ComparableComparableComparableComparableComparable-

Object

-

-

------

Literals

null, true, and false are the literals available in conditions. For example: request.header.host is null and flow.cachehit is true.






Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024