Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 877 Bytes

File metadata and controls

28 lines (21 loc) · 877 Bytes

How To Model A Nested Record

Use this pattern when one field is itself another authored schema and you want that boundary to stay explicit.

open CodecMapper

type Address = { Street: string; City: string }
let makeAddress street city = { Street = street; City = city }

type Person = { Id: int; Name: string; Home: Address }
let makePerson id name home = { Id = id; Name = name; Home = home }

let addressSchema =
    Schema.record makeAddress
    |> Schema.field "street" _.Street
    |> Schema.field "city" _.City
    |> Schema.build

let personSchema =
    Schema.record makePerson
    |> Schema.field "id" _.Id
    |> Schema.field "name" _.Name
    |> Schema.fieldWith "home" _.Home addressSchema
    |> Schema.build

Use Schema.fieldWith when the child value has its own explicit schema boundary instead of relying on the built-in auto-resolved cases.