-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da609d0
commit 4f7c34c
Showing
7 changed files
with
422 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/FSharp.Data.GraphQL.Tests/Variables and Inputs/CoercionTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...FSharp.Data.GraphQL.Tests/Variables and Inputs/OptionalsNormalizationTests.ValidString.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// The MIT License (MIT) | ||
|
||
namespace FSharp.Data.GraphQL.Tests.OptionalsNormalizationTests | ||
|
||
open System | ||
open FSharp.Data.GraphQL | ||
|
||
[<Struct>] | ||
type ValidString<'t> = internal ValidString of string | ||
with | ||
static member internal CreateVOption<'t> (value: string option) : ValidString<'t> voption = | ||
value |> ValueOption.ofOption |> ValueOption.map ValidString | ||
|
||
module ValidString = | ||
|
||
let value (ValidString text) = text | ||
|
||
let vOption strOption : string voption = strOption |> ValueOption.map value | ||
|
||
let vOptionValue strOption : string = strOption |> ValueOption.map value |> ValueOption.toObj | ||
|
||
open Validus | ||
open Validus.Operators | ||
|
||
module String = | ||
|
||
let notStartsWithWhiteSpace fieldName (s: string) = | ||
if s.StartsWith ' ' then | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot start with whitespace" ] | ||
else | ||
Ok <| s | ||
|
||
let notEndsWithWhiteSpace fieldName (s: string) = | ||
if s.EndsWith ' ' then | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot end with whitespace" ] | ||
else | ||
Ok <| s | ||
|
||
let notContainsWhiteSpace fieldName (s: string) = | ||
if s.Contains ' ' then | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain whitespace" ] | ||
else | ||
Ok <| s | ||
|
||
let notContainsBacktick fieldName (s: string) = | ||
if s.Contains '`' then | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain backtick" ] | ||
else | ||
Ok <| s | ||
|
||
let notContainsTilde fieldName (s: string) = | ||
if s.Contains '~' then | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain tilde" ] | ||
else | ||
Ok <| s | ||
|
||
let notContainsDash fieldName (s: string) = | ||
if s.Contains '-' then | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field cannot contain dash: '-'" ] | ||
else | ||
Ok <| s | ||
|
||
let notContainsUmlauts fieldName (s: string) = | ||
let umlauts = [ 'ä'; 'ö'; 'ü'; 'ß'; 'Ä'; 'Ö'; 'Ü' ] |> set | ||
|
||
let contains = s |> Seq.exists (fun c -> umlauts |> Set.contains c) | ||
|
||
if contains then | ||
Error | ||
<| ValidationErrors.create fieldName [ | ||
$"'%s{fieldName}' field cannot contain umlauts: ä, ö, ü, ß, Ä, Ö, Ü" | ||
] | ||
else | ||
Ok <| s | ||
|
||
open Validus.Operators | ||
|
||
let allowEmpty = ValueOption.ofObj >> ValueOption.filter (not << String.IsNullOrWhiteSpace) | ||
|
||
let validateStringCharacters = | ||
notStartsWithWhiteSpace | ||
<+> notEndsWithWhiteSpace | ||
<+> notContainsTilde | ||
<+> notContainsUmlauts | ||
<+> notContainsBacktick | ||
|
||
module Uri = | ||
|
||
let isValid fieldName uriString = | ||
if Uri.IsWellFormedUriString(uriString, UriKind.Absolute) then | ||
Ok uriString | ||
else | ||
Error | ||
<| ValidationErrors.create fieldName [ $"'%s{fieldName}' field is not a valid URI" ] | ||
|
||
|
||
//module VOptionString = | ||
|
||
// let allow (validator : Validator<string, string>) : Validator<string voption, string voption> = | ||
// fun fieldName (value : string voption) -> | ||
// match value with | ||
// | ValueNone -> Ok ValueNone | ||
// | ValueSome str -> (validator *|* ValueSome) fieldName str | ||
|
||
// let toValidationResult _ value : ValidationResult<string voption> = | ||
// let valueOption = | ||
// value | ||
// |> ValueOption.ofObj | ||
// |> ValueOption.filter (not << String.IsNullOrWhiteSpace) | ||
// match valueOption with | ||
// | ValueSome str -> ValueSome str |> Ok | ||
// | ValueNone -> ValueNone |> Ok | ||
|
||
module ValidationErrors = | ||
|
||
let toIGQLErrors (errors: ValidationErrors) : IGQLError list = | ||
errors | ||
|> ValidationErrors.toList | ||
|> List.map (fun e -> { new IGQLError with member _.Message = e }) | ||
|
||
module Operators = | ||
|
||
let vOption (v1: 'a -> 'a voption) (v2: Validator<'a, 'b>) : Validator<'a, 'b voption> = | ||
fun x y -> | ||
let value = v1 y | ||
match value with | ||
| ValueSome value -> (v2 *|* ValueSome) x y | ||
| ValueNone -> Ok ValueNone | ||
|
||
let (?=>) v1 v2 = vOption v1 v2 | ||
let (?=<) v2 v1 = vOption v1 v2 | ||
|
||
module Scalars = | ||
|
||
open System.Text.Json | ||
open FSharp.Data.GraphQL.Ast | ||
open FSharp.Data.GraphQL.Types | ||
open FSharp.Data.GraphQL.Types.SchemaDefinitions.Errors | ||
|
||
type Define with | ||
|
||
static member ValidStringScalar<'t>(typeName, createValid : Validator<string, 't voption>, ?description: string) = | ||
let createValid = createValid typeName | ||
Define.WrappedScalar | ||
(name = typeName, | ||
coerceInput = | ||
(function | ||
| Variable e when e.ValueKind = JsonValueKind.String -> e.GetString() |> createValid |> Result.mapError ValidationErrors.toIGQLErrors | ||
| InlineConstant (StringValue s) -> s |> createValid |> Result.mapError ValidationErrors.toIGQLErrors | ||
| Variable e -> e.GetDeserializeError typeName | ||
| InlineConstant value -> value.GetCoerceError typeName), | ||
coerceOutput = | ||
(function | ||
| :? ('t voption) as x -> x |> string |> Some | ||
| :? 't as x -> Some (string x) | ||
| :? string as s -> s |> Some | ||
| null -> None | ||
| _ -> raise <| System.NotSupportedException ()), | ||
?description = description) |
Oops, something went wrong.