Transforming request data
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 JsonLogic 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 (more info). 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
undefinedvalue, instead it returnsnull;Cannot use Objects in
value, as JsonLogic uses Object structure too and it doesn't allow any custom key/property;Instead use dot notation in
pathto 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
We can use string concatenation with JsonLogic:
{
"path": "data",
"name": "date",
"send_to_api": true,
"value": {
"type": "jsonlogic",
"schema": {
{
"cat": [
{
"var": "input.data.date"
}
"T",
{
"var": "input.data.time"
}
]
}
}
}
}We can do some mathematics with JsonLogic:
{
"path": "data",
"name": "discounted_price",
"send_to_api": true,
"value": {
"type": "jsonlogic",
"schema": {
{
"-": [
{
"var": "input.data.price"
},
{
"var": "input.data.discount"
}
]
}
}
}
}Last updated