Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/fugerit-org/fj-lib/issues/111>


### Changed

Expand Down
22 changes: 22 additions & 0 deletions docs/validator/validator_basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Here are the [Default Validator Messages](../../fj-core/src/main/resources/core/
<td><a href="#maxLength">maxength</a></td>
<td>If set checks for maximum length</td>
</tr>
<tr>
<td><a href="#strict">strict</a></td>
<td>If set to true, the validator will not be lenient and check for input overflow</td>
</tr>
</table>

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

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

<table>
<caption>Entry <a name="strict">strict</a></caption>
<tr>
<th>Default</th>
<td>false</td>
</tr>
<tr>
<th>Example</th>
<td>true</td>
</tr>
<tr>
<th>Since</th>
<td>8.7.1</td>
</tr>
</table>

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

Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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";
Expand All @@ -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;
}
Expand All @@ -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 ) ) {
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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 );
Expand All @@ -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() ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.junit.jupiter.api.Assertions.fail;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

Expand Down Expand Up @@ -112,5 +115,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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<entry key="maxDate">31/12/2020</entry>
</validator>

<validator id="testDateValidatorStrict" type="org.fugerit.java.core.validator.ValidatorDate">
<entry key="dateFormat">dd/MM/yyyy</entry>
<entry key="strict">true</entry>
</validator>

<validator id="testRegexValidator" type="org.fugerit.java.core.validator.ValidatorRegex">
<entry key="regex">^[a-zA-ZÀ-ž' \-\.,]*$</entry>
<entry key="info">[A-ZÀ-ž]</entry>
Expand Down