MIFOSX-36 - Added validation for Integer parameters
This commit is contained in:
+13
-6
@@ -43,17 +43,18 @@ public final class CodeValueCommandFromApiJsonDeserializer {
|
||||
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, this.supportedParameters);
|
||||
|
||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("codevalue");
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code.value");
|
||||
|
||||
final JsonElement element = this.fromApiJsonHelper.parse(json);
|
||||
|
||||
final String name = this.fromApiJsonHelper.extractStringNamed(CODEVALUE_JSON_INPUT_PARAMS.NAME.getValue(), element);
|
||||
baseDataValidator.reset().parameter(CODEVALUE_JSON_INPUT_PARAMS.NAME.getValue()).value(name).notBlank().notExceedingLengthOf(100);
|
||||
|
||||
/*final Integer position = this.fromApiJsonHelper.extractIntegerSansLocaleNamed(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(),
|
||||
element);
|
||||
baseDataValidator.reset().parameter(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue()).value(position).notBlank()
|
||||
.integerGreaterThanZero();*/
|
||||
if (this.fromApiJsonHelper.parameterExists(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(), element)) {
|
||||
//Validate input value is a valid Integer
|
||||
this.fromApiJsonHelper.extractIntegerSansLocaleNamed(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(),
|
||||
element);
|
||||
}
|
||||
|
||||
throwExceptionIfValidationWarningsExist(dataValidationErrors);
|
||||
}
|
||||
@@ -65,7 +66,7 @@ public final class CodeValueCommandFromApiJsonDeserializer {
|
||||
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, this.supportedParameters);
|
||||
|
||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("codevalue");
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code.value");
|
||||
|
||||
final JsonElement element = this.fromApiJsonHelper.parse(json);
|
||||
|
||||
@@ -75,6 +76,12 @@ public final class CodeValueCommandFromApiJsonDeserializer {
|
||||
.notExceedingLengthOf(100);
|
||||
}
|
||||
|
||||
if (this.fromApiJsonHelper.parameterExists(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(), element)) {
|
||||
//Validate input value is a valid Integer
|
||||
this.fromApiJsonHelper.extractIntegerSansLocaleNamed(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(),
|
||||
element);
|
||||
}
|
||||
|
||||
throwExceptionIfValidationWarningsExist(dataValidationErrors);
|
||||
}
|
||||
|
||||
|
||||
+24
-1
@@ -153,7 +153,7 @@ public class JsonParserHelper {
|
||||
JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
|
||||
final String stringValue = primitive.getAsString();
|
||||
if (StringUtils.isNotBlank(stringValue)) {
|
||||
intValue = Integer.valueOf(stringValue);
|
||||
intValue = convertToIntegerSanLocale(stringValue, parameterName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -373,6 +373,29 @@ public class JsonParserHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public Integer convertToIntegerSanLocale(final String numericalValueFormatted, final String parameterName) {
|
||||
|
||||
try {
|
||||
Integer number = null;
|
||||
|
||||
if (StringUtils.isNotBlank(numericalValueFormatted)) {
|
||||
number = Integer.valueOf(numericalValueFormatted);
|
||||
}
|
||||
|
||||
return number;
|
||||
} catch (NumberFormatException e) {
|
||||
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.integer", "The parameter "
|
||||
+ parameterName + " has value: " + numericalValueFormatted + " which is invalid integer.", parameterName, numericalValueFormatted);
|
||||
dataValidationErrors.add(error);
|
||||
|
||||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
|
||||
dataValidationErrors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public BigDecimal convertFrom(final String numericalValueFormatted, final String parameterName, final Locale clientApplicationLocale) {
|
||||
|
||||
if (clientApplicationLocale == null) {
|
||||
|
||||
Reference in New Issue
Block a user