express-gateway

Express Gateway: add params with Request-Transformer policy, but under condition


In my Node system, I have a server exposing some API, accessible through an Express Gateway. I would like to force the add of some params in the req.body, according to the App that is using my API.

To do this, I use personalized scope value. For instance, I have App1 and App2 (with their credentials) using my API. So, in eg credentials, I add scope app1 to App1 credentials, and app2 to App2 credentials' scopes.

In gateway.config.yml file I write:

   ...
      - request-transformer:   
        - condition:
          name: allOf
          conditions:
              -
                name: regexpmatch
                match: ^/user/signup?(.*)$                 
              -
                name: expression
                expression: "apiEndpoint.scopes.indexOf('app1')>=0"
        - action:
          body:
            add:
              custom_field: "'app1'"   
        - condition:
          name: allOf
          conditions:
              -
                name: regexpmatch
                match: ^/user/signup?(.*)$                 
              -
                name: expression
                expression: "apiEndpoint.scopes.indexOf('app2')>=0"
        - action:
          body:
            add:
              custom_field: "'app2'"   

But I get this error:

     error: Policy request-transformer params validation failed: 
data should have required property '.headers', 
data should have required property '.body', 
data should match some schema in anyOf
    (node:17362) UnhandledPromiseRejectionWarning: Error: POLICY_PARAMS_VALIDATION_FAILED

============

Actually watching the JSON transformation, the action was not attribute of request-transformer. Bad indentation.


Solution

  • You should pay attention to the yaml indentation, the one you posted is wrong and so the final object that Express Gateway is trying to digest is incorrect.

    I suggest you to use a Yaml2Json Converter, that will definitely help you see the final structure and help you debug the issue.

    Cheers!

    V.