@@ -81,6 +81,7 @@ module String =
8181 Ok <| s
8282
8383 open Validus.Operators
84+ open System.Text .Json .Serialization
8485
8586 let allowEmpty = ValueOption.ofObj >> ValueOption.filter ( not << String.IsNullOrWhiteSpace)
8687
@@ -146,7 +147,33 @@ module Scalars =
146147
147148 type Define with
148149
149- static member ValidStringScalar < 't >( typeName , createValid : Validator < string , 't voption >, ? description : string ) =
150+ static member ValidStringScalar < 't >
151+ ( typeName , createValid : Validator < string , 't >, toString : 't -> string , ? description : string )
152+ =
153+ let createValid : string -> ValidationResult < 't > = createValid typeName
154+ Define.WrappedScalar (
155+ name = typeName,
156+ coerceInput =
157+ ( function
158+ | Variable e when e.ValueKind = JsonValueKind.String ->
159+ e.GetString ()
160+ |> createValid
161+ |> Result.mapError ValidationErrors.toIGQLErrors
162+ | InlineConstant ( StringValue s) ->
163+ s
164+ |> createValid
165+ |> Result.mapError ValidationErrors.toIGQLErrors
166+ | Variable e -> e.GetDeserializeError typeName
167+ | InlineConstant value -> value.GetCoerceError typeName),
168+ coerceOutput =
169+ ( function
170+ | :? 't as x -> Some ( toString x)
171+ | :? string as s -> s |> Some
172+ | _ -> raise <| System.NotSupportedException ()),
173+ ?description = description
174+ )
175+
176+ static member ValidStringScalar < 't >( typeName , createValid : Validator < string , 't voption >, toString : 't -> string , ? description : string ) =
150177 let createValid = createValid typeName
151178 Define.WrappedScalar
152179 ( name = typeName,
@@ -158,8 +185,8 @@ module Scalars =
158185 | InlineConstant value -> value.GetCoerceError typeName),
159186 coerceOutput =
160187 ( function
161- | :? ( 't voption) as x -> x |> string |> Some
162- | :? 't as x -> Some ( string x)
188+ | :? ( 't voption) as x -> x |> ValueOption.map toString |> ValueOption.toOption
189+ | :? 't as x -> Some ( toString x)
163190 | :? string as s -> s |> Some
164191 | null -> None
165192 | _ -> raise <| System.NotSupportedException ()),
0 commit comments