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:
ApiManifestJsonLogic interface:
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
undefined
value, 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
path
to create nested structures like this:
output for this would be:
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:
In this example when Policy request is sent, it would look something like this:
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):
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:
In this example when Quote request is sent, it would look something like this:
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:
We can do some mathematics with JsonLogic:
Last updated