Alternatively, you can use rescript-schema to leverage data transformation when interacting with external systems:
type info = {
contractPerson: option<string>,
}
let infoSchema = S.object(s => {
contractPerson: s.field("contract_person", S.null(S.string))
})
let data = {
contractPerson: None,
}->S.serializeOrRaiseWith(infoSchema)
Console.log(data) // { contract_person: null }
Besides the option → null automatic conversion, you can notice in the example that you can also transform field names. And there are many more features you can find in the docs.
Also, there’s a ppx mode:
@schema
type info = {
@as("contract_person")
contractPerson: @s.null option<string>,
}
let data = {
contractPerson: None,
}->S.serializeOrRaiseWith(infoSchema)
Console.log(data) // { contract_person: null }