-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJsonFormUtil.elm
62 lines (47 loc) · 1.48 KB
/
JsonFormUtil.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module JsonFormUtil exposing (getDescription, getTextProp, getTitle, getUiSpec, jsonValueToString)
import Json.Decode as Decode
import Json.Encode as Encode
import Json.Form.UiSpec exposing (UiSpec, decoder)
import Json.Schema.Definitions exposing (Schema(..), SubSchema, getCustomKeywordValue)
import Json.Value as JsonValue exposing (JsonValue)
jsonValueToString : JsonValue -> String
jsonValueToString jv =
case jv of
JsonValue.StringValue s ->
s
JsonValue.NumericValue n ->
String.fromFloat n
_ ->
""
getTitle : Bool -> Schema -> String
getTitle isRequired schema =
getTextProp schema .title ""
|> (\title ->
if isRequired then
title ++ " *"
else
title
)
getDescription : Schema -> String
getDescription schema =
getTextProp schema .description ""
getTextProp : Schema -> (SubSchema -> Maybe String) -> String -> String
getTextProp schema prop def =
case schema of
ObjectSchema os ->
os
|> prop
|> Maybe.withDefault def
_ ->
def
getUiSpec : Schema -> UiSpec
getUiSpec schema =
schema
|> getCustomKeywordValue "ui"
|> Maybe.andThen
(\settings ->
settings
|> Decode.decodeValue decoder
|> Result.toMaybe
)
|> Maybe.withDefault Json.Form.UiSpec.blank