11module  Schema  exposing  (init , update , view )
22
33import  Form  as  F 
4- import  Form.Error  exposing  (ErrorValue (..) )
5- import  Form.Input  as  Input 
6- import  Form.Validate  exposing  (..)
74import  Html  exposing  (..)
8- import  Html.Attributes  exposing  (..)
9- import  Json.Decode 
10- import  Json.Schema 
11- import  Json.Schema.Definitions 
12-  exposing
13-  (  Schema (..) 
14-  ,   SingleType (..) 
15-  ,   SubSchema 
16-  ,   Type (..) 
17-  ) 
18- import  Schema.Error  exposing  (ValidationError , errorString )
5+ import  Json.Schema.Definitions  exposing  (Schema )
6+ import  Schema.Form  exposing  (Form )
197import  Schema.Validation  exposing  (validation )
20- import  Schema.Value  exposing  (Value (..) )
218
229
2310type alias  State  = 
@@ -26,18 +13,6 @@ type alias State =
2613 } 
2714
2815
29- type alias  Form  = 
30-  F . Form  ValidationError  Value 
31- 32- 33- type alias  ErrorString  e = 
34-  ErrorValue  e ->  String 
35- 36- 37- type alias  Path  = 
38-  List  String 
39- 40- 4116type alias  Msg  = 
4217 F . Msg 
4318
@@ -56,202 +31,4 @@ update msg state =
5631
5732view  :  State  ->  Html  Msg 
5833view  state = 
59-  schemaView []   state. schema state. form
60- 61- 62- schemaView  :  Path  ->  Schema  ->  Form  ->  Html  Msg 
63- schemaView  path schema form = 
64-  case  schema of 
65-  BooleanSchema  value -> 
66-  div [] 
67-  [   if  value ==  True  then 
68-  text " True" 
69- 70-  else 
71-  text " False" 
72-  ] 
73- 74-  ObjectSchema  subSchema -> 
75-  objectView path subSchema form
76- 77- 78- objectView  :  Path  ->  SubSchema  ->  Form  ->  Html  Msg 
79- objectView  path schema form = 
80-  case  schema. type_ of 
81-  AnyType  -> 
82-  fieldset []   [] 
83- 84-  NullableType  _ -> 
85-  fieldset []   [] 
86- 87-  UnionType  _ -> 
88-  fieldset []   [] 
89- 90-  SingleType  singleType -> 
91-  fieldView path schema singleType form
92- 93- 94- fieldView  :  Path  ->  SubSchema  ->  SingleType  ->  Form  ->  Html  Msg 
95- fieldView  path schema type_ form = 
96-  case  type_ of 
97-  IntegerType  -> 
98-  txt schema ( getFieldAsString path form) 
99- 100-  NumberType  -> 
101-  txt schema ( getFieldAsString path form) 
102- 103-  StringType  -> 
104-  case  schema. enum of 
105-  Just  _ -> 
106-  select schema ( getFieldAsString path form) 
107- 108-  Nothing  -> 
109-  txt schema ( getFieldAsString path form) 
110- 111-  BooleanType  -> 
112-  checkbox schema ( getFieldAsBool path form) 
113- 114-  ArrayType  -> 
115-  fieldset [] 
116-  [] 
117- 118-  ObjectType  -> 
119-  let 
120-  f = 
121-  getFieldAsString path form
122- 123-  schemataItem (  name,   subSchema )  = 
124-  schemaView ( path ++  [   name ] )  subSchema form
125- 126-  fields = 
127-  case  schema. properties of 
128-  Nothing  -> 
129-  [] 
130- 131-  Just  ( Json . Schema . Definitions . Schemata  schemata)  -> 
132-  List . map schemataItem schemata
133- 134-  meta = 
135-  [   Maybe . map ( \ str ->  h3 []   [   text str ] )  schema. title
136-  ,   Maybe . map ( \ str ->  p []   [   text str ] )  schema. description
137-  ,   Maybe . map ( error errorString)  f. liveError
138-  ] 
139-  |>  List . filterMap identity
140-  in 
141-  div [   id ( fieldPath path) ,   tabindex - 1  ]   ( meta ++  fields) 
142- 143-  NullType  -> 
144-  fieldset [] 
145-  [] 
146- 147- 148- txt  :  SubSchema  ->  F .FieldState  ValidationError  String  ->  Html  Msg 
149- txt  schema f = 
150-  field schema f <| 
151-  Input . textInput f
152-  [   class " form-control" 
153-  ,   id f. path
154-  ] 
155- 156- 157- checkbox  :  SubSchema  ->  F .FieldState  ValidationError  Bool  ->  Html  Msg 
158- checkbox  schema f = 
159-  fieldset
160-  [   classList
161-  [   (  " form-group" ,   True  ) 
162-  ,   (  " form-check" ,   True  ) 
163-  ,   (  " is-invalid" ,   f. liveError /=  Nothing  ) 
164-  ] 
165-  ] 
166-  [   label [   class " form-check-label"   ] 
167-  [   Input . checkboxInput f [   class " form-check-input" ,   id f. path ] 
168-  ,   text ( schema. title |>  Maybe . withDefault " " ) 
169-  ,   case  schema. description of 
170-  Just  str -> 
171-  div []   [   small []   [   text str ]   ] 
172- 173-  Nothing  -> 
174-  text " " 
175-  ] 
176-  ,   case  f. liveError of 
177-  Just  err -> 
178-  error errorString err
179- 180-  Nothing  -> 
181-  text " " 
182-  ] 
183- 184- 185- select  :  SubSchema  ->  F .FieldState  ValidationError  String  ->  Html  Msg 
186- select  schema f = 
187-  let 
188-  options = 
189-  case  schema. enum of 
190-  Just  values -> 
191-  values
192-  |>  List . map ( Json . Decode . decodeValue Json . Decode . string) 
193-  |>  List . map ( Result . withDefault " " ) 
194-  |>  List . map ( \ str ->  (  str,   str )) 
195- 196-  Nothing  -> 
197-  [] 
198-  in 
199-  field schema f <| 
200-  Input . selectInput ( [   (  " " ,   " "   )  ]   ++  options) 
201-  f
202-  [   class " form-control custom-select" 
203-  ,   id f. path
204-  ] 
205- 206- 207- field  :  SubSchema  ->  F .FieldState  ValidationError  String  ->  Html  Msg  ->  Html  Msg 
208- field  schema f content = 
209-  fieldset
210-  [   classList
211-  [   (  " form-group" ,   True  ) 
212-  ,   (  " is-invalid" ,   f. liveError /=  Nothing  ) 
213-  ,   (  " has-value" ,   f. value /=  Nothing  &&  f. value /=  Just  " "   ) 
214-  ] 
215-  ] 
216-  [   label [   for f. path ] 
217-  [   case  schema. title of 
218-  Just  str -> 
219-  span [   class " label-text"   ]   [   text str ] 
220- 221-  Nothing  -> 
222-  text " " 
223-  ,   content
224-  ,   case  schema. description of 
225-  Just  str -> 
226-  div []   [   small []   [   text str ]   ] 
227- 228-  Nothing  -> 
229-  text " " 
230-  ] 
231-  ,   case  f. liveError of 
232-  Just  err -> 
233-  error errorString err
234- 235-  Nothing  -> 
236-  text " " 
237-  ] 
238- 239- 240- error  :  ErrorString  ValidationError  ->  ErrorValue  ValidationError  ->  Html  msg 
241- error  func err = 
242-  div [   class " invalid-feedback"   ]   [   text ( func err)  ] 
243- 244- 245- getFieldAsBool  :  Path  ->  F .Form  e  o  ->  F .FieldState  e  Bool 
246- getFieldAsBool  path = 
247-  F . getFieldAsBool ( fieldPath path) 
248- 249- 250- getFieldAsString  :  Path  ->  F .Form  e  o  ->  F .FieldState  e  String 
251- getFieldAsString  path = 
252-  F . getFieldAsString ( fieldPath path) 
253- 254- 255- fieldPath  :  Path  ->  String 
256- fieldPath  = 
257-  String . join " ." 
34+  Schema . Form . schemaView []   state. schema state. form
0 commit comments