diff --git a/CHANGELOG.md b/CHANGELOG.md index 889b8420..344844c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - reduced logging level for XmlBeanHelper.setFromElementSafe +- ValidatorDate always accepts date format overflow + ### Changed diff --git a/docs/validator/validator_basic.md b/docs/validator/validator_basic.md index 5c6cd3df..28f86415 100644 --- a/docs/validator/validator_basic.md +++ b/docs/validator/validator_basic.md @@ -31,6 +31,10 @@ Here are the [Default Validator Messages](../../fj-core/src/main/resources/core/ maxength If set checks for maximum length + + strict + If set to true, the validator will not be lenient and check for input overflow +
top
@@ -139,3 +143,21 @@ Here are the [Default Validator Messages](../../fj-core/src/main/resources/core/
top
+ + + + + + + + + + + + + + +
Entry strict
Defaultfalse
Exampletrue
Since8.7.1
+ +
top
+ diff --git a/fj-core/src/main/java/org/fugerit/java/core/validator/ValidatorDate.java b/fj-core/src/main/java/org/fugerit/java/core/validator/ValidatorDate.java index 08e55a05..45d1522d 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/validator/ValidatorDate.java +++ b/fj-core/src/main/java/org/fugerit/java/core/validator/ValidatorDate.java @@ -1,11 +1,13 @@ package org.fugerit.java.core.validator; import java.text.ParseException; +import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import org.fugerit.java.core.cfg.ConfigException; +import org.fugerit.java.core.lang.helpers.BooleanUtils; import org.fugerit.java.core.lang.helpers.StringUtils; public class ValidatorDate extends BasicValidator { @@ -20,7 +22,9 @@ public class ValidatorDate extends BasicValidator { public static final String KEY_MINDATE = "minDate"; public static final String KEY_MAXDATE = "maxDate"; - + + public static final String KEY_STRICT = "strict"; + public static final String ERROR_KEY_DATE = "error.date"; public static final String ERROR_KEY_DATE_MIN = "error.date.min"; @@ -32,7 +36,13 @@ public class ValidatorDate extends BasicValidator { private String minDate; private String maxDate; - + + private boolean strict; + + public ValidatorDate() { + this.strict = Boolean.FALSE; + } + public String getDateFormat() { return dateFormat; } @@ -45,6 +55,10 @@ public String getMaxDate() { return maxDate; } + public boolean isStrict() { + return this.strict; + } + protected Date setDate( SimpleDateFormat sdf, String d ) throws ParseException { Date res = null; if ( StringUtils.isNotEmpty( d ) ) { @@ -77,6 +91,10 @@ public void configure( Properties atts ) throws ConfigException { if ( StringUtils.isNotEmpty( maxDateLocal ) ) { this.maxDate = maxDateLocal; } + String strictLocal = atts.getProperty( KEY_STRICT ); + if ( StringUtils.isNotEmpty( strictLocal ) ) { + this.strict = BooleanUtils.isTrue( strictLocal); + } } catch (Exception e) { throw new ConfigException( e ); } @@ -93,7 +111,9 @@ protected boolean validate( ValidatorContext context, String minDate, String max boolean valid = true; try { SimpleDateFormat sdf = new SimpleDateFormat( this.getDateFormat() ); - Date d = sdf.parse( context.getValue() ); + sdf.setLenient( !this.isStrict() ); + ParsePosition pp = new ParsePosition( 0 ); + Date d = sdf.parse( context.getValue(), pp ); if ( StringUtils.isNotEmpty( minDate ) && d.before( sdf.parse( minDate ) ) ) { valid = false; String message = this.formatMessage( context.getBundle() , ERROR_KEY_DATE_MIN, context.getLabel(), context.getValue(), minDate ); @@ -104,6 +124,9 @@ protected boolean validate( ValidatorContext context, String minDate, String max String message = this.formatMessage( context.getBundle() , ERROR_KEY_DATE_MAX, context.getLabel(), context.getValue(), maxDate ); context.getResult().addError( context.getFieldId(), message ); } + if ( this.isStrict() && pp.getIndex() != context.getValue().length() ) { + throw new ParseException( context.getValue(), pp.getIndex() ); + } } catch (Exception e) { valid = false; String message = this.formatMessage( context.getBundle() , ERROR_KEY_DATE, context.getLabel(), context.getValue(), StringUtils.valueWithDefault( this.getInfo(), this.getDateFormat() ) ); diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/validator/TestValidatorCatalog.java b/fj-core/src/test/java/test/org/fugerit/java/core/validator/TestValidatorCatalog.java index c8c47e0b..1a350394 100644 --- a/fj-core/src/test/java/test/org/fugerit/java/core/validator/TestValidatorCatalog.java +++ b/fj-core/src/test/java/test/org/fugerit/java/core/validator/TestValidatorCatalog.java @@ -112,5 +112,15 @@ void testNumberValidator001_IT() { this.validatorWorker( "testNumberValidator1" , result, l, "1.000.134.234.243,123", "Invalid number 2", false, params ); this.printResult(result); } + + @Test + void testDateValidatorStrict() { + Locale l = Locale.ITALY; + ValidatorResult result = new ValidatorResult(); + Properties params = new Properties(); + this.validatorWorker( "testDateValidatorStrict" , result, l, "01/03/2021AAA", "Valid date", Boolean.FALSE, params); + this.validatorWorker( "testDateValidatorStrict" , result, l, "01/03/2021", "Valid date", Boolean.TRUE, params); + this.printResult(result); + } } diff --git a/fj-core/src/test/resources/core/validator/validator-catalog-test.xml b/fj-core/src/test/resources/core/validator/validator-catalog-test.xml index 3a18146f..422a3b6f 100644 --- a/fj-core/src/test/resources/core/validator/validator-catalog-test.xml +++ b/fj-core/src/test/resources/core/validator/validator-catalog-test.xml @@ -18,6 +18,11 @@ 31/12/2020 + + dd/MM/yyyy + true + + ^[a-zA-ZÀ-ž' \-\.,]*$ [A-ZÀ-ž]