Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Created April 28, 2013 17:51
Show Gist options
  • Save eulerfx/5477724 to your computer and use it in GitHub Desktop.
Save eulerfx/5477724 to your computer and use it in GitHub Desktop.
Validation with exceptions
module private Assert =
let validName name = if System.String.IsNullOrEmpty(name) then invalidArg "name" "The name must not be null." else name
let validCount count = if count <= 0 then invalidArg "count" "Inventory count must be positive." else count
let inactive item = if item.isActive = true then failwith "The item is already deactivated."
let exec item =
let apply event =
let newItem = apply item event
event
function
| Create(name) -> name |> Assert.validName |> Created |> apply
| Deactivate -> item |> Assert.inactive; Deactivated |> apply
| Rename(name) -> name |> Assert.validName |> Renamed |> apply
| CheckInItems(count) -> count |> Assert.validCount |> ItemsCheckedIn |> apply
| RemoveItems(count) -> count |> Assert.validCount |> ItemsRemoved |> apply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment