forked from TOTBWF/FSharp.Data.GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidation.fs
More file actions
61 lines (54 loc) · 2.53 KB
/
Copy pathValidation.fs
File metadata and controls
61 lines (54 loc) · 2.53 KB
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
/// The MIT License (MIT)
/// Copyright (c) 2016 Bazinga Technologies Inc
module internal FSharp.Data.GraphQL.Validation
open FSharp.Data.GraphQL.Types
open FSharp.Data.GraphQL.Types.Patterns
type ValidationResult =
| Success
| Error of string list
let (@) res1 res2 =
match res1, res2 with
| Success, Success -> Success
| Success, _ -> res2
| _, Success -> res1
| Error e1, Error e2 -> Error (e1 @ e2)
let validateImplements (objdef: ObjectDef) (idef: InterfaceDef) =
let objectFields =
objdef.Fields
let errors =
idef.Fields
|> Array.fold (fun acc f ->
match Map.tryFind f.Name objectFields with
| None -> (sprintf "'%s' field is defined by interface %s, but not implemented in object %s" f.Name idef.Name objdef.Name)::acc
| Some objf when objf = f -> acc
| Some _ -> (sprintf "'%s.%s' field signature does not match it's definition in interface %s" objdef.Name f.Name idef.Name)::acc) []
match errors with
| [] -> Success
| err -> Error err
let validateType (namedTypes: Map<string, NamedDef>) typedef =
match typedef with
| Scalar scalardef -> Success
| Object objdef ->
let nonEmptyResult = if objdef.Fields.Count > 0 then Success else Error [ objdef.Name + " must have at least one field defined" ]
let implementsResult =
objdef.Implements
|> Array.fold (fun acc i -> acc @ validateImplements objdef i) Success
nonEmptyResult @ implementsResult
| InputObject indef ->
let nonEmptyResult = if indef.Fields.Length > 0 then Success else Error [ indef.Name + " must have at least one field defined" ]
nonEmptyResult
| Union uniondef ->
let nonEmptyResult = if uniondef.Options.Length > 0 then Success else Error [ uniondef.Name + " must have at least one type definition option" ]
nonEmptyResult
| Enum enumdef ->
let nonEmptyResult = if enumdef.Options.Length > 0 then Success else Error [ enumdef.Name + " must have at least one enum value defined" ]
nonEmptyResult
| Interface idef ->
let nonEmptyResult = if idef.Fields.Length > 0 then Success else Error [ idef.Name + " must have at least one field defined" ]
nonEmptyResult
| _ -> failwithf "Unexpected value of typedef: %O" typedef
let validate (namedTypes: Map<string, NamedDef>) : ValidationResult =
namedTypes
|> Map.toArray
|> Array.map snd
|> Array.fold (fun acc namedDef -> acc @ validateType namedTypes namedDef) Success