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);
|
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, this.supportedParameters);
|
||||||
|
|
||||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
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 JsonElement element = this.fromApiJsonHelper.parse(json);
|
||||||
|
|
||||||
final String name = this.fromApiJsonHelper.extractStringNamed(CODEVALUE_JSON_INPUT_PARAMS.NAME.getValue(), element);
|
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);
|
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(),
|
if (this.fromApiJsonHelper.parameterExists(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(), element)) {
|
||||||
element);
|
//Validate input value is a valid Integer
|
||||||
baseDataValidator.reset().parameter(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue()).value(position).notBlank()
|
this.fromApiJsonHelper.extractIntegerSansLocaleNamed(CODEVALUE_JSON_INPUT_PARAMS.POSITION.getValue(),
|
||||||
.integerGreaterThanZero();*/
|
element);
|
||||||
|
}
|
||||||
|
|
||||||
throwExceptionIfValidationWarningsExist(dataValidationErrors);
|
throwExceptionIfValidationWarningsExist(dataValidationErrors);
|
||||||
}
|
}
|
||||||
@@ -65,7 +66,7 @@ public final class CodeValueCommandFromApiJsonDeserializer {
|
|||||||
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, this.supportedParameters);
|
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, this.supportedParameters);
|
||||||
|
|
||||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
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 JsonElement element = this.fromApiJsonHelper.parse(json);
|
||||||
|
|
||||||
@@ -75,6 +76,12 @@ public final class CodeValueCommandFromApiJsonDeserializer {
|
|||||||
.notExceedingLengthOf(100);
|
.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);
|
throwExceptionIfValidationWarningsExist(dataValidationErrors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+24
-1
@@ -153,7 +153,7 @@ public class JsonParserHelper {
|
|||||||
JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
|
JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive();
|
||||||
final String stringValue = primitive.getAsString();
|
final String stringValue = primitive.getAsString();
|
||||||
if (StringUtils.isNotBlank(stringValue)) {
|
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) {
|
public BigDecimal convertFrom(final String numericalValueFormatted, final String parameterName, final Locale clientApplicationLocale) {
|
||||||
|
|
||||||
if (clientApplicationLocale == null) {
|
if (clientApplicationLocale == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user