This module provides the validation logic for the library. Please refer to the examples for a better understanding
A FieldValidation represents the Validation of a Field
Returns a failed FieldValidation
Returns a failed FieldValidation using a CustomError
Returns a successful FieldValidation
Helps creating a basic FieldValidation function
type YourError
= MustBeEmpty
emptyValidation : String -> FieldValidation YourError String
emptyValidation =
validation MustBeEmpty String.isEmptyHelps validating a String Value. If the Value has a different type,
fails with a WrongType FieldError.
type YourError
= MustBeEmpty
validateEmptyField : Value -> FieldValidation YourError String
validateEmptyField =
stringValid (validation MustBeEmpty String.isEmpty)Helps validating a Bool Value. If the Value has a different type,
fails with a WrongType FieldError.
type YourError
= MustBeChecked
validateCheckedField : Value -> FieldValidation YourError Bool
validateCheckedField =
boolValid (validation MustBeChecked ((==) True))Helps validating a String that can be cast into an Int. If the String
cannot be cast, fails with a NotInt FieldError.
doYourValidation : Int -> FieldValidation err a
validateField : Value -> FieldValidation err a
validateField =
stringValid (int (doYourValidation))Helps validating a String that can be cast into a Float. If the String
cannot be cast, fails with a NotFloat FieldError.
doYourValidation : Float -> FieldValidation err a
validateField : Value -> FieldValidation err a
validateField =
stringValid (float (doYourValidation))Helps validating a String that is not empty. If the Stringis empty,
fails with an EmptyString FieldError.
doYourValidation : String -> FieldValidation err a
validateField : Value -> FieldValidation err a
validateField =
stringValid (notEmpty (doYourValidation))Helps validating a String that has a specific length (> low && < high).
If the String has a different length, fails with a WrongLength FieldError.
doYourValidation : String -> FieldValidation err a
validateField : Value -> FieldValidation err a
validateField =
stringValid (length low high (doYourValidation))Helps validating a String that is an email. If the Stringis not an email,
fails with a NotEmail FieldError.
doYourValidation : String -> FieldValidation err a
validateField : Value -> FieldValidation err a
validateField =
stringValid (email (doYourValidation))Helps validating two String Values that match. If the Strings
don't match, fails with a PasswordNotEqual FieldError.
doYourValidation : String -> FieldValidation err a
validateField : Value -> Value -> FieldValidation err a
validateField =
passwordMatch (doYourValidation)A FormError represents an error that happened during a FormValidation.
It's basically a wrapper around the FieldError with the key associated
to the Field
A FormValidation represents the Validation of a Form
Returns a successful FormValidation
Converts a FormError into a Tuple
Validate represents a function that validates a Form.
It takes a Group of Fields and returns a FormValidation
Those functions help building a Validate function and aim to ease the
FormValidation. They will accumulate the different FormErrors during the
validation process.
Validates a required Field. This is the basic use case and will always
validate the Field
...
|> required fields comparable doYourValidation
...Hardcodes a value. This is useful when you need to harcode a specific value during the validation process
...
|> hardcoded yourHardcodedValue
...Validates an optional Field. This will use the validation function if
the Field is not empty or use the default value otherwise. Only works
on String Value
...
|> optional fields comparable doYourValidation yourDefaultValue
...optional is equivalent to a specific required:
...
|> required
fields
comparable
(stringValid <|
\str ->
if String.isEmpty str then
success yourDefaultValue
else
doYourValidation str
)
...Validates an optional Field with a Maybe. Same logic than with optional
but the default value is Nothing and the validated value is Just
...
|> optionalMaybe fields comparable doYourValidation
...Validates two Fields together. The validation function takes two Field
Values and returns one result. It is useful when you need to validate one
Field that depends on another, but you only need to store one result
...
|> twoFields fields comparable1 comparable2 doYourValidation
...Validates a Group of Fields. This can be useful in many different cases
but mainly when you need to nest a validation process
...
|> fieldGroup fields comparable doYourValidation
...Those functions are the "binding" equivalent of the previous ones. This means
that the FormValidation will fail at the first FormError encountered and
won't accumulate the errors. Don't mix those functions with the previous ones
Validates a required Field (binding)
...
|> required1 fields comparable doYourValidation
...Hardcodes a value for a Field (binding)
...
|> hardcoded1 yourHardcodedValue
...Validates an optional Field (binding)
...
|> optional1 fields comparable doYourValidation yourDefaultValue
...Validates an optional Field with a Maybe (binding)
...
|> optionalMaybe1 fields comparable doYourValidation
...Validates two Fields together (binding)
...
|> twoFields1 fields comparable1 comparable2 doYourValidation
...Validates a Group of Fields (binding)
...
|> fieldGroup fields comparable doYourValidation
...
A
FieldErrorrepresents an error that happened during aFieldValidation. Here are the main ones :