-
Notifications
You must be signed in to change notification settings - Fork 369
Issue-452 : Add support for annotation based input validation #903
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
base: develop
Are you sure you want to change the base?
Changes from all commits
13bb087
a5054c2
7465eab
cec4eb7
9cf48c4
880347c
3a75f7d
602472c
00160db
50f8fe8
e7c61c4
7f0713b
794119b
4a29156
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.owasp.esapi.reference.validation.annotations; | ||
|
|
||
| import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
| import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import javax.validation.Constraint; | ||
| import javax.validation.Payload; | ||
|
|
||
|
|
||
| @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) | ||
| @Retention(RUNTIME) | ||
| @Constraint(validatedBy = ValidCreditCardValidator.class) | ||
| @Documented | ||
| public @interface ValidCreditCard { | ||
|
|
||
| String message() default ""; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
|
|
||
| String context(); | ||
|
|
||
| boolean allowNull(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||
| package org.owasp.esapi.reference.validation.annotations; | ||||||
|
|
||||||
| import javax.validation.ConstraintValidator; | ||||||
| import javax.validation.ConstraintValidatorContext; | ||||||
|
|
||||||
| import org.owasp.esapi.ValidationErrorList; | ||||||
| import org.owasp.esapi.ESAPI; | ||||||
|
|
||||||
|
|
||||||
| public class ValidCreditCardValidator implements ConstraintValidator<ValidCreditCard, String>{ | ||||||
|
|
||||||
| private String context; | ||||||
| private boolean allowNull; | ||||||
|
|
||||||
| @Override | ||||||
| public void initialize(ValidCreditCard validCreditCard) { | ||||||
| context = validCreditCard.context(); | ||||||
| allowNull = validCreditCard.allowNull(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean isValid(String input, ConstraintValidatorContext constraintValidatorContext) { | ||||||
| if (input == null) { | ||||||
| return true; | ||||||
|
||||||
| return true; | |
| return allowNull; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.owasp.esapi.reference.validation.annotations; | ||
|
|
||
| import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
| import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import javax.validation.Constraint; | ||
| import javax.validation.Payload; | ||
|
|
||
|
|
||
| @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) | ||
| @Retention(RUNTIME) | ||
| @Constraint(validatedBy = ValidDateValidator.class) | ||
| @Documented | ||
| public @interface ValidDate { | ||
|
|
||
| String message() default ""; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
|
|
||
| String context(); | ||
|
|
||
| //Invalid type DateFormat for the annotation attribute ValidDate.format; | ||
| //only primitive type, String, Class, annotation, enumeration are permitted or 1-dimensional arrays thereof | ||
| //DateFormat format(); | ||
|
|
||
| int dateStyle(); | ||
|
|
||
| String locale(); | ||
|
|
||
| boolean allowNull(); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||
| package org.owasp.esapi.reference.validation.annotations; | ||||||
|
|
||||||
| import java.text.DateFormat; | ||||||
| import java.util.Locale; | ||||||
|
|
||||||
| import javax.validation.ConstraintValidator; | ||||||
| import javax.validation.ConstraintValidatorContext; | ||||||
|
|
||||||
| import org.owasp.esapi.ValidationErrorList; | ||||||
| import org.owasp.esapi.ESAPI; | ||||||
|
|
||||||
|
|
||||||
| public class ValidDateValidator implements ConstraintValidator<ValidDate, String>{ | ||||||
|
|
||||||
| private String context; | ||||||
| private int dateStyle; | ||||||
| private String localeString; | ||||||
| private boolean allowNull; | ||||||
|
|
||||||
| @Override | ||||||
| public void initialize(ValidDate validDate) { | ||||||
| context = validDate.context(); | ||||||
| dateStyle = validDate.dateStyle(); | ||||||
| localeString = validDate.locale(); | ||||||
| allowNull = validDate.allowNull(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean isValid(String input, ConstraintValidatorContext constraintValidatorContext) { | ||||||
| if (input == null) { | ||||||
| return true; | ||||||
|
||||||
| return true; | |
| return allowNull; |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fallback to Locale.getDefault() when Locale.ROOT is returned may not work as intended. Locale.forLanguageTag() returns Locale.ROOT when the tag is unrecognized or invalid, not when it should return the default locale. This means invalid locale strings like "invalid-locale" will incorrectly fall back to the system default instead of being treated as an error. Consider validating the locale string format before parsing or documenting this fallback behavior.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package org.owasp.esapi.reference.validation.annotations; | ||
|
|
||
| import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
| import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import javax.validation.Constraint; | ||
| import javax.validation.Payload; | ||
|
|
||
|
|
||
| @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) | ||
| @Retention(RUNTIME) | ||
| @Constraint(validatedBy = ValidDirectoryPathValidator.class) | ||
| @Documented | ||
| public @interface ValidDirectoryPath { | ||
|
|
||
| String message() default ""; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
|
|
||
| String context(); | ||
|
|
||
| String parent(); | ||
|
|
||
| boolean allowNull(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||
| package org.owasp.esapi.reference.validation.annotations; | ||||||
|
|
||||||
| import java.io.File; | ||||||
|
|
||||||
| import javax.validation.ConstraintValidator; | ||||||
| import javax.validation.ConstraintValidatorContext; | ||||||
|
|
||||||
| import org.owasp.esapi.ValidationErrorList; | ||||||
| import org.owasp.esapi.ESAPI; | ||||||
|
|
||||||
|
|
||||||
| public class ValidDirectoryPathValidator implements ConstraintValidator<ValidDirectoryPath, String>{ | ||||||
|
|
||||||
| private String context; | ||||||
| private String parentString; | ||||||
| private boolean allowNull; | ||||||
|
|
||||||
| @Override | ||||||
| public void initialize(ValidDirectoryPath validDirectoryPath) { | ||||||
| context = validDirectoryPath.context(); | ||||||
| parentString = validDirectoryPath.parent(); | ||||||
| allowNull = validDirectoryPath.allowNull(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean isValid(String input, ConstraintValidatorContext constraintValidatorContext) { | ||||||
| if (input == null) { | ||||||
| return true; | ||||||
|
||||||
| return true; | |
| return allowNull; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.owasp.esapi.reference.validation.annotations; | ||
|
|
||
| import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
| import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import javax.validation.Constraint; | ||
| import javax.validation.Payload; | ||
|
|
||
|
|
||
| @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) | ||
| @Retention(RUNTIME) | ||
| @Constraint(validatedBy = ValidDoubleValidator.class) | ||
| @Documented | ||
| public @interface ValidDouble { | ||
|
|
||
| String message() default ""; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
|
|
||
| String context(); | ||
|
|
||
| double minValue(); | ||
|
|
||
| double maxValue(); | ||
|
|
||
| boolean allowNull(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||
| package org.owasp.esapi.reference.validation.annotations; | ||||||
|
|
||||||
| import javax.validation.ConstraintValidator; | ||||||
| import javax.validation.ConstraintValidatorContext; | ||||||
|
|
||||||
| import org.owasp.esapi.ValidationErrorList; | ||||||
| import org.owasp.esapi.ESAPI; | ||||||
|
|
||||||
|
|
||||||
| public class ValidDoubleValidator implements ConstraintValidator<ValidDouble, String>{ | ||||||
|
|
||||||
| private String context; | ||||||
| private double minValue; | ||||||
| private double maxValue; | ||||||
| private boolean allowNull; | ||||||
|
|
||||||
| @Override | ||||||
| public void initialize(ValidDouble validDouble) { | ||||||
| context = validDouble.context(); | ||||||
| minValue = validDouble.minValue(); | ||||||
| maxValue = validDouble.maxValue(); | ||||||
| allowNull = validDouble.allowNull(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean isValid(String input, ConstraintValidatorContext constraintValidatorContext) { | ||||||
| if (input == null) { | ||||||
| return true; | ||||||
|
||||||
| return true; | |
| return allowNull; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing empty line at the end of the file should be removed for consistency with Java formatting conventions.