Skip to content

Add @Length as built-in validator #91

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

Merged
merged 2 commits into from
Jul 28, 2023
Merged

Add @Length as built-in validator #91

merged 2 commits into from
Jul 28, 2023

Conversation

rbygrave
Copy link
Contributor

@rbygrave rbygrave commented Jul 28, 2023

Now this is pretty much duplicate functionality of @Size so there is some argument for not adding this.

Argument for adding @Length is that:

  • @Length is what existing users of jakarta validator are expecting to be able to use
  • The error message "Length must be ..." is better for end users to understand wrt string lengths
  • The string length is probably the MOST COMMON validation requirement and as such it's important that it is done well. Although this can be done with say @Size(max=...) the error message has 'Size' vs 'Length' and maybe not ideal in that sense.

Now this is pretty much duplicate functionality of @SiZe so there is some argument for not adding this.

Argument for adding @Length is that:
 - @Length is what existing users of jakarta validator are expecting to be able to use
 - The error message "*Length* must be ..." is better for end users to understand wrt string lengths
 - The string length is probably the MOST COMMON validation requirement and as such its important that it is done well. Although this can be done with say @SiZe(max=...) the error message has 'Size' vs 'Length' and maybe not ideal in that sense.
@sonatype-lift
Copy link

sonatype-lift bot commented Jul 28, 2023

Sonatype Lift is retiring

Sonatype Lift will be retiring on Sep 12, 2023, with its analysis stopping on Aug 12, 2023. We understand that this news may come as a disappointment, and Sonatype is committed to helping you transition off it seamlessly. If you’d like to retain your data, please export your issues from the web console.
We are extremely grateful and thank you for your support over the years.

📖 Read about the impacts and timeline

@@ -112,6 +113,37 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
}
}

private static final class LengthAdapter implements ValidationAdapter<Object> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

wait why do we need a separate adapter? can we not reuse the size adapter? since the logic is the exact same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

since the logic is the exact same?

To be exactly the same, @Length would apply to Collection, Map and Array. Currently in this PR @Length only applies to CharSequence. Hmm, thinking that probably doesn't matter or could be seen as a positive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merging in by just re-using the SizeAdapter for now.

@rbygrave rbygrave merged commit b3783c1 into main Jul 28, 2023
@rbygrave rbygrave deleted the feature/Length branch July 28, 2023 11:28
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.

2 participants