Skip to content

Commit 923b923

Browse files
committed
Handle numbers passed in as strings
1 parent 648121b commit 923b923

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/SchemaUtils.elm

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,14 +2088,19 @@ typeToAnnotationWithMaybe type_ =
20882088
CliMonad.succeed Elm.Annotation.unit
20892089

20902090

2091-
basicTypeToAnnotation : Common.BasicType -> { a | format : Maybe String } -> CliMonad Elm.Annotation.Annotation
2092-
basicTypeToAnnotation basicType { format } =
2091+
basicTypeToAnnotation : Common.BasicType -> { a | format : Maybe String, pattern : Maybe String } -> CliMonad Elm.Annotation.Annotation
2092+
basicTypeToAnnotation basicType { format, pattern } =
20932093
let
20942094
default : Elm.Annotation.Annotation
20952095
default =
20962096
case basicType of
20972097
Common.String ->
2098-
Elm.Annotation.string
2098+
case pattern of
2099+
Just "^\\d+$" ->
2100+
Elm.Annotation.int
2101+
2102+
_ ->
2103+
Elm.Annotation.string
20992104

21002105
Common.Integer ->
21012106
Elm.Annotation.int
@@ -2375,14 +2380,19 @@ typeToEncoder type_ =
23752380
CliMonad.succeed (\_ -> Gen.Json.Encode.null)
23762381

23772382

2378-
basicTypeToEncoder : Common.BasicType -> { a | format : Maybe String } -> CliMonad (Elm.Expression -> Elm.Expression)
2379-
basicTypeToEncoder basicType { format } =
2383+
basicTypeToEncoder : Common.BasicType -> { a | format : Maybe String, pattern : Maybe String } -> CliMonad (Elm.Expression -> Elm.Expression)
2384+
basicTypeToEncoder basicType { format, pattern } =
23802385
let
23812386
default : Elm.Expression -> Elm.Expression
23822387
default =
23832388
case basicType of
23842389
Common.String ->
2385-
Gen.Json.Encode.call_.string
2390+
case pattern of
2391+
Just "^\\d+$" ->
2392+
\v -> v |> Gen.String.call_.fromInt |> Gen.Json.Encode.call_.string
2393+
2394+
_ ->
2395+
Gen.Json.Encode.call_.string
23862396

23872397
Common.Integer ->
23882398
Gen.Json.Encode.call_.int
@@ -2762,7 +2772,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
27622772
(Elm.string
27632773
("Unexpected value: expected "
27642774
++ constToString expected
2765-
++ " got "
2775+
++ ", got "
27662776
)
27672777
)
27682778
(toString actual)
@@ -2776,6 +2786,24 @@ basicTypeToDecoder basicType { format, const, pattern } =
27762786
case basicType of
27772787
Common.String ->
27782788
case ( const, pattern ) of
2789+
( Nothing, Just "^\\d+$" ) ->
2790+
Gen.Json.Decode.string
2791+
|> Gen.Json.Decode.andThen
2792+
(\actual ->
2793+
Elm.Case.maybe (Gen.String.call_.toInt actual)
2794+
{ nothing =
2795+
Gen.Json.Decode.call_.fail
2796+
(Elm.Op.append
2797+
(Elm.string
2798+
"Unexpected value: expected a string matching \"^\\\\d+$\", got "
2799+
)
2800+
(Gen.Json.Encode.encode 0 (Gen.Json.Encode.call_.string actual))
2801+
)
2802+
, just = ( "i", \i -> Gen.Json.Decode.succeed i )
2803+
}
2804+
)
2805+
|> CliMonad.succeed
2806+
27792807
( Nothing, Just p ) ->
27802808
Elm.Let.letIn identity
27812809
|> Elm.Let.value "regex"
@@ -2793,7 +2821,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
27932821
(Elm.string
27942822
("Unexpected value: expected a string matching "
27952823
++ Json.Encode.encode 0 (Json.Encode.string p)
2796-
++ " got "
2824+
++ ", got "
27972825
)
27982826
)
27992827
(Gen.Json.Encode.encode 0 (Gen.Json.Encode.call_.string actual))
@@ -2851,7 +2879,7 @@ basicTypeToDecoder basicType { format, const, pattern } =
28512879
(Elm.string
28522880
("Unexpected value: expected "
28532881
++ boolToString expected
2854-
++ " got "
2882+
++ ", got "
28552883
++ boolToString (not expected)
28562884
)
28572885
)

0 commit comments

Comments
 (0)