Safe Haskell | None |
---|
Synopsis
- data ValidatorResult
- = Success
- | Failure !Text
- | FailureHtml !Text
- isSuccess :: ValidatorResult -> Bool
- isFailure :: ValidatorResult -> Bool
- attachValidatorResult :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> ValidatorResult -> model -> model
- attachFailure :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Text -> model -> model
- attachFailureHtml :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Html -> model -> model
- getValidationFailure :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Text
- getValidationViolation :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Violation
Documentation
data ValidatorResult Source #
Instances
Show ValidatorResult Source # | |
Defined in IHP.ValidationSupport.Types showsPrec :: Int -> ValidatorResult -> ShowS # show :: ValidatorResult -> String showList :: [ValidatorResult] -> ShowS # | |
Eq ValidatorResult Source # | |
Defined in IHP.ValidationSupport.Types (==) :: ValidatorResult -> ValidatorResult -> Bool # (/=) :: ValidatorResult -> ValidatorResult -> Bool # |
isSuccess :: ValidatorResult -> Bool Source #
isFailure :: ValidatorResult -> Bool Source #
attachValidatorResult :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> ValidatorResult -> model -> model Source #
attachFailure :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Text -> model -> model Source #
Adds a plain-text validation error to a record
Example:
>>>
record |> attachFailure #email "should be a valid email"
User { .., meta = MetaBag { .., annotations = [ ("email", TextViolation "should be a valid email") ] } }
You can use this together with getValidationFailure
user |> attachFailure #email "cannot be empty" |> getValidationFailure #email -- Returns: Just "cannot be empty"
If your error message uses HTML code, use attachFailureHtml
.
attachFailureHtml :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Html -> model -> model Source #
Adds a validation error to a record. The error message can contain HTML code.
Example:
>>>
record |> attachFailureHtml #email [hsx|should be a valid email. <a href="https://example.com/docs#email">Check out the documentation</a>|]
User { .., meta = MetaBag { .., annotations = [ ("email", HtmlViolation "should be a valid email. <a href="https://example.com/docs#email">Check out the documentation</a>") ] } }
You can use this together with getValidationViolation
user |> attachFailure #email "cannot be empty" |> getValidationViolation #email -- Returns: Just (HtmlViolation "should be a valid email. <a href="https://example.com/docs#email">Check out the documentation</a>")
getValidationFailure :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Text Source #
Returns the validation failure for a field or Nothing
user |> attachFailure #email "cannot be empty" |> getValidationFailure #email -- Returns: Just "cannot be empty"
When attachFailureHtml
is used, this function will return HTML code:
user |> attachFailureHtml #url [hsx|Invalid value, check <a href="https://example.com">the documentation</a>|] |> getValidationFailure #url -- Returns: Just "Invalid value, check <a href="https://example.com">the documentation</a>"
If you need to special-case validation errors with HTML code, use getValidationViolation
getValidationViolation :: forall (field :: Symbol) model. (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Violation Source #
Similar to getValidationFailure
, but exposes the information whether the error message contains HTML code
>>>
user |> getValidationViolation #email
Just (TextViolation "cannot be empty")