Skip to content

Commit a84a2a0

Browse files
authored
Merge pull request #112 from fugerit-org/111-validatordate-always-accepts-date-format-overflow
feat: ValidatorDate always accepts date format overflow #111
2 parents 5ab8603 + fcdab00 commit a84a2a0

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111

1212
- reduced logging level for XmlBeanHelper.setFromElementSafe
13+
- ValidatorDate always accepts date format overflow <https://github.com/fugerit-org/fj-lib/issues/111>
14+
1315

1416
### Changed
1517

docs/validator/validator_basic.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Here are the [Default Validator Messages](../../fj-core/src/main/resources/core/
3131
<td><a href="#maxLength">maxength</a></td>
3232
<td>If set checks for maximum length</td>
3333
</tr>
34+
<tr>
35+
<td><a href="#strict">strict</a></td>
36+
<td>If set to true, the validator will not be lenient and check for input overflow</td>
37+
</tr>
3438
</table>
3539

3640
<br/><a href="#top">top</a><br/>
@@ -139,3 +143,21 @@ Here are the [Default Validator Messages](../../fj-core/src/main/resources/core/
139143

140144
<br/><a href="#top">top</a><br/>
141145

146+
<table>
147+
<caption>Entry <a name="strict">strict</a></caption>
148+
<tr>
149+
<th>Default</th>
150+
<td>false</td>
151+
</tr>
152+
<tr>
153+
<th>Example</th>
154+
<td>true</td>
155+
</tr>
156+
<tr>
157+
<th>Since</th>
158+
<td>8.7.1</td>
159+
</tr>
160+
</table>
161+
162+
<br/><a href="#top">top</a><br/>
163+

fj-core/src/main/java/org/fugerit/java/core/validator/ValidatorDate.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.fugerit.java.core.validator;
22

33
import java.text.ParseException;
4+
import java.text.ParsePosition;
45
import java.text.SimpleDateFormat;
56
import java.util.Date;
67
import java.util.Properties;
78

89
import org.fugerit.java.core.cfg.ConfigException;
10+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
911
import org.fugerit.java.core.lang.helpers.StringUtils;
1012

1113
public class ValidatorDate extends BasicValidator {
@@ -20,7 +22,9 @@ public class ValidatorDate extends BasicValidator {
2022
public static final String KEY_MINDATE = "minDate";
2123

2224
public static final String KEY_MAXDATE = "maxDate";
23-
25+
26+
public static final String KEY_STRICT = "strict";
27+
2428
public static final String ERROR_KEY_DATE = "error.date";
2529

2630
public static final String ERROR_KEY_DATE_MIN = "error.date.min";
@@ -32,7 +36,13 @@ public class ValidatorDate extends BasicValidator {
3236
private String minDate;
3337

3438
private String maxDate;
35-
39+
40+
private boolean strict;
41+
42+
public ValidatorDate() {
43+
this.strict = Boolean.FALSE;
44+
}
45+
3646
public String getDateFormat() {
3747
return dateFormat;
3848
}
@@ -45,6 +55,10 @@ public String getMaxDate() {
4555
return maxDate;
4656
}
4757

58+
public boolean isStrict() {
59+
return this.strict;
60+
}
61+
4862
protected Date setDate( SimpleDateFormat sdf, String d ) throws ParseException {
4963
Date res = null;
5064
if ( StringUtils.isNotEmpty( d ) ) {
@@ -77,6 +91,10 @@ public void configure( Properties atts ) throws ConfigException {
7791
if ( StringUtils.isNotEmpty( maxDateLocal ) ) {
7892
this.maxDate = maxDateLocal;
7993
}
94+
String strictLocal = atts.getProperty( KEY_STRICT );
95+
if ( StringUtils.isNotEmpty( strictLocal ) ) {
96+
this.strict = BooleanUtils.isTrue( strictLocal);
97+
}
8098
} catch (Exception e) {
8199
throw new ConfigException( e );
82100
}
@@ -93,7 +111,9 @@ protected boolean validate( ValidatorContext context, String minDate, String max
93111
boolean valid = true;
94112
try {
95113
SimpleDateFormat sdf = new SimpleDateFormat( this.getDateFormat() );
96-
Date d = sdf.parse( context.getValue() );
114+
sdf.setLenient( !this.isStrict() );
115+
ParsePosition pp = new ParsePosition( 0 );
116+
Date d = sdf.parse( context.getValue(), pp );
97117
if ( StringUtils.isNotEmpty( minDate ) && d.before( sdf.parse( minDate ) ) ) {
98118
valid = false;
99119
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
104124
String message = this.formatMessage( context.getBundle() , ERROR_KEY_DATE_MAX, context.getLabel(), context.getValue(), maxDate );
105125
context.getResult().addError( context.getFieldId(), message );
106126
}
127+
if ( this.isStrict() && pp.getIndex() != context.getValue().length() ) {
128+
throw new ParseException( context.getValue(), pp.getIndex() );
129+
}
107130
} catch (Exception e) {
108131
valid = false;
109132
String message = this.formatMessage( context.getBundle() , ERROR_KEY_DATE, context.getLabel(), context.getValue(), StringUtils.valueWithDefault( this.getInfo(), this.getDateFormat() ) );

fj-core/src/test/java/test/org/fugerit/java/core/validator/TestValidatorCatalog.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,15 @@ void testNumberValidator001_IT() {
112112
this.validatorWorker( "testNumberValidator1" , result, l, "1.000.134.234.243,123", "Invalid number 2", false, params );
113113
this.printResult(result);
114114
}
115+
116+
@Test
117+
void testDateValidatorStrict() {
118+
Locale l = Locale.ITALY;
119+
ValidatorResult result = new ValidatorResult();
120+
Properties params = new Properties();
121+
this.validatorWorker( "testDateValidatorStrict" , result, l, "01/03/2021AAA", "Valid date", Boolean.FALSE, params);
122+
this.validatorWorker( "testDateValidatorStrict" , result, l, "01/03/2021", "Valid date", Boolean.TRUE, params);
123+
this.printResult(result);
124+
}
115125

116126
}

fj-core/src/test/resources/core/validator/validator-catalog-test.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<entry key="maxDate">31/12/2020</entry>
1919
</validator>
2020

21+
<validator id="testDateValidatorStrict" type="org.fugerit.java.core.validator.ValidatorDate">
22+
<entry key="dateFormat">dd/MM/yyyy</entry>
23+
<entry key="strict">true</entry>
24+
</validator>
25+
2126
<validator id="testRegexValidator" type="org.fugerit.java.core.validator.ValidatorRegex">
2227
<entry key="regex">^[a-zA-ZÀ-ž' \-\.,]*$</entry>
2328
<entry key="info">[A-ZÀ-ž]</entry>

0 commit comments

Comments
 (0)