First, we create some basic validation functions using the existing Result<_, _>
type in F#.
Note that the Error
case returns a string list
. This is necessary.
let validateAge (age: int) =
if age = 21
then Ok 21
else Error ["Invalid Age"]
let validateHeight (height: float) =
if height = 180.
then Ok 180.
else Error ["Invalid Height"]
let validateName (name: string) =
if name = "Alice"
then Ok "Alice"
else Error ["Invalid Name"]
Then, we create a composite data type that we want to validate.
type Data = {
Age: int
Height: float
Name: string
NotValidated: string
}
Now, we can use our validation
computation expression to compose our existing validators.
let validate (data: Data) =
validation {
validate age in validateAge data.Age
validate height in validateHeight data.Height
validate name in validateName data.Name
return {
data with
Age = age
Height = height
Name = name
}
}
We can now call our validate
method:
validate {
Age = 21
Height = 180.
Name = "Alice"
NotValidated = "hello"
}
which will give us the following output:
Ok ({
Age = 21
Height = 180.
Name = "Alice"
NotValidated = "hello"
})
If we can validate
with invalid data:
validate {
Age = 20
Height = 180.
Name = "Bob"
NotValidated = "hello"
}
we get the following result:
Error [
"Invalid Age"
"Invalid Name"
]
Take a look at sample.fsx
or our unit tests for more examples.