This module provides the validation logic for the library. Please refer to the examples for a better understanding
Returns a successful FieldValidation using a result
Returns a failed FieldValidation using an error
Returns a failed FieldValidation using a ConfigError
First of all you need to specify what type of Field you want to validate
Helps validating a string Field (input, select). By using this function
you basically say "this field is an input or a select, we are waiting for a
string here". And then you pass your string FieldValidation function that
will validate the string
type YourError
= EmptyString
notEmpty : Value -> FieldValidation YourError String
notEmpty =
stringField
(\aString ->
if String.isEmpty aString then
failure EmptyString
else
success aString
)Note: if the field Value is not a string, it fails with a WrongType
ConfigError
Helps validating a bool Field (checkbox). By using this function you
basically say "this field is a checkbox, we are waiting for a bool here".
And then you pass your bool FieldValidation function that will validate
the bool
type YourError
= NotChecked
isChecked : Value -> FieldValidation YourError Bool
isChecked =
checkboxField
(\aBool ->
if aBool then
success aBool
else
failure NotChecked
)Note: if the field Value is not a bool, it fails with a WrongType
ConfigError
Then you create a validation function for this specific type, or use the basic validation helpers defined below.
Helps creating a basic validation function. It is useful when you have a basic test and you only need to fail with one error.
type YourError
= MustBeEmpty
emptyValidation : String -> FieldValidation YourError String
emptyValidation =
validation MustBeEmpty String.isEmptyHelps validating a Bool that is True (checkbox checked). If the Bool
is False, fails with the given error.
type YourError
= NotChecked
| ...
postValidation : Bool -> FieldValidation YourError a
...
validateField : Bool -> FieldValidation YourError a
validateField =
isChecked NotChecked postValidationHelps validating a String that can be cast into an Int. If the String
cannot be cast, fails with the given error.
type YourError
= NotInt
| ...
postValidation : Int -> FieldValidation YourError a
...
validateField : String -> FieldValidation YourError a
validateField =
int NotInt postValidationHelps validating a String that can be cast into a Float. If the String
cannot be cast, fails with the given error.
type YourError
= NotFloat
| ...
postValidation : Float -> FieldValidation YourError a
...
validateField : String -> FieldValidation YourError a
validateField =
float NotFloat postValidationHelps validating a String that is not empty. If the String is empty,
fails with the given error.
type YourError
= StringEmpty
| ...
postValidation : String -> FieldValidation YourError a
...
validateField : String -> FieldValidation YourError a
validateField =
notEmpty StringEmpty postValidationHelps validating a String that has a specific length (> low && < high).
If the String has a different length, fails with the given error.
type YourError
= WrongLength
| ...
postValidation : String -> FieldValidation YourError a
...
validateField : String -> FieldValidation YourError a
validateField =
length 4 6 WrongLength postValidationHelps validating a String that is an email. If the Stringis not an
email, fails with the given error.
type YourError
= NotValidEmail
| ...
postValidation : String -> FieldValidation YourError a
...
validateField : String -> FieldValidation YourError a
validateField =
email NotValidEmail postValidationHelps validating two string Value that match. If the strings
don't match, fails with the given error.
type YourError
= DifferentPassword
| ...
postValidation : String -> FieldValidation YourError a
...
validateField : Value -> Value -> FieldValidation YourError a
validateField =
passwordMatch DifferentPassword postValidationNote: if the two Value are not strings, it fails with a WrongType
ConfigError
A FormValidation represents the validation of a Form.
It is similar to FieldValidation: you are not going use the type itself but
instead you will create a Validate function (see below)
Returns a successful FormValidation using a result
Validate represents the function that validates a Form.
It takes Fields and creates a FormValidation.
It usually starts with the valid function, representing the final result,
and then uses the different Validate helpers to validate each form Field
validateForm : Validate String YourError Model
validateForm fields =
valid Model
|> required fields "field-name1" (stringField ...)
|> required fields "field-name2" (boolField ...)
|> optional fields "field-name3" ...
...Validates a required Field. This is the basic use case : the Field is
required and you want to validate it.
...
|> required fields "first-name" (stringField <| someStringValidation)
...Hardcodes a value. This is useful when you need to harcode a specific value during the validation process
...
|> hardcoded "someHarcodedValue"
...Validates an optional Field. This will use the validation function if
the Field is not empty or use the default value otherwise
...
|> optional fields "wallet" 0.0 (float ...)
...Note: it only works on string Field so no need to use stringField here
Validates an optional Field with Maybe. Same logic than optional but
the default value is Nothing and the validated one is Just
...
|> optionalWithMaybe fields "age" (int ...)
...Note: it only works on string Field so no need to use stringField here
Validates a discardable Field. This is useful when you need to validate
the Field but don't need the result.
...
|> discardable fields "terms" (boolField <| isChecked ...)
...Validates two Field together. This is useful when you need to validate
one Field that depends on another, but you only need to store one result.
...
|> FV.twoFields fields "password" "password-repeat" (passwordMatch ...)
...Note : the validation function takes two field Value so you cannot use the
stringField and boolField helpers here, you will have to deal with Value
Validates a fieldgroup. This is mainly useful when you need to nest another validation process
...
|> fieldgroup fields "job-profile" jobProfileValidate
...The default behavior of Validate is to validate each Field and gather all
the errors while validating the all Form (it is accumulative).
But if you need a non-accumulative validation process (this means that the validation stops at the first error encountered) there are non-accumulative versions of the validate helpers.
They have the same name but finish with a 1. Do not mix them with the default ones.
Validates a required Field (non-accumulative)
Hardcodes a value (non-accumulative)
Validates an optional Field (non-accumulative)
Validates an optional Field with Maybe (non-accumulative)
Validates a discardable Field (non-accumulative)
Validates two Field together (non-accumulative)
Validates a fieldgroup (non-accumulative)
A
FieldValidationrepresents the validation of aField.It can be a
successor afailure(and sometimes aconfigFailure, please refer to theConfigErrordefinition and the README troubleshooting section).You are not going to use the
FieldValidationitself but you will create aFieldValidationfunction that will help you validateField