Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 627a9c1

Browse files
committed
ParameterValidation now distinguishes between double and integer values and validates them properly. Fix KinkyDesign/jaqpot-web#104
1 parent 58d9999 commit 627a9c1

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

JaqpotCoreServices/src/main/java/org/jaqpot/core/service/validator/ParameterValidator.java

+54-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.jaqpot.core.service.validator;
77

8+
import com.fasterxml.jackson.databind.deser.std.NumberDeserializers;
89
import org.apache.commons.lang3.StringUtils;
910
import org.jaqpot.core.data.serialize.JSONSerializer;
1011
import org.jaqpot.core.model.Parameter;
@@ -27,8 +28,10 @@ public class ParameterValidator {
2728
public enum Type {
2829
NUMERIC,
2930
STRING,
31+
INTEGER,
3032
NUMERIC_ARRAY,
3133
STRING_ARRAY,
34+
INTEGER_ARRAY,
3235
UNDEFINED
3336
}
3437

@@ -77,7 +80,9 @@ public void validate(String input, Set<Parameter> parameters) throws ParameterTy
7780

7881
//Get type of algorithm's parameter
7982
Type typeOfParameter = Type.UNDEFINED;
80-
if (isNumeric(parameter.getValue().toString()))
83+
if(isInteger(parameter.getValue().toString()))
84+
typeOfParameter = Type.INTEGER;
85+
else if (isNumeric(parameter.getValue().toString()))
8186
typeOfParameter = Type.NUMERIC;
8287
else if (StringUtils.isAlphanumericSpace(parameter.getValue().toString()))
8388
typeOfParameter = Type.STRING;
@@ -89,6 +94,18 @@ else if (parameter.getValue() instanceof Collection)
8994

9095
//Get type of user's value and validate
9196
switch (typeOfParameter) {
97+
case INTEGER:
98+
if (isInteger(value.toString())) {
99+
if (parameter.getAllowedValues()!=null)
100+
checkAllowedValues(parameterId,Integer.parseInt(value.toString()), (List<Integer>) (List<?>) parameter.getAllowedValues());
101+
if (parameter.getMinValue()!=null && isInteger(parameter.getMinValue().toString()))
102+
checkIsLessThan(parameterId,Integer.parseInt(value.toString()), (Integer.parseInt(parameter.getMinValue().toString())));
103+
if (parameter.getMaxValue()!=null && isInteger(parameter.getMaxValue().toString()))
104+
checkIsGreaterThan(parameterId,Integer.parseInt(value.toString()), (Integer.parseInt(parameter.getMaxValue().toString())));
105+
}
106+
else
107+
throw new ParameterTypeException("Parameter with id:" + parameterId + " must be a whole number");
108+
break;
92109
case NUMERIC:
93110
if (isNumeric(value.toString())) {
94111
if (parameter.getAllowedValues()!=null)
@@ -115,6 +132,20 @@ else if (parameter.getValue() instanceof Collection)
115132
else
116133
throw new ParameterTypeException("Parameter with id: '" + parameterId + "' must be Alphanumeric");
117134
break;
135+
case INTEGER_ARRAY:
136+
if ((value instanceof Collection && getTypeOfCollection((Collection) value) == Type.INTEGER_ARRAY)) {
137+
checkAllowedValues(parameterId,value, parameter.getAllowedValues());
138+
checkMinMaxSize(parameterId,(Collection) value, parameter.getMinArraySize(), parameter.getMaxArraySize());
139+
for (Object o : (Collection) value) {
140+
if (parameter.getMinValue()!=null && isInteger(parameter.getMinValue().toString()))
141+
checkIsLessThan(parameterId,Integer.parseInt(o.toString()), (Integer.parseInt(parameter.getMinValue().toString())));
142+
if (parameter.getMaxValue()!=null && isInteger(parameter.getMaxValue().toString()))
143+
checkIsGreaterThan(parameterId,Integer.parseInt(o.toString()), (Integer.parseInt(parameter.getMaxValue().toString())));
144+
}
145+
}
146+
else
147+
throw new ParameterTypeException("Parameter with id: '" + parameterId + "' must be Array of integer values");
148+
break;
118149
case NUMERIC_ARRAY:
119150
if ((value instanceof Collection && getTypeOfCollection((Collection) value) == Type.NUMERIC_ARRAY)) {
120151
checkAllowedValues(parameterId,value, parameter.getAllowedValues());
@@ -149,7 +180,7 @@ else if (parameter.getValue() instanceof Collection)
149180
}
150181
}
151182

152-
static <T> boolean checkAllowedValues(String parameterId, T value, List<T> elements) throws ParameterRangeException {
183+
private static <T> boolean checkAllowedValues(String parameterId, T value, List<T> elements) throws ParameterRangeException {
153184
if (value!=null)
154185
if (elements!=null) {
155186
for (T o : elements) {
@@ -161,21 +192,21 @@ static <T> boolean checkAllowedValues(String parameterId, T value, List<T> elem
161192
return true;
162193
}
163194

164-
static <T extends Comparable<? super T>> Boolean checkIsLessThan (String parameterId, T value, T minimum) throws ParameterRangeException {
195+
private static <T extends Comparable<? super T>> Boolean checkIsLessThan(String parameterId, T value, T minimum) throws ParameterRangeException {
165196
if (minimum != null && isNumeric(minimum.toString()))
166197
if (value.compareTo(minimum)<0)
167198
throw new ParameterRangeException("Parameter with id: '" + parameterId + "' has a value less than the parameter's allowed minimum");
168199
return true;
169200
}
170201

171-
static <T extends Comparable<? super T>> Boolean checkIsGreaterThan (String parameterId, T value, T maximum) throws ParameterRangeException {
202+
private static <T extends Comparable<? super T>> Boolean checkIsGreaterThan(String parameterId, T value, T maximum) throws ParameterRangeException {
172203
if (maximum != null)
173204
if (value.compareTo(maximum)>0)
174205
throw new ParameterRangeException("Parameter with id: '" + parameterId + "' has a value greater than the parameter's allowed maximum");
175206
return true;
176207
}
177208

178-
static Boolean checkMinMaxSize(String parameterId, Collection collection, Integer minSize, Integer maxSize) throws ParameterRangeException {
209+
private static Boolean checkMinMaxSize(String parameterId, Collection collection, Integer minSize, Integer maxSize) throws ParameterRangeException {
179210
if (minSize!=null && isNumeric(minSize.toString()))
180211
if (collection.size()<minSize)
181212
throw new ParameterRangeException("Parameter with id: '" + parameterId + "' has an array size lees than the parameter's allowed minimum array size");
@@ -187,31 +218,23 @@ static Boolean checkMinMaxSize(String parameterId, Collection collection, Intege
187218

188219
//Returns if array is a (consistent) collection of Strings (Type.STRING) or Numbers (Type.NUMERIC).
189220
//else returns Type.UNDEFINED
190-
static Type getTypeOfCollection(Collection collection)
191-
{
221+
private static Type getTypeOfCollection(Collection collection) {
192222
Type content = null;
193-
for (Object value:collection)
194-
{
195-
if(!isNumeric(value.toString()))
196-
if (!StringUtils.isAlphanumericSpace(value.toString()))
197-
return Type.UNDEFINED;
198-
else
199-
if (content == Type.NUMERIC_ARRAY)
200-
return Type.UNDEFINED;
201-
else
202-
content=Type.STRING_ARRAY;
203-
else
204-
if (content == Type.STRING_ARRAY)
205-
return Type.UNDEFINED;
206-
else
207-
content=Type.NUMERIC_ARRAY;
223+
for (Object value : collection) {
224+
if (isInteger(value.toString()) && (content == Type.INTEGER_ARRAY || content == null))
225+
content = Type.INTEGER_ARRAY;
226+
else if (isNumeric(value.toString()) && (content == Type.NUMERIC_ARRAY || content == Type.INTEGER_ARRAY || content == null))
227+
content = Type.NUMERIC_ARRAY;
228+
else if (StringUtils.isAlphanumericSpace(value.toString()) && (content == Type.STRING_ARRAY || content == null))
229+
content = Type.STRING_ARRAY;
230+
else return Type.UNDEFINED;
208231
}
209232
return content;
210233
}
211234

212235
//Probably most performant solution to check for isNumeric, according to discussion here
213236
//http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java/1102916#1102916
214-
static boolean isNumeric(String str)
237+
private static boolean isNumeric(String str)
215238
{
216239
try
217240
{
@@ -223,4 +246,12 @@ static boolean isNumeric(String str)
223246
}
224247
return true;
225248
}
249+
250+
private static boolean isInteger(String str)
251+
{
252+
if (isNumeric(str))
253+
if (str.contains("."))
254+
return false;
255+
return true;
256+
}
226257
}

0 commit comments

Comments
 (0)