Skip to content

Commit 7c65c8f

Browse files
committed
More lenient url validation accepts custom protocols
1 parent be2b5bd commit 7c65c8f

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/FormToolkit/Field.elm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import Internal.View
7979
import Json.Decode as Decode
8080
import Json.Encode as Encode
8181
import List.Extra
82+
import Regex
8283
import RoseTree.Path as Path
8384
import RoseTree.Tree as Tree
8485
import 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+
14221433
checkPattern : Node id -> Node id
14231434
checkPattern node =
14241435
let

tests/ParseTest.elm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)