-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelpers.elm
469 lines (399 loc) · 13.9 KB
/
Helpers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
module Json.Schema.Helpers exposing
( ImpliedType
, collectIds
, typeToList
--, implyType
--, for
, typeToString
, whenObjectSchema
--, makeJsonPointer
-- , resolve
--, resolveReference
--, calcSubSchemaType
)
import Dict
import Json.Decode as Decode
import Json.Encode as Encode
import Json.Schema.Definitions as Schema
exposing
( Items(..)
, Schema(..)
, Schemata(..)
, SingleType(..)
, SubSchema
, Type(..)
)
import Ref exposing (SchemataPool, parseJsonPointer)
type alias ImpliedType =
{ type_ : Type
, schema : SubSchema
, error : Maybe String
}
singleTypeToString : SingleType -> String
singleTypeToString st =
case st of
StringType ->
"string"
IntegerType ->
"integer"
NumberType ->
"number"
BooleanType ->
"boolean"
ObjectType ->
"object"
ArrayType ->
"array"
NullType ->
"null"
typeToString : Type -> String
typeToString t =
case t of
NullableType NullType ->
"null"
NullableType st ->
"nullable " ++ singleTypeToString st
SingleType s ->
singleTypeToString s
UnionType l ->
l
|> List.map singleTypeToString
|> String.join ", "
AnyType ->
"any"
typeToList : Type -> List String
typeToList t =
case t of
NullableType NullType ->
[ "null" ]
NullableType st ->
[ "nullable " ++ singleTypeToString st ]
SingleType s ->
[ singleTypeToString s ]
UnionType l ->
l
|> List.map singleTypeToString
AnyType ->
[]
whenObjectSchema : Schema -> Maybe SubSchema
whenObjectSchema schema =
case schema of
ObjectSchema os ->
Just os
BooleanSchema _ ->
Nothing
makeJsonPointer : ( Bool, String, List String ) -> String
makeJsonPointer ( isPointer, ns, path ) =
if isPointer then
("#" :: path)
|> String.join "/"
|> (++) ns
else if List.isEmpty path then
ns
else
path
|> String.join "/"
|> (++) (ns ++ "#")
{-
for : String -> Schema -> Maybe Schema
for jsonPointer schema =
jsonPointer
|> parseJsonPointer
|> List.foldl (weNeedToGoDeeper schema) (Just schema)
implyType : Value -> Schema -> String -> ImpliedType
implyType val schema subpath =
let
path =
parseJsonPointer subpath
actualValue =
val
|> Decode.decodeValue (Decode.at path Decode.value)
|> Result.toMaybe
in
path
|> List.foldl (weNeedToGoDeeper schema) (Just schema)
|> Maybe.andThen whenObjectSchema
|> Maybe.andThen (calcSubSchemaType actualValue schema)
|> \x ->
case x of
Nothing ->
{ type_ = AnyType
, schema = blankSubSchema
, error = Just <| "Can't imply type: " ++ subpath
}
Just ( t, os ) ->
{ type_ = t
, schema = os
, error = Nothing
}
-}
{-
calcSubSchemaType : Maybe Value -> Schema -> SubSchema -> Maybe ( Type, SubSchema )
calcSubSchemaType actualValue schema os =
(case os.ref of
Just ref ->
ref
|> resolveReference schema
|> Maybe.andThen whenObjectSchema
Nothing ->
Just os
)
|> Maybe.andThen
(\os ->
case os.type_ of
AnyType ->
[ os.anyOf
, os.allOf
, os.oneOf
]
|> List.map (Maybe.withDefault [])
|> List.concat
|> tryAllSchemas actualValue schema
|> \res ->
if res == Nothing then
if os.properties /= Nothing || os.additionalProperties /= Nothing then
Just ( SingleType ObjectType, os )
else if os.enum /= Nothing then
os.enum
|> deriveTypeFromEnum
|> \t -> Just ( t, os )
else if os == blankSubSchema then
Just ( AnyType, os )
else
Nothing
else
res
UnionType ut ->
if ut == [ BooleanType, ObjectType ] || ut == [ ObjectType, BooleanType ] then
Just ( SingleType ObjectType, os )
else
Just ( os.type_, os )
x ->
Just ( x, os )
)
deriveTypeFromValue : Value -> Maybe Type
deriveTypeFromValue val =
case Decode.decodeValue Decode.string val of
Ok _ ->
Just <| SingleType StringType
Err _ ->
Nothing
deriveTypeFromEnum : Maybe (List Value) -> Type
deriveTypeFromEnum enum =
enum
|> Maybe.andThen List.head
|> Maybe.andThen deriveTypeFromValue
|> Maybe.withDefault AnyType
-}
{-
resolve : Schema -> Schema -> Schema
resolve rootSchema schema =
schema
|> whenObjectSchema
|> Maybe.andThen
(\os ->
os.ref
|> Maybe.andThen (resolveReference "" rootSchema)
)
|> Maybe.withDefault schema
-}
{-
weNeedToGoDeeper : Schema -> String -> Maybe Schema -> Maybe Schema
weNeedToGoDeeper rootSchema key schema =
schema
|> Maybe.andThen whenObjectSchema
|> Maybe.andThen
(\os ->
case os.ref of
Just r ->
resolveReference rootSchema r
Nothing ->
schema
)
|> Maybe.andThen (findProperty key rootSchema)
|> Maybe.map (resolve rootSchema)
findProperty : String -> Schema -> Schema -> Maybe Schema
findProperty name rootSchema schema =
let
os =
whenObjectSchema schema
in
os
|> Maybe.andThen .properties
|> Maybe.andThen
(\(Schemata pp) ->
pp
|> List.foldl
(\( key, s ) res ->
if res /= Nothing || key /= name then
res
else
Just s
)
Nothing
)
|> (\r ->
if r == Nothing then
os
|> Maybe.andThen .additionalProperties
else
r
)
|> (\r ->
if r == Nothing then
os
|> Maybe.andThen .anyOf
|> Maybe.andThen
(\anyOf ->
anyOf
|> List.foldl
(\s r ->
if r == Nothing then
s
|> resolve rootSchema
|> findProperty name rootSchema
else
r
)
Nothing
)
else
r
)
|> \r ->
if r == Nothing then
Just blankSchema
else
r
findDefinition : String -> Schemata -> Maybe SubSchema
findDefinition ref (Schemata defs) =
defs
|> List.foldl
(\( key, def ) res ->
if res == Nothing && ("#/definitions/" ++ key) == ref then
whenObjectSchema def
else
res
)
Nothing
tryAllSchemas : Maybe Value -> Schema -> List Schema -> Maybe ( Type, SubSchema )
tryAllSchemas actualValue rootSchema listSchemas =
listSchemas
|> List.map (resolve rootSchema)
|> List.foldl
(\schema res ->
if res == Nothing then
case actualValue of
Just av ->
case Validation.validate Ref.defaultPool av rootSchema schema of
Ok _ ->
schema
|> whenObjectSchema
|> Maybe.andThen (calcSubSchemaType actualValue rootSchema)
Err _ ->
Nothing
Nothing ->
schema
|> whenObjectSchema
|> Maybe.andThen (calcSubSchemaType actualValue rootSchema)
else
res
)
Nothing
-}
{-
getDefinition : Maybe Schemata -> String -> Maybe Schema
getDefinition defs name =
defs
|> Maybe.andThen
(\(Schemata x) ->
List.foldl
(\( key, prop ) result ->
if name == key then
Just prop
else
result
)
Nothing
x
)
-}
{-
debugSchema : String -> Maybe Schema -> Maybe Schema
debugSchema msg schema =
let
a =
case schema of
Just s ->
s
|> Schema.encode
|> Encode.encode 4
|> Debug.log
|> (\f -> f msg)
Nothing ->
Debug.log msg "Nothing"
in
schema
debugSubSchema : String -> Maybe SubSchema -> Maybe SubSchema
debugSubSchema msg schema =
let
a =
case schema of
Just s ->
ObjectSchema s
|> Schema.encode
|> Encode.encode 4
|> Debug.log
|> (\f -> f msg)
Nothing ->
Debug.log msg "Nothing"
in
schema
-}
collectIds : Schema -> SchemataPool -> ( SchemataPool, String )
collectIds schema pool =
let
getNs : Maybe String -> String
getNs uri =
case uri of
Just s ->
let
( _, ns, _ ) =
parseJsonPointer s ""
in
ns
Nothing ->
""
manageId : String -> Encode.Value -> SchemataPool -> List ( String, Encode.Value ) -> ( List ( String, Encode.Value ), ( SchemataPool, String ) )
manageId ns source poolLocal obj =
case List.filter (\( name, _ ) -> name == "id" || name == "$id") obj of
( _, val ) :: _ ->
val
|> Decode.decodeValue Decode.string
|> Result.map
(\id ->
let
( isPointer, newNs, path ) =
parseJsonPointer id ns
in
case Decode.decodeValue Schema.decoder source of
Ok schemaLocal ->
( obj, ( Dict.insert (makeJsonPointer ( isPointer, newNs, path )) schemaLocal poolLocal, newNs ) )
Err _ ->
( obj, ( poolLocal, ns ) )
)
|> Result.withDefault ( obj, ( poolLocal, ns ) )
_ ->
( obj, ( poolLocal, ns ) )
walkValue source ( poolLocal, ns ) =
source
|> Decode.decodeValue (Decode.keyValuePairs Decode.value)
|> Result.withDefault []
|> manageId ns source poolLocal
|> (\( list, res ) -> List.foldl (\( _, val ) -> walkValue val) res list)
in
case schema of
ObjectSchema { id, source } ->
walkValue source ( pool, getNs id )
_ ->
( pool, "" )