File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ import Internal.View
7979import Json.Decode as Decode
8080import Json.Encode as Encode
8181import List.Extra
82+ import Regex
8283import RoseTree.Path as Path
8384import RoseTree.Tree as Tree
8485import String.Extra
@@ -288,7 +289,9 @@ email =
288289 init Email
289290
290291
291- {- | Builds a URL input field.
292+ {- | Builds a URL input field, validation will fail if input is a valid url.
293+ This check is more lenient than `Parse.url` since protocols other than http and
294+ https are accepted.
292295
293296 urlField : Field id
294297 urlField =
@@ -1408,7 +1411,7 @@ checkEmail node =
14081411 setError ParseError node
14091412
14101413 Url ->
1411- case Internal . Value . toString attrs. value |> Maybe . andThen Url . fromString of
1414+ case Internal . Value . toString attrs. value |> Maybe . andThen validateUrl of
14121415 Just _ ->
14131416 node
14141417
@@ -1419,6 +1422,14 @@ checkEmail node =
14191422 node
14201423
14211424
1425+ validateUrl : String -> Maybe Url .Url
1426+ validateUrl urlString =
1427+ Regex . fromString " ^(\\ w+)://"
1428+ |> Maybe . withDefault Regex . never
1429+ |> ( \ regex -> Regex . replace regex ( \ _ -> " http://" ) urlString)
1430+ |> Url . fromString
1431+
1432+
14221433checkPattern : Node id -> Node id
14231434checkPattern node =
14241435 let
Original file line number Diff line number Diff line change @@ -383,6 +383,9 @@ suite =
383383 , \ _ -> testUrl " http://example.com" True
384384 , \ _ -> testUrl " https://example.com/path" True
385385 , \ _ -> testUrl " https://example.com/path?query=value" True
386+ , \ _ -> testUrl " ftp://example.com" True
387+ , \ _ -> testUrl " ssh://example.com" True
388+ , \ _ -> testUrl " custom://example.com" True
386389 , \ _ -> testUrl " not-a-url" False
387390 , \ _ -> testUrl " example.com" False
388391 , \ _ -> testUrl " //example.com" False
You can’t perform that action at this time.
0 commit comments