Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Model string value from external system

$
0
0

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 optionnull 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 }

Viewing all articles
Browse latest Browse all 2592

Trending Articles