Four policies change a message in place or enforce its contract. Redact masks values. Remove deletes members. Transform rewrites headers. ValidateAPISpecification rejects a request that does not match the API’s own schema. This post runs each against a real gateway, with the YAML from API Studio code view and the response that came back.

The message post covers Parse, which all the body examples below depend on.

The test project

One API Studio project, one API called nano-shape, one operation per example. There is no backend. The response is either the message after the policy ran, or a set that reads values back.

ExtractIdentity, Authenticate and Authorize check client credentials, then an OperationSwitch sends each operation to its own policies. The body operations run a parse first.

Every call goes to the published product URL with the client credentials of a subscribed application:

curl -s -X POST -H 'Content-Type: application/json' \
  -H "X-IBM-Client-Id: <client-id>" \
  -H "X-IBM-Client-Secret: <client-secret>" \
  -d '{"id":"CUST-77","customer":{"email":"ada@example.com"}}' \
  "https://<product>-<catalog>-<org>.nanogw.<ingress-domain>/1.0.0/nano-shape/redact/body"

The test document for the body examples:

{
  "id": "CUST-77",
  "name": "Ada Lovelace",
  "customer": { "email": "ada@example.com", "phone": "+44 20 7946 0958" },
  "internal": { "score": 812, "segment": "gold" },
  "debug": { "traceId": "abc-123" },
  "lines": [ { "sku": "A-1", "qty": 2 } ]
}

Example 1: redact body fields

Redact overwrites values and keeps the structure. Each capture is a JSONata path relative to the body, and the body must be parsed first:

kind: Redact
spec:
  message:
    messageName: request
    body:
      captures:
        jsonata:
          - $.customer.email
          - $.customer.phone

A set after it returns the body, so the result is visible:

kind: Set
spec:
  valueType: jsonata
  messageBody:
    messageName: response
    value: $body('request')
HTTP/2 200

{"id":"CUST-77","name":"Ada Lovelace","customer":{"email":"*****","phone":"*****"},"internal":{"score":812,"segment":"gold"},"debug":{"traceId":"abc-123"},"lines":[{"sku":"A-1","qty":2}]}

The two values became *****, five asterisks. customer.email still exists. A backend that validates a schema requiring those members will not fail. This is the pattern for a request you want to log or mirror without letting an email address through.

The policy changes the message in place. There is no output message and no variable.

Example 2: redact headers and query parameters

The same policy covers the envelope. headers and queryParameters are lists of names:

kind: Redact
spec:
  message:
    messageName: request
    headers:
      - Authorization
    queryParameters:
      - token

A set reads the values back, the way the next policy or the backend would see them:

kind: Set
spec:
  valueType: jsonata
  messageBody:
    messageName: response
    value: |
      {
        "authPresent": $exists($header('request', 'Authorization')),
        "authValue": $header('request', 'Authorization'),
        "tokenPresent": $exists($queryParameter('request', 'token')),
        "tokenValue": $queryParameter('request', 'token'),
        "tenant": $header('request', 'X-Tenant')
      }
curl ... -X POST -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer secret-token' -H 'X-Tenant: acme' \
  -d '{"note":"x"}' \
  "https://<gateway-host>/1.0.0/nano-shape/redact/headers?token=s3cr3t"
HTTP/2 200

{"authPresent":true,"authValue":"*****","tokenPresent":true,"tokenValue":"%2A%2A%2A%2A%2A","tenant":"acme"}

The header is still there with a masked value. It was not deleted. The masked query parameter is URL-encoded, so ***** reads back as %2A%2A%2A%2A%2A. If you log it, you log the encoded form.

Without ?token=:

{"authPresent":true,"authValue":"*****","tokenPresent":false,"tenant":"acme"}

$exists separates “masked” from “never sent”, which is what you want in an audit log. The Authorization header here is the caller’s, not the one used to authenticate to the gateway. X-Tenant was left alone.

Unlike set, the Redact policy can carry more than one target at once, so message, variable, distributedVariable and telemetry can sit in one policy. I only used message.

Example 3: remove members

Remove deletes members instead of masking them:

kind: Remove
spec:
  message:
    messageName: request
    body:
      captures:
        jsonata:
          - $.internal
          - $.debug
HTTP/2 200

{"id":"CUST-77","name":"Ada Lovelace","customer":{"email":"ada@example.com","phone":"+44 20 7946 0958"},"lines":[{"sku":"A-1","qty":2}]}

internal and debug are gone. The document is smaller, not masked.

Choose between the two like this:

  • Redact when the field must still exist. A consumer validates the schema or a log parser expects the key.
  • Remove when the field must not exist. A secret that must never be stored, or an extension that confuses a strict backend.

Both take JSONata paths rooted at $, not expressions.

What happens when the path is missing? My project notes said both policies fail the request. I sent {"id":"CUST-77"} to both redact/body and remove/body to check:

HTTP/2 200

{"id":"CUST-77"}

Neither failed. A path that does not exist is skipped. Do not rely on the policy to tell you a field was missing.

Example 4: transform a header

Transform rewrites a header with a JSONata expression:

kind: Transform
spec:
  messageHeader:
    messageName: request
    jsonataTransforms:
      - headerNames:
          - X-Tenant
        transform: $uppercase($header('request', 'X-Tenant'))
      - headerNames:
          - X-Request-Id
        transform: $uppercase($header('request', 'X-Request-Id'))
curl ... -X POST -H 'Content-Type: application/json' \
  -H 'X-Tenant: acme' -H 'X-Request-Id: req-9f2c41' -d '{"orderId":"ORD-1001"}' \
  "https://<gateway-host>/1.0.0/nano-shape/transform/header"
HTTP/2 200

{"tenant":"ACME","requestId":"REQ-9F2C41"}

Both headers were upper-cased in place. headerNames is a list, so one expression can cover several headers.

Now the same call with no X-Request-Id:

HTTP/2 200

{"tenant":"ACME"}

No error. The transform for the missing header was skipped and nothing was created. Transform rewrites headers that exist. It does not add one. Use set with action: add for that.

From my project notes, Transform with messageBody also does nothing on this runtime, and the project builds response bodies with set instead. I did not re-run that for this post. messageUriQuery is another target in the reference. I did not test it.

Example 5: validate the request

ValidateAPISpecification enforces the schema in the published OpenAPI document:

kind: ValidateAPISpecification
spec:
  input: request
  validate:
    request:
      schema:
        validate: true

The request body schema for this operation:

type: object
required: [orderId, quantity]
properties:
  orderId:
    type: string
  quantity:
    type: integer
    minimum: 1

A valid request:

curl ... -X POST -H 'Content-Type: application/json' -d '{"orderId":"ORD-1001","quantity":2}' \
  "https://<gateway-host>/1.0.0/nano-shape/validate/request"
HTTP/2 200

{"validated":true,"orderId":"ORD-1001"}

Three bad ones, all rejected:

Body Status
{"orderId":"ORD-1001","quantity":0} (below the minimum) 400
{"orderId":"ORD-1001"} (missing quantity) 400
{"orderId":"ORD-1001","quantity":"two"} (wrong type) 400

All three returned:

HTTP/2 400

Bad Request

The caller is not told which rule failed. My project notes said the body names the violated constraint. It did not on my gateway, so do not promise callers a useful error message.

validate has four forms: request.schema.validate, response.schema.validate, openApiSchemaRef and file. I used the first. openApiSchemaRef and file take a failureStatusCode, so you can return 422. I did not test them.

Validate checks the shape, not the business. quantity: 2 on an out-of-stock item still passes. It also validates against the published schema, so a schema change only counts after you republish.

Summary

  • Redact keeps the field and masks the value. Remove deletes it. Neither complains about a path that is not there.
  • Redacted query parameters read back URL-encoded.
  • Transform changes headers that already exist and ignores the rest.
  • A failed Validate is a plain 400. Log the reason server-side if you need it.

Limitations and Notes

  • Tested on a DataPower Nano Gateway with the API Connect 12.1 documentation set. Check the version you run.
  • Not tested: Redact or Remove on variable, distributedVariable or telemetry, Transform on a query parameter or a body, Validate on a response, openApiSchemaRef, file and failureStatusCode.
  • I did not look at the gateway log, so I cannot say what it records for a failed validation.
  • The Nano Gateway only validates JSON schemas, so keep OpenAPI schemas to JSON media types.