Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slug validator #1076

Merged
merged 2 commits into from Sep 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion IHP/ValidationSupport/ValidateField.hs
Expand Up @@ -129,6 +129,7 @@ validateAny validators text =
case any isSuccess $ map ($ text) validators of
True -> Success
False -> Failure "did not pass any validators"
{-# INLINABLE validateAny #-}


-- | Validates that value passes all of the given validators
Expand All @@ -146,6 +147,8 @@ validateAll validators text =
in case all isSuccess results of
True -> Success
False -> (filter isFailure results) !! 0
{-# INLINABLE validateAll #-}


-- | Validates that value is not empty
--
Expand Down Expand Up @@ -389,6 +392,7 @@ isColor = validateAny [isRgbHexColor, isRgbaHexColor, isRgbColor, isRgbaColor]
|> withCustomErrorMessage "is not a valid color"
{-# INLINABLE isColor #-}


-- | Validates string starts with @http://@ or @https://@
--
-- >>> isUrl "https://digitallyinduced.com"
Expand All @@ -405,7 +409,7 @@ isUrl text = Failure "is not a valid url. It needs to start with http:// or http
isInList :: (Eq value, Show value) => [value] -> value -> ValidatorResult
isInList list value | list |> includes value = Success
isInList list value = Failure ("is not allowed. It needs to be one of the following: " <> (tshow list))

{-# INLINABLE isInList #-}

-- | Validates that value is True
--
Expand All @@ -416,6 +420,7 @@ isInList list value = Failure ("is not allowed. It needs to be one of the follow
-- Failure "This field cannot be false"
isTrue :: Bool -> ValidatorResult
isTrue value = if value then Success else Failure "This field cannot be false"
{-# INLINABLE isTrue #-}


-- | Validates that value is False
Expand All @@ -427,6 +432,7 @@ isTrue value = if value then Success else Failure "This field cannot be false"
-- Failure "This field cannot be true"
isFalse :: Bool -> ValidatorResult
isFalse value = if not value then Success else Failure "This field cannot be true"
{-# INLINABLE isFalse #-}


-- | Validates that value is matched by the regular expression
Expand All @@ -442,3 +448,17 @@ isFalse value = if not value then Success else Failure "This field cannot be tru
--
matchesRegex :: Text -> Text -> ValidatorResult
matchesRegex regex text = if text =~ regex then Success else Failure $ "This field does not match the regular expression \"" <> regex <> "\""
{-# INLINABLE matchesRegex #-}


-- | Validates that value is a valid slug
--
-- >>> isSlug "i-am-a-slug"
-- Success
--
-- >>> isSlug "I-AM-A-Slug (Copy)"
-- Failure "is not a valid slug (consisting of only letters, numbers, underscores or hyphens)"
isSlug :: Text -> ValidatorResult
isSlug text | text =~ ("^[a-zA-Z0-9_-]+$" :: Text) = Success
isSlug text = Failure "is not a valid slug (consisting of only letters, numbers, underscores or hyphens)"
{-# INLINABLE isSlug #-}