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

Implement constraints using Comparable interface #181

Merged
merged 5 commits into from
Nov 25, 2024

Conversation

pmeinhardt
Copy link
Contributor

Allow building the constraints

  • maximum and exclusiveMaximum,
  • minimum and exclusiveMinimum

for any type T, using a Comparable<T>.

Previously, these constraints were only available for subtypes of Number and relied on converting to Double for comparing numbers of different types. This could cause issues when comparing values which cannot be cleanly converted from the original number type.

For compatibility and convenience, we provide overloads for these constraints that accept an Int as the limit value as this seems like a rather common case.

Closes #180

@pmeinhardt
Copy link
Contributor Author

pmeinhardt commented Nov 23, 2024

Here's a first draft @dhoepelman; I have included your feedback on the issue.

I still need to take care of the deprecated constraints from JsonSchema.

@pmeinhardt pmeinhardt force-pushed the comparable-constraints branch 2 times, most recently from 36127a2 to f919d00 Compare November 23, 2024 18:41
@pmeinhardt
Copy link
Contributor Author

Alright. I just brought back the original implementations of the now deprecated maximum, minimum etc. constraints from io.konform.validation.jsonschema.

@pmeinhardt pmeinhardt marked this pull request as ready for review November 23, 2024 18:46
@@ -13,26 +14,34 @@ public fun <T : Number> ValidationBuilder<T>.multipleOf(factor: Number): Constra
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

Can you add overloads for Long too? Float seems like a more niche use-case so feel free to remove, keeping them is fine now you made them.

Suggested change
// The below functions are overloads of the Comparable<T> functions, provided for backwards
// compatibility and convenience for the common case where a number is constrained by an Int

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you add overloads for Long too? Float seems like a more niche use-case so feel free to remove, keeping them is fine now you made them.

Kotlin automatically converts the literal to a Long value depending on the context:

assertEquals(
Valid<Long>(10),
Validation<Long> { maximum(10) }(10)
)

That's why I was thinking an explicit overload might not be needed. I haven't tried with a value explicitly typed as Int though. I will check that and can certainly add them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah I see, looked at the widening rules but integer literal to long does happen automatically, so shouldn't be needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sorry. Saw your message a moment too late and just pushed an update adding the overload for Long. 🙈

I checked the case where the limit value is explicitly typed, e.g., when is provided using a constant value from elsewhere:

val MAX: Int = 100
val validation = Validation<Long> {
    maximum(MAX)
}

In this case the overload still might be useful.

@dhoepelman
Copy link
Collaborator

Thanks! Looks good at a quick glance.

Will find some time soon to do full review and merge.

Copy link
Collaborator

@dhoepelman dhoepelman left a comment

Choose a reason for hiding this comment

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

Thanks!

@dhoepelman dhoepelman enabled auto-merge (squash) November 25, 2024 08:11
Allow building the constraints

- `maximum` and `exclusiveMaximum`,
- `minimum` and `exclusiveMinimum`

for any type `T`, using a `Comparable<T>`.

Previously, these constraints were only available for subtypes of
`Number` and relied on converting to `Double` for comparing numbers of
different types. This could cause issues when comparing values which
cannot be cleanly converted from the original number type.

For compatibility and convenience, we provide overloads for these
constraints that accept an `Int` as the limit value as this seems like
a rather common case.

Closes konform-kt#180
auto-merge was automatically disabled November 25, 2024 10:14

Head branch was pushed to by a user without write access

@dhoepelman
Copy link
Collaborator

dhoepelman commented Nov 25, 2024

@pmeinhardt Can you either restore or incorporate in your own commits the fixes from

620aba5
97511cd
abec3a0 (gradle apiDump)

@pmeinhardt
Copy link
Contributor Author

@pmeinhardt Can you either restore or incorporate in your own commits the fixes from

620aba5 97511cd abec3a0 (gradle apiDump)

You mean cherry-pick them here? Sure thing. 🏃

Authored-by: David Hoepelman <[email protected]>
Authored-by: David Hoepelman <[email protected]>
@pmeinhardt
Copy link
Contributor Author

pmeinhardt commented Nov 25, 2024

D'uh. Had to apply them as patches, as Git would not fetch those commits. Sorry about that and thanks a ton for the fixes. I haven't taken the time yet to set up all platforms and ran only the JVM tests locally. 🫶

@dhoepelman dhoepelman enabled auto-merge (squash) November 25, 2024 19:21
@dhoepelman dhoepelman merged commit 8d9a3e8 into konform-kt:main Nov 25, 2024
2 checks passed
@pmeinhardt pmeinhardt deleted the comparable-constraints branch November 25, 2024 19:30
@dhoepelman dhoepelman added this to the v0.11.0 milestone Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve minimum, maximum… number constraints?
2 participants