How is MatchesPath implemented in apigee? how can we implement it in javascript?

ushanj2015
Participant I

i am trying to do similar validation as MachesPath in javascript.

I need to compare proxy.pathsuffix & pattern from variable.

Ex: pathsuffix = /a/123/users

variable= /a/{id}/users

need to compare above values matches then set some boolean value.

1 2 751
2 REPLIES 2

If I were doing this , I would use a Regular Expression. JS includes a RegExp object that allows you to perform pattern matching. The pattern language for RegExp does not use the same syntax as "/a/{id}/users", but something similar.

var r = new RegExp('^/a/([^/]+)/users$');
var s = context.getVariable('proxy.pathsuffix');
var match = s.match(r);
if (match) {
  var id = match[1]; // extract the 1st group
  ...
  context.setVariable('extracted_id', id);
}

For the above callout, if there is a match, then it sets the context variable extracted_id. If there is no match, then that variable is unset. Therefore you could use something like this in a subsequent flow:

<Step>
  <Name>RF-InvalidUrl</Name> 
  <Condition>extracted_id = null</Condition>
</Step>

For more information on regular expressions, and the pattern syntax, you can try regular-expressions.info. For an online regex tester/evaluator, try regexr. I have used that one .

Here are some example expressions:

expression explanation
^/a/([^/]+)/users$ ^ = Beginning of string
/a/ = matches itself
[^/] = any character except slash
([^/]+) = a sequence of 1 or more characters, none of which are slash
/users = matches itself
$ = end of string


^/a/([0-9]+)/users$

[0-9] = any character between 0 and 9
[0-9]+ = 1 or more characters between 0 and 9

everything else is as above.

^/a/([0-9]{5})/users$

[0-9]{5} = a sequence of exactly 5 characters, between 0 and 9

The regular expression language is very flexible; you can match just about anything you like.

ushanj2015
Participant I

thanks for your answer.

But my requirement is slightly different. I need validate path suffix dynamically & put this as common code in sharedflow. cannot add for individual proxy.

I need to store chosen paths in KVM & validate it in proxy pathsuffix. If both matches then set variable.