🖼️
KASKO Frontend Documentation
  • README
  • Getting started
    • Setting up a new webapp
    • Webapp manifest
    • Building the flow
    • Defining layouts
    • Defining computed values
    • Defining dynamic paths
  • Core concepts
    • Forms
    • Validation
      • Using existing validators
      • Adding custom validators
    • Configuration
      • Price format
      • Date format
    • Performing API calls
    • Toggling visibility of components
    • Translations
    • Rules For Robust Application
  • Guides
    • Using logical statements (JsonLogic)
    • Adding discount codes
    • Developing Custom Plugins
    • Setting required requests
    • Adding save quote for later
    • Transforming request data
    • Manifest merging strategy
    • Knockout flow
    • Local pricing logic
    • Setting up dashboard policy profile
    • Handling offers
    • Repeater
  • Snippets
    • Reset input state
  • Useful resources
    • All component descriptions
    • JsonLogic core documentation
    • Kasko.js documentation
    • Example webapp
    • Example plugins (w/ various frameworks)
Powered by GitBook
On this page
  • Gotchas
  • Example with transformed value
  • Example with conditional value
  • Other useful example cases
  1. Guides

Transforming request data

Last updated 9 months ago

Sometimes there is need to change how what values and how are they being sent to API in for example quote or policy requests. Manifest request transformers can be used in cases like these.

Underneath is used to figure out how to transform values. It is used both for send_to_api and value.

Transformer interface:

interface {
  name: string,
  path: string | null,
  send_to_api?: boolean | ApiManifestJsonLogic,
  value?: boolean | string | number | null | ApiManifestJsonLogic
}

ApiManifestJsonLogic interface:

interface {
  type: "jsonlogic",
  schema: JsonLogic
}

JsonLogic can access variables (). We expose 3 state slices for both send_to_api and value JsonLogic: input, flags and quote.

Gotchas

There are some cases that might not be that clear initially:

  • JsonLogic never returns undefined value, instead it returns null;

  • Cannot use Objects in value, as uses Object structure too and it doesn't allow any custom key/property;

    • Instead use dot notation in path to create nested structures like this:

{
  "transformer": [
    {
      "name": "name",
      "path": "data.person",
      "send_to_api": true,
      "value": "John Wick"
    },
    {
      "name": "age",
      "path": "data.person",
      "send_to_api": true,
      "value": 21
    }
  ]
}

output for this would be:

{
  "data": {
    "person": {
      "name": "John Wick",
      "age": 21
    }
  }
}

Example with transformed value

Let's say the API needs address field, but user can tick prefill_address tickbox, that would change address from previously filled in address fields

Webapp manifest configuration fragment:

{
  "version": "1.0",
  "schema": {
    "config": {
      "default_values": {
        "data.prefill_address": true,
        "data.risk_address": "Risk ave 20",
        "data.address": "Classic ave 1"
      },
      "requests": [
        {
          "type": "policy",
          "transformer": [
            {
              "name": "address",
              "path": "data",
              "send_to_api": true,
              "value": {
                "type": "jsonlogic",
                "schema": {
                  "if": [
                    {
                      "===": [
                        {
                          "var": "input.data.prefill_address"
                        },
                        true
                      ]
                    },
                    {
                      "var": "input.data.risk_address"
                    },
                    {
                      "var": "input.data.address"
                    }
                  ]
                }
              }
            }
          ]
        }
      ]
    },
    "flow": []
  }
}

In this example when Policy request is sent, it would look something like this:

{
  "data": {
    "address": "Risk ave 20"
  }
}

And if there's already some data from field_definition it gets merged with transformed data. In case with some other data it would look like this (let's imagine that there is address and risk_address in field_definition too):

{
  "first_name": "John",
  "last_name": "Wick",
  "data": {
    "risk_address": "Risk ave 20",
    "address": "Risk ave 20"
  }
}

Example with conditional value

Let's say it's necessary to send value of person_count to Quote API service conditionally. When user is set has_multiple_persons to true then send value, if has_multiple_persons is false then do not send person_count.

Webapp manifest configuration fragment:

{
  "version": "1.0",
  "schema": {
    "config": {
      "default_values": {
        "has_multiple_persons": true,
        "person_count": 2
      },
      "requests": [
        {
          "type": "quote",
          "transformer": [
            {
              "name": "person_count",
              "path": "",
              "send_to_api": {
                "type": "jsonlogic",
                "schema": {
                  "===": [
                    {
                      "var": "input.has_multiple_persons"
                    },
                    true
                  ]
                }
              },
              "value": {
                "type": "jsonlogic",
                "schema": {
                  "var": "input.person_count"
                }
              }
            }
          ]
        }
      ]
    },
    "flow": []
  }
}

In this example when Quote request is sent, it would look something like this:

{
  "person_count": 2
}

And if there's already some data from field_definition it also gets merged same way like in previous example.

Other useful example cases

{
  "path": "data",
  "name": "date",
  "send_to_api": true,
  "value": {
    "type": "jsonlogic",
    "schema": {
      {
        "cat": [
          {
            "var": "input.data.date"
          }
          "T",
          {
            "var": "input.data.time"
          }
        ]
      }
    }
  }
}
{
  "path": "data",
  "name": "discounted_price",
  "send_to_api": true,
  "value": {
    "type": "jsonlogic",
    "schema": {
      {
        "-": [
          {
            "var": "input.data.price"
          },
          {
            "var": "input.data.discount"
          }
        ]
      }
    }
  }
}

We can use string concatenation with :

We can do some mathematics with :

JsonLogic
more info
JsonLogic
JsonLogic
JsonLogic