layout: post
title: “Designing with types: Constrained strings”
description: “Adding more semantic information to a primitive type”
nav: thinking-functionally
seriesId: “Designing with types”
seriesOrder: 6

categories: [Types, DDD]

In a previous post, I talked about avoiding using plain primitive strings for email addresses, zip codes, states, etc.
By wrapping them in a single case union, we could (a) force the types to be distinct and (b) add validation rules.

In this post, we’ll look at whether we can extend that concept to an even more fine grained level.

When is a string not a string?

Let’s look a simple PersonalName type.

  1. type PersonalName =
  2. {
  3. FirstName: string;
  4. LastName: string;
  5. }

The type says that the first name is a string. But really, is that all it is? Are there any other constraints that we might need to add to it?

Well, OK, it must not be null. But that is assumed in F#.

What about the length of the string? Is it acceptable to have a name which is 64K characters long? If not, then is there some maximum length allowed?

And can a name contain linefeed characters or tabs? Can it start or end with whitespace?

Once you put it this way, there are quite a lot of constraints even for a “generic” string. Here are some of the obvious ones:

  • What is its maximum length?
  • Can it cross over multiple lines?
  • Can it have leading or trailing whitespace?
  • Can it contain non-printing characters?

Should these constraints be part of the domain model?

So we might acknowledge that some constraints exist, but should they really be part of the domain model (and the corresponding types derived from it)?
For example, the constraint that a last name is limited to 100 characters — surely that is specific to a particular implementation and not part of the domain at all.

I would answer that there is a difference between a logical model and a physical model. In a logical model some of these constraints might not be relevant, but in a physical model they most certainly are. And when we are writing code, we are always dealing with a physical model anyway.

Another reason for incorporating the constraints into the model is that often the model is shared across many separate applications. For example, a personal name may be created in a e-commerce application, which writes it into a database table and then puts it on a message queue to be picked up by a CRM application, which in turn calls an email templating service, and so on.

It is important that all these applications and services have the same idea of what a personal name is, including the length and other constraints. If the model does not make the constraints explicit, then it is easy to have a mismatch when moving across service boundaries.

For example, have you ever written code that checks the length of a string before writing it to a database?

  1. void SaveToDatabase(PersonalName personalName)
  2. {
  3. var first = personalName.First;
  4. if (first.Length > 50)
  5. {
  6. // ensure string is not too long
  7. first = first.Substring(0,50);
  8. }
  9. //save to database
  10. }

If the string is too long at this point, what should you do? Silently truncate it? Throw an exception?

A better answer is to avoid the problem altogether if you can. By the time the string gets to the database layer it is too late — the database layer should not be making these kinds of decisions.

The problem should be dealt with when the string was first created, not when it is used. In other words, it should have been part of the validation of the string.

But how can we trust that the validation has been done correctly for all possible paths? I think you can guess the answer…

Modeling constrained strings with types

The answer, of course, is to create wrapper types which have the constraints built into the type.

So let’s knock up a quick prototype using the single case union technique we used before.

  1. module String100 =
  2. type T = String100 of string
  3. let create (s:string) =
  4. if s <> null && s.Length <= 100
  5. then Some (String100 s)
  6. else None
  7. let apply f (String100 s) = f s
  8. let value s = apply id s
  9. module String50 =
  10. type T = String50 of string
  11. let create (s:string) =
  12. if s <> null && s.Length <= 50
  13. then Some (String50 s)
  14. else None
  15. let apply f (String50 s) = f s
  16. let value s = apply id s
  17. module String2 =
  18. type T = String2 of string
  19. let create (s:string) =
  20. if s <> null && s.Length <= 2
  21. then Some (String2 s)
  22. else None
  23. let apply f (String2 s) = f s
  24. let value s = apply id s

Note that we immediately have to deal with the case when the validation fails by using an option type as the result. It makes creation more painful, but we can’t avoid it if we want the benefits later.

For example, here is a good string and a bad string of length 2.

  1. let s2good = String2.create "CA"
  2. let s2bad = String2.create "California"
  3. match s2bad with
  4. | Some s2 -> // update domain object
  5. | None -> // handle error

In order to use the String2 value we are forced to check whether it is Some or None at the time of creation.

Problems with this design

One problem is that we have a lot of duplicated code. In practice a typical domain only has a few dozen string types, so there won’t be that much wasted code. But still, we can probably do better.

Another more serious problem is that comparisons become harder. A String50 is a different type from a String100 so that they cannot be compared directly.

  1. let s50 = String50.create "John"
  2. let s100 = String100.create "Smith"
  3. let s50' = s50.Value
  4. let s100' = s100.Value
  5. let areEqual = (s50' = s100') // compiler error

This kind of thing will make working with dictionaries and lists harder.

Refactoring

At this point we can exploit F#’s support for interfaces, and create a common interface that all wrapped strings have to support, and also some standard functions:

  1. module WrappedString =
  2. /// An interface that all wrapped strings support
  3. type IWrappedString =
  4. abstract Value : string
  5. /// Create a wrapped value option
  6. /// 1) canonicalize the input first
  7. /// 2) If the validation succeeds, return Some of the given constructor
  8. /// 3) If the validation fails, return None
  9. /// Null values are never valid.
  10. let create canonicalize isValid ctor (s:string) =
  11. if s = null
  12. then None
  13. else
  14. let s' = canonicalize s
  15. if isValid s'
  16. then Some (ctor s')
  17. else None
  18. /// Apply the given function to the wrapped value
  19. let apply f (s:IWrappedString) =
  20. s.Value |> f
  21. /// Get the wrapped value
  22. let value s = apply id s
  23. /// Equality test
  24. let equals left right =
  25. (value left) = (value right)
  26. /// Comparison
  27. let compareTo left right =
  28. (value left).CompareTo (value right)

The key function is create, which takes a constructor function and creates new values using it only when the validation passes.

With this in place it is a lot easier to define new types:

  1. module WrappedString =
  2. // ... code from above ...
  3. /// Canonicalizes a string before construction
  4. /// * converts all whitespace to a space char
  5. /// * trims both ends
  6. let singleLineTrimmed s =
  7. System.Text.RegularExpressions.Regex.Replace(s,"\s"," ").Trim()
  8. /// A validation function based on length
  9. let lengthValidator len (s:string) =
  10. s.Length <= len
  11. /// A string of length 100
  12. type String100 = String100 of string with
  13. interface IWrappedString with
  14. member this.Value = let (String100 s) = this in s
  15. /// A constructor for strings of length 100
  16. let string100 = create singleLineTrimmed (lengthValidator 100) String100
  17. /// Converts a wrapped string to a string of length 100
  18. let convertTo100 s = apply string100 s
  19. /// A string of length 50
  20. type String50 = String50 of string with
  21. interface IWrappedString with
  22. member this.Value = let (String50 s) = this in s
  23. /// A constructor for strings of length 50
  24. let string50 = create singleLineTrimmed (lengthValidator 50) String50
  25. /// Converts a wrapped string to a string of length 50
  26. let convertTo50 s = apply string50 s

For each type of string now, we just have to:

  • create a type (e.g. String100)
  • an implementation of IWrappedString for that type
  • and a public constructor (e.g. string100) for that type.

(In the sample above I have also thrown in a useful convertTo to convert from one type to another.)

The type is a simple wrapped type as we have seen before.

The implementation of the Value method of the IWrappedString could have been written using multiple lines, like this:

  1. member this.Value =
  2. let (String100 s) = this
  3. s

But I chose to use a one liner shortcut:

  1. member this.Value = let (String100 s) = this in s

The constructor function is also very simple. The canonicalize function is singleLineTrimmed, the validator function checks the length, and the constructor is the String100 function (the function associated with the single case, not to be confused with the type of the same name).

  1. let string100 = create singleLineTrimmed (lengthValidator 100) String100

If you want to have other types with different constraints, you can easily add them. For example you might want to have a Text1000 type that supports multiple lines and embedded tabs and is not trimmed.

  1. module WrappedString =
  2. // ... code from above ...
  3. /// A multiline text of length 1000
  4. type Text1000 = Text1000 of string with
  5. interface IWrappedString with
  6. member this.Value = let (Text1000 s) = this in s
  7. /// A constructor for multiline strings of length 1000
  8. let text1000 = create id (lengthValidator 1000) Text1000

Playing with the WrappedString module

We can now play with the module interactively to see how it works:

  1. let s50 = WrappedString.string50 "abc" |> Option.get
  2. printfn "s50 is %A" s50
  3. let bad = WrappedString.string50 null
  4. printfn "bad is %A" bad
  5. let s100 = WrappedString.string100 "abc" |> Option.get
  6. printfn "s100 is %A" s100
  7. // equality using module function is true
  8. printfn "s50 is equal to s100 using module equals? %b" (WrappedString.equals s50 s100)
  9. // equality using Object method is false
  10. printfn "s50 is equal to s100 using Object.Equals? %b" (s50.Equals s100)
  11. // direct equality does not compile
  12. printfn "s50 is equal to s100? %b" (s50 = s100) // compiler error

When we need to interact with types such as maps that use raw strings, it is easy to compose new helper functions.

For example, here are some helpers to work with maps:

  1. module WrappedString =
  2. // ... code from above ...
  3. /// map helpers
  4. let mapAdd k v map =
  5. Map.add (value k) v map
  6. let mapContainsKey k map =
  7. Map.containsKey (value k) map
  8. let mapTryFind k map =
  9. Map.tryFind (value k) map

And here is how these helpers might be used in practice:

  1. let abc = WrappedString.string50 "abc" |> Option.get
  2. let def = WrappedString.string100 "def" |> Option.get
  3. let map =
  4. Map.empty
  5. |> WrappedString.mapAdd abc "value for abc"
  6. |> WrappedString.mapAdd def "value for def"
  7. printfn "Found abc in map? %A" (WrappedString.mapTryFind abc map)
  8. let xyz = WrappedString.string100 "xyz" |> Option.get
  9. printfn "Found xyz in map? %A" (WrappedString.mapTryFind xyz map)

So overall, this “WrappedString” module allows us to create nicely typed strings without interfering too much. Now let’s use it in a real situation.

Using the new string types in the domain

Now we have our types, we can change the definition of the PersonalName type to use them.

  1. module PersonalName =
  2. open WrappedString
  3. type T =
  4. {
  5. FirstName: String50;
  6. LastName: String100;
  7. }
  8. /// create a new value
  9. let create first last =
  10. match (string50 first),(string100 last) with
  11. | Some f, Some l ->
  12. Some {
  13. FirstName = f;
  14. LastName = l;
  15. }
  16. | _ ->
  17. None

We have created a module for the type and added a creation function that converts a pair of strings into a PersonalName.

Note that we have to decide what to do if either of the input strings are invalid. Again, we cannot postpone the issue till later, we have to deal with it at construction time.

In this case we use the simple approach of creating an option type with None to indicate failure.

Here it is in use:

  1. let name = PersonalName.create "John" "Smith"

We can also provide additional helper functions in the module.

Let’s say, for example, that we want to create a fullname function that will return the first and last names joined together.

Again, more decisions to make.

  • Should we return a raw string or a wrapped string?
    The advantage of the latter is that the callers know exactly how long the string will be, and it will be compatible with other similar types.

  • If we do return a wrapped string (say a String100), then how do we handle the the case when the combined length is too long? (It could be up to 151 chars, based on the length of the first and last name types.). We could either return an option, or force a truncation if the combined length is too long.

Here’s code that demonstrates all three options.

  1. module PersonalName =
  2. // ... code from above ...
  3. /// concat the first and last names together
  4. /// and return a raw string
  5. let fullNameRaw personalName =
  6. let f = personalName.FirstName |> value
  7. let l = personalName.LastName |> value
  8. f + " " + l
  9. /// concat the first and last names together
  10. /// and return None if too long
  11. let fullNameOption personalName =
  12. personalName |> fullNameRaw |> string100
  13. /// concat the first and last names together
  14. /// and truncate if too long
  15. let fullNameTruncated personalName =
  16. // helper function
  17. let left n (s:string) =
  18. if (s.Length > n)
  19. then s.Substring(0,n)
  20. else s
  21. personalName
  22. |> fullNameRaw // concat
  23. |> left 100 // truncate
  24. |> string100 // wrap
  25. |> Option.get // this will always be ok

Which particular approach you take to implementing fullName is up to you. But it demonstrates a key point about this style of type-oriented design: these decisions have to be taken up front, when creating the code. You cannot postpone them till later.

This can be very annoying at times, but overall I think it is a good thing.

Revisiting the email address and zip code types

We can use this WrappedString module to reimplement the EmailAddress and ZipCode types.

  1. module EmailAddress =
  2. type T = EmailAddress of string with
  3. interface WrappedString.IWrappedString with
  4. member this.Value = let (EmailAddress s) = this in s
  5. let create =
  6. let canonicalize = WrappedString.singleLineTrimmed
  7. let isValid s =
  8. (WrappedString.lengthValidator 100 s) &&
  9. System.Text.RegularExpressions.Regex.IsMatch(s,@"^\S+@\S+\.\S+$")
  10. WrappedString.create canonicalize isValid EmailAddress
  11. /// Converts any wrapped string to an EmailAddress
  12. let convert s = WrappedString.apply create s
  13. module ZipCode =
  14. type T = ZipCode of string with
  15. interface WrappedString.IWrappedString with
  16. member this.Value = let (ZipCode s) = this in s
  17. let create =
  18. let canonicalize = WrappedString.singleLineTrimmed
  19. let isValid s =
  20. System.Text.RegularExpressions.Regex.IsMatch(s,@"^\d{5}$")
  21. WrappedString.create canonicalize isValid ZipCode
  22. /// Converts any wrapped string to a ZipCode
  23. let convert s = WrappedString.apply create s

Other uses of wrapped strings

This approach to wrapping strings can also be used for other scenarios where you don’t want to mix string types together accidentally.

One case that leaps to mind is ensuring safe quoting and unquoting of strings in web applications.

For example, let’s say that you want to output a string to HTML. Should the string be escaped or not?
If it is already escaped, you want to leave it alone but if it is not, you do want to escape it.

This can be a tricky problem. Joel Spolsky discusses using a naming convention here, but of course, in F#, we want a type-based solution instead.

A type-based solution will probably use a type for “safe” (already escaped) HTML strings (HtmlString say), and one for safe Javascript strings (JsString), one for safe SQL strings (SqlString), etc.
Then these strings can be mixed and matched safely without accidentally causing security issues.

I won’t create a solution here (and you will probably be using something like Razor anyway), but if you are interested you can read about a Haskell approach here and a port of that to F#.

Update

Many people have asked for more information on how to ensure that constrained types such as EmailAddress are only created through a special constructor that does the validation.
So I have created a gist here that has some detailed examples of other ways of doing it.