Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80d8807

Browse files
committed
Make error messages customizable.
1 parent f54803e commit 80d8807

File tree

4 files changed

+186
-164
lines changed

4 files changed

+186
-164
lines changed

‎example/Main.elm‎

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Main exposing (main)
22

33
import Browser
44
import Form exposing (Msg(..))
5+
import Form.Error exposing (ErrorValue(..))
56
import Html exposing (..)
67
import Html.Attributes exposing (class)
78
import Html.Events exposing (..)
@@ -10,13 +11,109 @@ import Json.Schema.Builder exposing (..)
1011
import Json.Schema.Definitions
1112
import Schema
1213
import Schema.Encode
14+
import Schema.Error exposing (ValidationError(..))
1315

1416

1517
main : Program () Schema.State Schema.Msg
1618
main =
1719
Browser.sandbox { init = init, update = update, view = view }
1820

1921

22+
init : Schema.State
23+
init =
24+
case schema of
25+
Ok schema_ ->
26+
Schema.init { errors = errorString } schema_
27+
28+
Err error ->
29+
Debug.todo error
30+
31+
32+
update : Schema.Msg -> Schema.State -> Schema.State
33+
update msg state =
34+
Schema.update msg state
35+
36+
37+
view : Schema.State -> Html Schema.Msg
38+
view state =
39+
form [ onSubmit Form.Submit ]
40+
[ Schema.view state
41+
, button [ class "btn btn-primary" ] [ text "Submit" ]
42+
, case Form.getOutput state.form of
43+
Just output ->
44+
let
45+
json =
46+
Json.Encode.encode 4 (Schema.Encode.encode output)
47+
in
48+
pre [] [ text json ]
49+
50+
Nothing ->
51+
text ""
52+
]
53+
54+
55+
errorString : Schema.Errors
56+
errorString path error =
57+
case error of
58+
Empty ->
59+
"The field can not be empty."
60+
61+
InvalidString ->
62+
"This field is required."
63+
64+
InvalidEmail ->
65+
"That is not a valid email address."
66+
67+
InvalidFormat ->
68+
"That is not the correct format."
69+
70+
InvalidInt ->
71+
"That is not a valid number."
72+
73+
InvalidFloat ->
74+
"That is not a valid decimal number."
75+
76+
InvalidBool ->
77+
"That is not a valid option."
78+
79+
SmallerIntThan n ->
80+
"Can not be smaller than " ++ String.fromInt n ++ "."
81+
82+
GreaterIntThan n ->
83+
"Can not be greater than " ++ String.fromInt n ++ "."
84+
85+
SmallerFloatThan n ->
86+
"Can not be smaller than " ++ String.fromFloat n ++ "."
87+
88+
GreaterFloatThan n ->
89+
"Can not be greater than " ++ String.fromFloat n ++ "."
90+
91+
ShorterStringThan n ->
92+
"Must be at least " ++ String.fromInt n ++ " characters long."
93+
94+
LongerStringThan n ->
95+
"Can not be more than " ++ String.fromInt n ++ " characters long."
96+
97+
NotIncludedIn ->
98+
"Is not a valid selection from the list."
99+
100+
CustomError Invalid ->
101+
"Is not valid."
102+
103+
CustomError InvalidSet ->
104+
"All items added need to be unique."
105+
106+
CustomError (ShorterListThan n) ->
107+
if path == "airports" then
108+
"You need to add at least " ++ String.fromInt n ++ " airports."
109+
110+
else
111+
"You need to add at least " ++ String.fromInt n ++ " items."
112+
113+
CustomError (LongerListThan n) ->
114+
"You can not add more than " ++ String.fromInt n ++ " items."
115+
116+
20117
schema : Result String Json.Schema.Definitions.Schema
21118
schema =
22119
buildSchema
@@ -194,36 +291,3 @@ schema =
194291
)
195292
]
196293
|> toSchema
197-
198-
199-
init : Schema.State
200-
init =
201-
case schema of
202-
Ok s ->
203-
Schema.init s
204-
205-
Err error ->
206-
Debug.todo error
207-
208-
209-
update : Schema.Msg -> Schema.State -> Schema.State
210-
update msg state =
211-
Schema.update msg state
212-
213-
214-
view : Schema.State -> Html Schema.Msg
215-
view state =
216-
form [ onSubmit Form.Submit ]
217-
[ Schema.view state
218-
, button [ class "btn btn-primary" ] [ text "Submit" ]
219-
, case Form.getOutput state.form of
220-
Just output ->
221-
let
222-
json =
223-
Json.Encode.encode 4 (Schema.Encode.encode output)
224-
in
225-
pre [] [ text json ]
226-
227-
Nothing ->
228-
text ""
229-
]

‎src/Schema.elm‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
1-
module Schema exposing (Msg, State, init, update, view)
1+
module Schema exposing (Errors, Msg, Options, State, init, update, view)
22

33
import Form as F
44
import Html exposing (..)
55
import Json.Schema.Definitions exposing (Schema)
66
import Schema.Default exposing (default)
7-
import Schema.Form exposing (Form)
7+
import Schema.Error
8+
import Schema.Form exposing (Form, Options)
89
import Schema.Validation exposing (validation)
910

1011

12+
type alias Options =
13+
Schema.Form.Options
14+
15+
16+
type alias Errors =
17+
Schema.Error.Errors Schema.Error.ValidationError
18+
19+
1120
type alias State =
1221
{ form : Form
1322
, schema : Schema
23+
, options : Options
1424
}
1525

1626

1727
type alias Msg =
1828
F.Msg
1929

2030

21-
init : Schema -> State
22-
init schema =
31+
init : Options->Schema -> State
32+
init options schema =
2333
{ form = F.initial (default schema) (validation schema)
2434
, schema = schema
35+
, options = options
2536
}
2637

2738

2839
update : Msg -> State -> State
2940
update msg state =
30-
{ state | form = Debug.log "form"<|F.update (validation state.schema) msg state.form }
41+
{ state | form = F.update (validation state.schema) msg state.form }
3142

3243

3344
view : State -> Html Msg
3445
view state =
35-
Schema.Form.schemaView [] state.schema state.form
46+
Schema.Form.schemaView state.options [] state.schema state.form

‎src/Schema/Error.elm‎

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
module Schema.Error exposing
2-
( ValidationError(..)
3-
, errorString
4-
)
1+
module Schema.Error exposing (Errors, ValidationError(..))
52

63
import Form exposing (Form)
74
import Form.Error exposing (ErrorValue(..))
@@ -14,59 +11,5 @@ type ValidationError
1411
| LongerListThan Int
1512

1613

17-
errorString : ErrorValue ValidationError -> String
18-
errorString error =
19-
case error of
20-
Empty ->
21-
"Fältet får inte vara tomt."
22-
23-
InvalidString ->
24-
"Du måste fylla i fältet."
25-
26-
InvalidEmail ->
27-
"Det är inte en giltig e-postadress."
28-
29-
InvalidFormat ->
30-
"Fältet har inte rätt format."
31-
32-
InvalidInt ->
33-
"Det är inte en siffra."
34-
35-
InvalidFloat ->
36-
"Det är inte ett flyttal."
37-
38-
InvalidBool ->
39-
"Du måste svara ja eller nej."
40-
41-
SmallerIntThan n ->
42-
"Får inte vara mindre än " ++ String.fromInt n ++ "."
43-
44-
GreaterIntThan n ->
45-
"Får inte vara större än " ++ String.fromInt n ++ "."
46-
47-
SmallerFloatThan n ->
48-
"Får inte vara mindre än " ++ String.fromFloat n ++ "."
49-
50-
GreaterFloatThan n ->
51-
"Får inte vara större än " ++ String.fromFloat n ++ "."
52-
53-
ShorterStringThan n ->
54-
"Måste vara minst " ++ String.fromInt n ++ " tecken."
55-
56-
LongerStringThan n ->
57-
"Får inte vara längre än " ++ String.fromInt n ++ " tecken."
58-
59-
NotIncludedIn ->
60-
"Är inte ett korrekt val från listan."
61-
62-
CustomError Invalid ->
63-
"Invalid"
64-
65-
CustomError InvalidSet ->
66-
"Den kombination av val du har gjort är inte tillåten."
67-
68-
CustomError (ShorterListThan n) ->
69-
"Måste innehålla minst " ++ String.fromInt n ++ " saker."
70-
71-
CustomError (LongerListThan n) ->
72-
"Får inte innehålla mer än " ++ String.fromInt n ++ " saker."
14+
type alias Errors e =
15+
String -> ErrorValue e -> String

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /