@@ -2,6 +2,7 @@ module Main exposing (main)
22
33import Browser
44import Form exposing (Msg (..) )
5+ import Form.Error exposing (ErrorValue (..) )
56import Html exposing (..)
67import Html.Attributes exposing (class )
78import Html.Events exposing (..)
@@ -10,13 +11,109 @@ import Json.Schema.Builder exposing (..)
1011import Json.Schema.Definitions
1112import Schema
1213import Schema.Encode
14+ import Schema.Error exposing (ValidationError (..) )
1315
1416
1517main : Program () Schema .State Schema .Msg
1618main =
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+ 20117schema : Result String Json .Schema .Definitions .Schema
21118schema =
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- ]
0 commit comments