Forum Discussion

res13's avatar
res13
New Contributor
4 years ago

Standardization: Custom rule which ensures that at least one server url matches a pattern

I am trying to create a custom rule which ensures that there is at least one server url which matches a certain pattern (https://{host}/mypath).

My test API looks like this:

 

 

...
servers:
  - description: SwaggerHub API Auto Mocking
    url: https://myswaggerhub/virts/myorg/myapi/1.0.0
  - url: 'https://{host}/mypath'
    variables:
      host:
        enum:
          - 'myfirsthost:443'
          - 'mysecondhost:443'
        default: 'myfirsthost:443'
...

 

 

I tried the following:

1) Create a JSONPath matching my mandatory URL which must exists. Unfortunately, 'path exists' does not support expressions (see attached image named custom_rule_1.png)

2) Create a JSONPath matching all urls and add a regex to match my mandatory URL. Unfortunately, this also excludes all other URLs (see attached image named custom_rule_2.png)

3) Create a JSONPath matching all urls and add a regex to match my mandatory URL or any other URL. Unfortunately, this does not enforce that my mandatory URL must be present (see attached image named custom_rule_3.png

  • OK, try this, treat servers as an array:

     

    path: $.servers.1.url

    regex: https://{host}/mypath

    - path must exit: false

     

    Problem is, this assumes the "desired path" is always the second URL under the Servers tag. 

     

    Maybe have another rule that $.servers.0.url must exist and NOT be the special path ? 

    Or make sure the "special path" is always the first [zero] URL. The auto-mock doesn't need to be first <grin>.

    • res13's avatar
      res13
      New Contributor

      Thanks, but your solution seems to be too static for my use case.

      My given example was really just an example to simulate the problem, but in reality the APIs can look quite different.

      My only goal is to make sure that the one server url is present an nothing more.

      It just does not feel right, to enforce a certain order of server urls inside all of these APIs just to make one standardization rule work.

      Is there no other solution? Or is there maybe a chance that the 'path exists' feature will support expressions in the future?

      • mhiggins's avatar
        mhiggins
        Staff

        I have created a Feature Request in the SwaggerHub Product Aha board.

         

        SWAG-I-333 Custom Rules linter to support path-exists expressions.