1
1
module Form.Validation exposing (validate )
2
2
3
- import Form.Error as Error exposing (ErrorValue (..) )
3
+ import Dict
4
+ import Form.Error as Error exposing (ErrorValue (..) , TextFormat (..) )
4
5
import Form.Normalization exposing (normalizeValue )
5
6
import Form.Regex
7
+ import Form.State exposing (Settings )
6
8
import Json.Decode as Decode exposing (Value )
7
9
import Json.Encode as Encode
8
10
import Json.Schema.Definitions
@@ -19,17 +21,17 @@ import Set
19
21
import Validation exposing (Validation , error )
20
22
21
23
22
- validate : Schema -> Value -> Validation Value
23
- validate schema rawValue =
24
+ validate : Settings -> Schema -> Value -> Validation Value
25
+ validate settings schema rawValue =
24
26
let
25
27
value =
26
28
normalizeValue rawValue
27
29
in
28
- Validation . voidRight value <| validateSchema schema value
30
+ Validation . voidRight value <| validateSchema settings schema value
29
31
30
32
31
- validateSchema : Schema -> Value -> Validation Value
32
- validateSchema schema rawValue =
33
+ validateSchema : Settings -> Schema -> Value -> Validation Value
34
+ validateSchema settings schema rawValue =
33
35
let
34
36
value =
35
37
normalizeValue rawValue
@@ -44,29 +46,29 @@ validateSchema schema rawValue =
44
46
Validation . fail ( error <| Unimplemented " Boolean schemas are not implemented." )
45
47
46
48
ObjectSchema objectSchema ->
47
- validateSubSchema objectSchema value
49
+ validateSubSchema settings objectSchema value
48
50
49
51
50
- validateSubSchema : SubSchema -> Value -> Validation Value
51
- validateSubSchema schema =
52
+ validateSubSchema : Settings -> SubSchema -> Value -> Validation Value
53
+ validateSubSchema settings schema =
52
54
let
53
55
typeValidations : Value -> Validation Value
54
56
typeValidations =
55
57
case schema. type_ of
56
58
SingleType type_ ->
57
- validateSingleType schema type_
59
+ validateSingleType settings schema type_
58
60
59
61
AnyType ->
60
62
Validation . succeed
61
63
62
64
NullableType type_ ->
63
65
Validation . oneOf
64
66
[ \ v -> Result . map ( always Encode . null) <| validateNull v
65
- , validateSingleType schema type_
67
+ , validateSingleType settings schema type_
66
68
]
67
69
68
70
UnionType types ->
69
- Validation . oneOf <| List . map ( \ type_ -> validateSingleType schema type_) types
71
+ Validation . oneOf <| List . map ( \ type_ -> validateSingleType settings schema type_) types
70
72
in
71
73
Validation . validateAll
72
74
[ Validation . whenJust schema. const validateConst
@@ -75,8 +77,8 @@ validateSubSchema schema =
75
77
]
76
78
77
79
78
- validateSingleType : SubSchema -> SingleType -> Value -> Validation Value
79
- validateSingleType schema type_ value =
80
+ validateSingleType : Settings -> SubSchema -> SingleType -> Value -> Validation Value
81
+ validateSingleType settings schema type_ value =
80
82
case type_ of
81
83
ObjectType ->
82
84
let
@@ -99,7 +101,7 @@ validateSingleType schema type_ value =
99
101
Ok Encode . null
100
102
101
103
( Just val, _ ) ->
102
- validateSchema propSchema val
104
+ validateSchema settings propSchema val
103
105
in
104
106
Validation . validateAll ( List . map ( \ ( key, propSchema ) _ -> validateKey key propSchema) propList) value
105
107
@@ -113,7 +115,7 @@ validateSingleType schema type_ value =
113
115
Result . map Encode . bool <| validateBool value
114
116
115
117
StringType ->
116
- Result . map Encode . string <| validateString schema value
118
+ Result . map Encode . string <| validateString settings schema value
117
119
118
120
NullType ->
119
121
Result . map ( always Encode . null) <| validateNull value
@@ -122,8 +124,8 @@ validateSingleType schema type_ value =
122
124
Err <| error ( Error . Unimplemented " array" )
123
125
124
126
125
- validateString : SubSchema -> Value -> Validation String
126
- validateString schema v =
127
+ validateString : Settings -> SubSchema -> Value -> Validation String
128
+ validateString settings schema v =
127
129
case Decode . decodeValue Decode . string v of
128
130
Err _ ->
129
131
Err <| error Error . InvalidString
@@ -133,31 +135,37 @@ validateString schema v =
133
135
[ Validation . whenJust schema. minLength validateMinLength
134
136
, Validation . whenJust schema. maxLength validateMaxLength
135
137
, Validation . whenJust schema. pattern validatePattern -- TODO: check specs if this is correct
136
- , Validation . whenJust schema. format validateFormat -- TODO: check specs if this is correct
138
+ , Validation . whenJust schema. format ( validateFormat settings ) -- TODO: check specs if this is correct
137
139
]
138
140
s
139
141
140
142
141
- validateFormat : String -> String -> Validation String
142
- validateFormat format v =
143
+ validateFormat : Settings -> String -> String -> Validation String
144
+ validateFormat settings format value =
143
145
case format of
144
146
" date-time" ->
145
- validateRegex Form . Regex . dateTime Error . DateTime v
147
+ validateRegex Form . Regex . dateTime Error . DateTime value
146
148
147
149
" date" ->
148
- validateRegex Form . Regex . date Error . Date v
150
+ validateRegex Form . Regex . date Error . Date value
149
151
150
152
" time" ->
151
- validateRegex Form . Regex . time Error . Time v
153
+ validateRegex Form . Regex . time Error . Time value
152
154
153
155
" email" ->
154
- validateRegex Form . Regex . email Error . Email v
156
+ validateRegex Form . Regex . email Error . Email value
155
157
156
158
" phone" ->
157
- validateRegex Form . Regex . phone Error . Phone v
159
+ validateRegex Form . Regex . phone Error . Phone value
158
160
159
- _ ->
160
- Validation . succeed v
161
+ customFormat ->
162
+ let
163
+ customValidation =
164
+ Dict . get customFormat settings. customFormats
165
+ |> Maybe . map ( \ validation -> validation value)
166
+ |> Maybe . withDefault ( Result . Ok value)
167
+ in
168
+ Result . mapError ( \ _ -> error ( Error . InvalidCustomFormat customFormat)) customValidation
161
169
162
170
163
171
validatePattern : String -> String -> Validation String
0 commit comments