How to configure dynamic key values in RAML 1.0?

How to configure dynamic key values in RAML 1.0?

While writing your API definition in RAML 1.0, you may have found a scenario in which you need to set different types/formats for values (in a key/value pair), for example...

In the screenshot above, you may find different types for the "sex" key, in which you just separate different types using the pipe "|", but... what if you need to set different types for a key?

What if you don't know the key name, but you know the value?

For example, if you are going to receive a key/value pair in which the key corresponds to an invoice ID, or, maybe you are going to receive a list of addresses like in the example below...

{
    "addresses": [
          {"Beach House": {"address": "Lake 1122", "zipcode": "3344"}}
        , {"New York Appartment": {"address": "Random 1122", "zipcode": "10001"}}
    ]
}        

In this case, we have to use REGEX!

For example, if we set a key as "/^[a-zA-Z0-9_.-]*$/" it will accept any alphanumeric string as key.

Here you have an API Definition in RAML using this approach...

As a summary, you may use regex to configure dynamic key names, but please keep in mind that if you have too many properties using dynamic key names, you may need to find a way for RAML to identify which one is which, to perform proper API definition validation.

Special thanks to Alex Lima , MuleSoft specialist.

Ajaysankar J

Integration Architect

1 年

Germán Corbetta can we use this dynamic property for host name in the api base url in raml which we can expose in the api manager consumer endpoint which changes for every environment the api is published ?

要查看或添加评论,请登录

Germán Corbetta的更多文章

社区洞察

其他会员也浏览了