Rerouted (regenerated, verified). Same repository and tag, but GitHub now builds the zip with different bytes, so it no longer matches the SHA-1 package.elm-lang.org recorded. The Lamdera package server serves a copy recovered from https://github.com/whage/elm-validate.
What we checked: Tag 1.1.0 is still on github.com/whage/elm-validate; its commit 8a955e25f5e8 is from 2020-02-10 14:42 UTC, before the version was published (2020-02-10 14:48 UTC); elm.json is byte-identical to package.elm-lang.org's; the exposed modules match docs.json; the zip's SHA-1 is not the published one (GitHub's zip no longer comes out byte for byte the same), so the contents were checked instead.
The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.
Details · package.elm-lang.org's zip: https://github.com/whage/elm-validate/zipball/1.1.0/
elm-validate provides convenience functions for validating data.
It is based around the idea of a Validator, which runs checks on a
subject and returns a Result which can be be either Ok (Valid originalSubject)
if there were no validation errors or Err validationErrors if the
validation failed.
case validate someValidator someSubject of
Ok validSubject -> ...--> (Valid someSubject)
Err validationErrors -> ...--> List of validation errorsFor example:
import Validate exposing (ifBlank, ifNotInt, validate)
type alias Model =
{ name : String, email : String, age : String, selections : List String }
modelValidator : Validator String Model
modelValidator =
Validate.all
[ ifBlank .name "Please enter a name."
, ifBlank .email "Please enter an email address."
, ifNotInt .age "Age must be a whole number."
, ifEmptyList .selections "Please select at least one."
]
validate modelValidator
{ name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
--> Err [ "Please enter an email address.", "Age must be a whole number." ]
validate modelValidator
{ name = "Sam", email = "[email protected]", age = "27", selections = [ "cats" ] }
--> Ok (Valid { name = "Sam", email = "[email protected]", age = "27", selections = [ "cats" ] })You can represent your errors however you like. One nice approach is to use tuple of the error message and the field responsible for the error:
type Field =
Name | Email | Age | Selections
modelValidator : Validator ( Field, String ) Model
modelValidator =
Validate.all
[ ifBlank .name ( Name, "Please enter a name." )
, ifBlank .email ( Email, "Please enter an email address." )
, ifNotInt .age ( Age, "Age must be a whole number." )
, ifEmptyList .selections ( Selections, "Please select at least one." )
]
type alias Model =
{ name : String, email : String, age : String }
validate modelValidator
{ name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
--> Err [ ( Email, "Please enter an email address." )
--> , ( Age, "Age must be a whole number." )
--> ]Functions that detect the absence of a value, such as ifBlank and ifEmptyList, accept an error as the second argument:
modelValidator : Validator ( Field, String ) Model
modelValidator =
Validate.all
[ ifBlank .name ( Name, "Please enter a name." )
, ifEmptyList .selections ( Selections, "Please select at least one." )
]Functions the detect something wrong with a value, such as isNotInt and isInvalidEmail, accept a function as the second argument so you can include the incorrect value in the error message:
modelValidator : Validator ( Field, String ) Model
modelValidator =
Validate.all
[ ifNotInt .count (\value -> ( Count, value ++ " is not an integer." ))
, ifInvalidEmail .email (\value -> ( Email, value ++ " is not a valid email address." ))
]