Skip to content

Commit

Permalink
Use a more robust check for isUrl validator (#1880)
Browse files Browse the repository at this point in the history
* Use a more robust check for isUrl

* URL must start with http:// or https://
  • Loading branch information
amitaibu authored Dec 30, 2023
1 parent d065883 commit 9fe2cde
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions IHP/ValidationSupport/ValidateField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import IHP.ModelSupport
import IHP.HaskellSupport
import Text.Regex.TDFA
import Data.List ((!!))
import Network.URI (parseURI, uriScheme)

-- | A function taking some value and returning a 'ValidatorResult'
--
Expand Down Expand Up @@ -411,10 +412,15 @@ isColor = validateAny [isRgbHexColor, isRgbaHexColor, isRgbColor, isRgbaColor]
-- Success
--
-- >>> isUrl "digitallyinduced.com"
-- Failure "is not a valid url. It needs to start with http:// or https://"
-- Failure "URL must start with http:// or https://"
isUrl :: Text -> ValidatorResult
isUrl text | "http://" `isPrefixOf` text || "https://" `isPrefixOf` text = Success
isUrl text = Failure "is not a valid url. It needs to start with http:// or https://"
isUrl url =
case parseURI (unpack url) of
Just uri ->
if uriScheme uri `elem` ["http:", "https:"]
then Success
else Failure "URL must start with http:// or https://"
Nothing -> Failure "Invalid URL"
{-# INLINABLE isUrl #-}


Expand Down

0 comments on commit 9fe2cde

Please sign in to comment.