Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.struts2.validator.annotations.ExpressionValidator;
import org.apache.struts2.validator.annotations.FieldExpressionValidator;
import org.apache.struts2.validator.annotations.IntRangeFieldValidator;
import org.apache.struts2.validator.annotations.LocalDateRangeFieldValidator;
import org.apache.struts2.validator.annotations.LongRangeFieldValidator;
import org.apache.struts2.validator.annotations.RegexFieldValidator;
import org.apache.struts2.validator.annotations.RequiredFieldValidator;
Expand All @@ -46,6 +47,7 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -125,6 +127,10 @@ else if (a instanceof DateRangeFieldValidator v) {
}

}
// Process LocalDateRangeFieldValidator
else if (a instanceof LocalDateRangeFieldValidator v) {
result.add(processLocalDateRangeFieldValidatorAnnotation(v, fieldName, methodName));
}
// Process EmailValidator
else if (a instanceof EmailValidator v) {
ValidatorConfig temp = processEmailValidatorAnnotation(v, fieldName, methodName);
Expand Down Expand Up @@ -267,6 +273,9 @@ private void processValidationAnnotation(Annotation a, String fieldName, String
}
}
}
for (LocalDateRangeFieldValidator v : validations.localDateRangeFields()) {
result.add(processLocalDateRangeFieldValidatorAnnotation(v, fieldName, methodName));
}
EmailValidator[] emv = validations.emails();
if (emv != null) {
for (EmailValidator v : emv) {
Expand Down Expand Up @@ -891,6 +900,40 @@ private ValidatorConfig processCreditCardValidatorAnnotation(CreditCardValidator
.build();
}

private ValidatorConfig processLocalDateRangeFieldValidatorAnnotation(LocalDateRangeFieldValidator v, String fieldName, String methodName) {
String validatorType = "localdate";

Map<String, Object> params = new HashMap<>();

if (fieldName != null) {
params.put("fieldName", fieldName);
} else if (StringUtils.isNotEmpty(v.fieldName())) {
params.put("fieldName", v.fieldName());
}
if (StringUtils.isNotEmpty(v.min())) {
params.put("min", LocalDate.parse(v.min()));
}
if (StringUtils.isNotEmpty(v.max())) {
params.put("max", LocalDate.parse(v.max()));
}
if (StringUtils.isNotEmpty(v.minExpression())) {
params.put("minExpression", v.minExpression());
}
if (StringUtils.isNotEmpty(v.maxExpression())) {
params.put("maxExpression", v.maxExpression());
}

validatorFactory.lookupRegisteredValidatorType(validatorType);
return new ValidatorConfig.Builder(validatorType)
.addParams(params)
.addParam("methodName", methodName)
.shortCircuit(v.shortCircuit())
.defaultMessage(v.message())
.messageKey(v.key())
.messageParams(v.messageParams())
.build();
}

private ValidatorConfig processDateRangeFieldValidatorAnnotation(DateRangeFieldValidator v, String fieldName, String methodName) {
String validatorType = "date";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.validator.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* <!-- START SNIPPET: description -->
* This validator checks that a {@link java.time.LocalDate} field has a value within a specified range, both
* bounds inclusive. The min and max values are ISO dates (yyyy-MM-dd).
* <!-- END SNIPPET: description -->
*
* <p><u>Annotation usage:</u></p>
*
* <!-- START SNIPPET: usage -->
* <p>The annotation must be applied at method level.</p>
* <!-- END SNIPPET: usage -->
*
* <p><u>Annotation parameters:</u></p>
*
* <!-- START SNIPPET: parameters -->
* <table class='confluenceTable' summary=''>
* <tr>
* <th class='confluenceTh'> Parameter </th>
* <th class='confluenceTh'> Required </th>
* <th class='confluenceTh'> Default </th>
* <th class='confluenceTh'> Notes </th>
* </tr>
* <tr>
* <td class='confluenceTd'>message</td>
* <td class='confluenceTd'>yes</td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'>field error message</td>
* </tr>
* <tr>
* <td class='confluenceTd'>key</td>
* <td class='confluenceTd'>no</td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'>i18n key from language specific properties file.</td>
* </tr>
* <tr>
* <td class='confluenceTd'>messageParams</td>
* <td class='confluenceTd'>no</td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'>Additional params to be used to customize message - will be evaluated against the Value Stack</td>
* </tr>
* <tr>
* <td class='confluenceTd'>fieldName</td>
* <td class='confluenceTd'>no</td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'>&nbsp;</td>
* </tr>
* <tr>
* <td class='confluenceTd'>shortCircuit</td>
* <td class='confluenceTd'>no</td>
* <td class='confluenceTd'>false</td>
* <td class='confluenceTd'>If this validator should be used as shortCircuit.</td>
* </tr>
* <tr>
* <td class='confluenceTd'>type</td>
* <td class='confluenceTd'>yes</td>
* <td class='confluenceTd'>ValidatorType.FIELD</td>
* <td class='confluenceTd'>Enum value from ValidatorType. Either FIELD or SIMPLE can be used here.</td>
* </tr>
* <tr>
* <td class='confluenceTd'> min </td>
* <td class='confluenceTd'> no </td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'> ISO date (yyyy-MM-dd). The minimum the date must be. </td>
* </tr>
* <tr>
* <td class='confluenceTd'>minExpression</td>
* <td class='confluenceTd'>no</td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'>OGNL expression used to obtain the minimum the date must be.</td>
* </tr>
* <tr>
* <td class='confluenceTd'> max </td>
* <td class='confluenceTd'> no </td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'> ISO date (yyyy-MM-dd). The maximum date can be. </td>
* </tr>
* <tr>
* <td class='confluenceTd'>maxExpression</td>
* <td class='confluenceTd'>no</td>
* <td class='confluenceTd'>&nbsp;</td>
* <td class='confluenceTd'>OGNL expression used to obtain the maximum date can be.</td>
* </tr>
* </table>
*
* <p>If neither <em>min</em> nor <em>max</em> is set, nothing will be done.</p>
* <!-- END SNIPPET: parameters -->
*
* <p><u>Example code:</u></p>
*
* <pre>
* <!-- START SNIPPET: example -->
* &#64;LocalDateRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "2005-01-01", max = "2005-12-31")
* <!-- END SNIPPET: example -->
* </pre>
*
* @since 7.5.0
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalDateRangeFieldValidator {

/**
* @return The minimum the date must be, as an ISO date (yyyy-MM-dd).
*/
String min() default "";

/**
* @return An expression which will be evaluated against the Value Stack to get the min value
*/
String minExpression() default "";

/**
* @return The maximum the date can be, as an ISO date (yyyy-MM-dd).
*/
String max() default "";

/**
* @return An expression which will be evaluated against the Value Stack to get the max value
*/
String maxExpression() default "";

/**
* @return The default error message for this validator.
* NOTE: It is required to set a message, if you are not using the message key for 18n lookup!
*/
String message() default "";

/**
* @return The message key to lookup for i18n.
*/
String key() default "";

/**
* @return Additional params to be used to customize message - will be evaluated against the Value Stack
*/
String[] messageParams() default {};

/**
* @return The optional fieldName for SIMPLE validator types.
*/
String fieldName() default "";

/**
* If this is activated, the validator will be used as short-circuit.
*
* Adds the short-circuit="true" attribute value if <tt>true</tt>.
* @return true if validator will be used as short-circuit. Default is false.
*/
boolean shortCircuit() default false;

/**
* @return The validation type for this field/method.
*/
ValidatorType type() default ValidatorType.FIELD;

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
* <td class='confluenceTd'> Add list of DateRangeFieldValidators </td>
* </tr>
* <tr>
* <td class='confluenceTd'> localDateRangeFields </td>
* <td class='confluenceTd'> no </td>
* <td class='confluenceTd'> Add list of LocalDateRangeFieldValidators </td>
* </tr>
* <tr>
* <td class='confluenceTd'> emails </td>
* <td class='confluenceTd'> no </td>
* <td class='confluenceTd'> Add list of EmailValidators </td>
Expand Down Expand Up @@ -186,6 +191,8 @@

DateRangeFieldValidator[] dateRangeFields() default {};

LocalDateRangeFieldValidator[] localDateRangeFields() default {};

EmailValidator[] emails() default {};

CreditCardValidator[] creditCards() default {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.validator.validators;

import org.apache.struts2.conversion.TypeConversionException;
import org.apache.struts2.conversion.impl.DefaultTypeConverter;

import java.lang.reflect.Member;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Map;

/**
* Converts the {@code min} / {@code max} params of {@link LocalDateRangeFieldValidator} strictly as ISO dates,
* mapped in {@code LocalDateRangeFieldValidator-conversion.properties}, so a bound does not depend on the
* application's {@code struts.date.format}.
*
* @since 7.5.0
*/
public final class LocalDateRangeBoundConverter extends DefaultTypeConverter {

@Override
public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName,
Object value, Class toType) {
if (value instanceof String bound) {
try {
return LocalDate.parse(bound.trim());
} catch (DateTimeParseException e) {
throw new TypeConversionException("Could not parse " + propertyName + " as an ISO date (yyyy-MM-dd)", e);
}
}
return value;
}

}
Loading
Loading