Repository navigation
Expand file tree
/
Copy pathValidationTypes.fs
More file actions
69 lines (53 loc) · 2.11 KB
/
Copy pathValidationTypes.fs
File metadata and controls
69 lines (53 loc) · 2.11 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
62
63
64
65
66
67
68
69
namespace FSharp.Data.GraphQL.Validation
open System.Collections.Generic
open System.Text.Json.Serialization
open FsToolkit.ErrorHandling
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Extensions
[<Struct>]
type ValidationResult<'Err> =
| Success
| ValidationError of 'Err list
member this.AsResult : Result<unit, 'Err list> =
match this with
| Success -> Ok ()
| ValidationError errors -> Error errors
[<AutoOpen>]
module Result =
type ResultBuilder with
member inline _.Source (result : ValidationResult<'error>) = result.AsResult
module ValidationResult =
let (@@) (res1 : ValidationResult<'Err>) (res2 : ValidationResult<'Err>) : ValidationResult<'Err> =
match res1, res2 with
| Success, Success -> Success
| Success, _ -> res2
| _, Success -> res1
| ValidationError e1, ValidationError e2 -> ValidationError (e1 @ e2)
/// Call the given sequence of validations, accumulating any errors, and return one ValidationResult.
let collect (f : 'T -> ValidationResult<'Err>) (xs : 'T seq) : ValidationResult<'Err> =
// TODO: Use PSeq
Seq.fold (fun acc t -> acc @@ (f t)) Success xs
let mapErrors (f : 'Err1 -> 'Err2) (res : ValidationResult<'Err1>) : ValidationResult<'Err2> =
match res with
| Success -> Success
| ValidationError errors -> ValidationError (List.map f errors)
type internal GQLValidator<'Val> = 'Val -> ValidationResult<IGQLError>
module GQLValidator =
let empty = fun _ -> Success
[<AbstractClass; Sealed>]
type AstError =
static member AsResult (message : string, ?path : FieldPath) =
[
{
Message = message
Exception = ValueNone
Path = path |> Skippable.ofOption |> Skippable.map List.rev
Locations = Skip
Extensions =
Include (
Dictionary<string, obj> ()
|> GQLProblemDetails.SetErrorKind ErrorKind.Validation
)
}
]
|> ValidationResult.ValidationError