Skip to content

Commit

Permalink
add matchesRegex validator
Browse files Browse the repository at this point in the history
  • Loading branch information
hendi committed Sep 1, 2021
1 parent 99dccbc commit cf3197d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions IHP/ValidationSupport/ValidateField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,18 @@ 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"


-- | Validates that value is matched by the regular expression
--
-- >>> matchesRegex "^[0-9]{4}$" "2016"
-- Success
--
-- >>> matchesRegex "^[0-9]{4}$" "16"
-- Failure "This field does not match the regular expression \"^[0-9]{4}$\""
--
-- >>> matchesRegex "[0-9]{4}" "xx2016xx"
-- Success -- regex is missing ^ and $
--
matchesRegex :: Text -> Text -> ValidatorResult
matchesRegex regex text = if text =~ regex then Success else Failure $ "This field does not match the regular expression \"" <> regex <> "\""

0 comments on commit cf3197d

Please sign in to comment.