Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integral number combinator #27

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docsrc/content/api.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Where `NumberAttributes` can be any valid combination of

- `IsNonNegative` - ensure that the number is 0 or greater.

- `IsIntegral` - ensure that the number is an integral number.

- `IsGreaterThan` - ensure that the number is greater than a given number.

- `IsLessThan` - ensure that the number is less than a given number.
Expand Down
2 changes: 2 additions & 0 deletions src/FSharp.Data.JsonValidation/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module public JsonValidation =
| IsPositive
| IsNegative
| IsNonNegative
| IsIntegral
| IsGreaterThan of decimal
| IsLessThan of decimal

Expand Down Expand Up @@ -74,6 +75,7 @@ module public JsonValidation =
| num, IsPositive::rest when num > 0M -> numberMeetsProperties num rest
| num, IsNegative::rest when num < 0M -> numberMeetsProperties num rest
| num, IsNonNegative::rest when num >= 0M -> numberMeetsProperties num rest
| num, IsIntegral::rest when num % 1M = 0M -> numberMeetsProperties num rest
| num, IsGreaterThan n::rest when num > n -> numberMeetsProperties num rest
| num, IsLessThan n::rest when num < n -> numberMeetsProperties num rest
| num, prop::_ -> Invalid <| sprintf "Expected number %f to meet %A property" num prop
Expand Down
11 changes: 10 additions & 1 deletion tests/FSharp.Data.JsonValidation.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ let ``a non-negative JSON number IsNonNegative`` () =
valid <| validate (NumberThat [IsNonNegative]) (JsonValue.Number 0M)
invalid <| validate (NumberThat [IsNonNegative]) (JsonValue.Number -42M)

[<Test>]
let ``an integral JSON number IsIntegral`` () =
valid <| validate (NumberThat [IsIntegral]) (JsonValue.Number 42M)
valid <| validate (NumberThat [IsIntegral]) (JsonValue.Number 0M)
valid <| validate (NumberThat [IsIntegral]) (JsonValue.Number -42M)
invalid <| validate (NumberThat [IsIntegral]) (JsonValue.Number 42.94M)
invalid <| validate (NumberThat [IsIntegral]) (JsonValue.Number -42.94M)
valid <| validate (NumberThat [IsIntegral]) (JsonValue.Float 0.0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is interesting to note that a JsonValue.Number or JsonValue.Float ending with a .0 will validate.

I believe that this is reasonable because 42 = 42.0 mathematically speaking. I would love to hear your thoughts @mjgpy3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's totally acceptable!


[<Test>]
let ``a JSON number that is greater than another number IsGreaterThan`` () =
valid <| validate (NumberThat [IsGreaterThan 10M]) (JsonValue.Number 42M)
Expand All @@ -155,7 +164,7 @@ let ``a JSON number that is less than another number IsLessThan`` () =

[<Test>]
let ``a JSON number can have multiple NumberThat properties`` () =
valid <| validate (NumberThat [IsGreaterThan 0M; IsPositive]) (JsonValue.Number 42M)
valid <| validate (NumberThat [IsGreaterThan 0M; IsPositive; IsIntegral]) (JsonValue.Number 42M)
valid <| validate (NumberThat [IsGreaterThan 0M; IsPositive; IsNonNegative]) (JsonValue.Number 42M)
invalid <| validate (NumberThat [IsLessThan 0M; IsNegative]) (JsonValue.Number 42M)
invalid <| validate (NumberThat [IsNegative; IsPositive]) (JsonValue.Number 42M)
Expand Down