use of javax.validation.constraints.Size in project podam by devopsfolks.
the class TypeManufacturerUtil method findCollectionSize.
/**
* Searches for annotation with information about collection/map size
* and filling strategies
*
* @param strategy
* a data provider strategy
* @param annotations
* a list of annotations to inspect
* @param collectionElementType
* a collection element type
* @param elementStrategyHolder
* a holder to pass found element strategy back to the caller,
* can be null
* @param keyStrategyHolder
* a holder to pass found key strategy back to the caller,
* can be null
* @return
* A number of element in collection or null, if no annotation was
* found
* @throws InstantiationException
* A strategy cannot be instantiated
* @throws IllegalAccessException
* A strategy cannot be instantiated
*/
public static Integer findCollectionSize(DataProviderStrategy strategy, List<Annotation> annotations, Class<?> collectionElementType, Holder<AttributeStrategy<?>> elementStrategyHolder, Holder<AttributeStrategy<?>> keyStrategyHolder) throws InstantiationException, IllegalAccessException {
// If the user defined a strategy to fill the collection elements,
// we use it
Size size = null;
for (Annotation annotation : annotations) {
if (annotation instanceof PodamCollection) {
PodamCollection collectionAnnotation = (PodamCollection) annotation;
if (null != elementStrategyHolder) {
Class<? extends AttributeStrategy<?>> attributeStrategy = collectionAnnotation.collectionElementStrategy();
if (null == attributeStrategy || ObjectStrategy.class.isAssignableFrom(attributeStrategy)) {
attributeStrategy = collectionAnnotation.mapElementStrategy();
}
if (null != attributeStrategy) {
elementStrategyHolder.setValue(attributeStrategy.newInstance());
}
}
if (null != keyStrategyHolder) {
Class<? extends AttributeStrategy<?>> attributeStrategy = collectionAnnotation.mapKeyStrategy();
if (null != attributeStrategy) {
keyStrategyHolder.setValue(attributeStrategy.newInstance());
}
}
return collectionAnnotation.nbrElements();
} else if (annotation instanceof Size) {
size = (Size) annotation;
}
}
Integer nbrElements = strategy.getNumberOfCollectionElements(collectionElementType);
if (null != size) {
if (nbrElements > size.max()) {
nbrElements = size.max();
}
if (nbrElements < size.min()) {
nbrElements = size.min();
}
}
return nbrElements;
}
use of javax.validation.constraints.Size in project cxf by apache.
the class JaxRs2Extension method applyBeanValidatorAnnotations.
/**
* This is essentially a duplicate of {@link io.swagger.jackson.ModelResolver.applyBeanValidatorAnnotations}.
*
* @param parameter
* @param annotations
*/
private void applyBeanValidatorAnnotations(final Parameter parameter, final List<Annotation> annotations) {
Map<String, Annotation> annos = new HashMap<>();
if (annotations != null) {
for (Annotation annotation : annotations) {
annos.put(annotation.annotationType().getName(), annotation);
}
}
if (annos.containsKey(NotNull.class.getName())) {
parameter.setRequired(true);
}
if (parameter instanceof AbstractSerializableParameter) {
AbstractSerializableParameter<?> serializable = (AbstractSerializableParameter<?>) parameter;
if (annos.containsKey(Min.class.getName())) {
Min min = (Min) annos.get(Min.class.getName());
serializable.setMinimum(BigDecimal.valueOf(min.value()));
}
if (annos.containsKey(Max.class.getName())) {
Max max = (Max) annos.get(Max.class.getName());
serializable.setMaximum(BigDecimal.valueOf(max.value()));
}
if (annos.containsKey(Size.class.getName())) {
Size size = (Size) annos.get(Size.class.getName());
serializable.setMinimum(BigDecimal.valueOf(size.min()));
serializable.setMaximum(BigDecimal.valueOf(size.max()));
serializable.setMinItems(size.min());
serializable.setMaxItems(size.max());
}
if (annos.containsKey(DecimalMin.class.getName())) {
DecimalMin min = (DecimalMin) annos.get(DecimalMin.class.getName());
if (min.inclusive()) {
serializable.setMinimum(BigDecimal.valueOf(Double.valueOf(min.value())));
} else {
serializable.setExclusiveMinimum(!min.inclusive());
}
}
if (annos.containsKey(DecimalMax.class.getName())) {
DecimalMax max = (DecimalMax) annos.get(DecimalMax.class.getName());
if (max.inclusive()) {
serializable.setMaximum(BigDecimal.valueOf(Double.valueOf(max.value())));
} else {
serializable.setExclusiveMaximum(!max.inclusive());
}
}
if (annos.containsKey(Pattern.class.getName())) {
Pattern pattern = (Pattern) annos.get(Pattern.class.getName());
serializable.setPattern(pattern.regexp());
}
}
}
use of javax.validation.constraints.Size in project cxf by apache.
the class JaxRs2Extension method applyBeanValidatorAnnotations.
/**
* This is mostly a duplicate of {@link io.swagger.v3.core.jackson.ModelResolver#applyBeanValidatorAnnotations}.
*
* @param parameter
* @param annotations
*/
private void applyBeanValidatorAnnotations(final Parameter parameter, final List<Annotation> annotations) {
Map<String, Annotation> annos = new HashMap<>();
if (annotations != null) {
annotations.forEach(annotation -> {
annos.put(annotation.annotationType().getName(), annotation);
});
}
if (annos.containsKey(NotNull.class.getName())) {
parameter.setRequired(true);
}
Schema<?> schema = parameter.getSchema();
if (annos.containsKey(Min.class.getName())) {
Min min = (Min) annos.get(Min.class.getName());
schema.setMinimum(BigDecimal.valueOf(min.value()));
}
if (annos.containsKey(Max.class.getName())) {
Max max = (Max) annos.get(Max.class.getName());
schema.setMaximum(BigDecimal.valueOf(max.value()));
}
if (annos.containsKey(Size.class.getName())) {
Size size = (Size) annos.get(Size.class.getName());
schema.setMinimum(BigDecimal.valueOf(size.min()));
schema.setMaximum(BigDecimal.valueOf(size.max()));
schema.setMinItems(size.min());
schema.setMaxItems(size.max());
}
if (annos.containsKey(DecimalMin.class.getName())) {
DecimalMin min = (DecimalMin) annos.get(DecimalMin.class.getName());
if (min.inclusive()) {
schema.setMinimum(BigDecimal.valueOf(Double.valueOf(min.value())));
} else {
schema.setExclusiveMinimum(!min.inclusive());
}
}
if (annos.containsKey(DecimalMax.class.getName())) {
DecimalMax max = (DecimalMax) annos.get(DecimalMax.class.getName());
if (max.inclusive()) {
schema.setMaximum(BigDecimal.valueOf(Double.valueOf(max.value())));
} else {
schema.setExclusiveMaximum(!max.inclusive());
}
}
if (annos.containsKey(Pattern.class.getName())) {
Pattern pattern = (Pattern) annos.get(Pattern.class.getName());
schema.setPattern(pattern.regexp());
}
}
use of javax.validation.constraints.Size in project swagger-core by swagger-api.
the class ModelResolver method applyBeanValidatorAnnotations.
protected void applyBeanValidatorAnnotations(Property property, Annotation[] annotations) {
Map<String, Annotation> annos = new HashMap<String, Annotation>();
if (annotations != null) {
for (Annotation anno : annotations) {
annos.put(anno.annotationType().getName(), anno);
}
}
if (annos.containsKey("javax.validation.constraints.NotNull")) {
property.setRequired(true);
}
if (annos.containsKey("javax.validation.constraints.Min")) {
if (property instanceof AbstractNumericProperty) {
Min min = (Min) annos.get("javax.validation.constraints.Min");
AbstractNumericProperty ap = (AbstractNumericProperty) property;
ap.setMinimum(new BigDecimal(min.value()));
}
}
if (annos.containsKey("javax.validation.constraints.Max")) {
if (property instanceof AbstractNumericProperty) {
Max max = (Max) annos.get("javax.validation.constraints.Max");
AbstractNumericProperty ap = (AbstractNumericProperty) property;
ap.setMaximum(new BigDecimal(max.value()));
}
}
if (annos.containsKey("javax.validation.constraints.Size")) {
Size size = (Size) annos.get("javax.validation.constraints.Size");
if (property instanceof AbstractNumericProperty) {
AbstractNumericProperty ap = (AbstractNumericProperty) property;
ap.setMinimum(new BigDecimal(size.min()));
ap.setMaximum(new BigDecimal(size.max()));
} else if (property instanceof StringProperty) {
StringProperty sp = (StringProperty) property;
sp.minLength(new Integer(size.min()));
sp.maxLength(new Integer(size.max()));
} else if (property instanceof ArrayProperty) {
ArrayProperty sp = (ArrayProperty) property;
sp.setMinItems(size.min());
sp.setMaxItems(size.max());
}
}
if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
if (property instanceof AbstractNumericProperty) {
AbstractNumericProperty ap = (AbstractNumericProperty) property;
ap.setMinimum(new BigDecimal(min.value()));
ap.setExclusiveMinimum(!min.inclusive());
}
}
if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
if (property instanceof AbstractNumericProperty) {
AbstractNumericProperty ap = (AbstractNumericProperty) property;
ap.setMaximum(new BigDecimal(max.value()));
ap.setExclusiveMaximum(!max.inclusive());
}
}
if (annos.containsKey("javax.validation.constraints.Pattern")) {
Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
if (property instanceof StringProperty) {
StringProperty ap = (StringProperty) property;
ap.setPattern(pattern.regexp());
}
}
}
use of javax.validation.constraints.Size in project minijax by minijax.
the class MinijaxConstraintDescriptor method build.
@SuppressWarnings("unchecked")
public static <T extends Annotation> MinijaxConstraintDescriptor<T> build(final AnnotatedType annotatedType, final T annotation) {
final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class);
if (constraint == null) {
return null;
}
final Class<?> valueClass = ReflectionUtils.getRawType(annotatedType);
final Class<?> annotationClass = annotation.annotationType();
if (constraint.validatedBy().length > 0) {
return buildDeclaredValidator(annotation, constraint.validatedBy()[0]);
} else if (annotationClass == AssertFalse.class) {
return (MinijaxConstraintDescriptor<T>) buildAssertFalseValidator((AssertFalse) annotation, valueClass);
} else if (annotationClass == AssertTrue.class) {
return (MinijaxConstraintDescriptor<T>) buildAssertTrueValidator((AssertTrue) annotation, valueClass);
} else if (annotationClass == Max.class) {
return (MinijaxConstraintDescriptor<T>) buildMaxValidator((Max) annotation, valueClass);
} else if (annotationClass == Min.class) {
return (MinijaxConstraintDescriptor<T>) buildMinValidator((Min) annotation, valueClass);
} else if (annotationClass == NotBlank.class) {
return (MinijaxConstraintDescriptor<T>) buildNotBlankValidator((NotBlank) annotation, valueClass);
} else if (annotationClass == NotEmpty.class) {
return (MinijaxConstraintDescriptor<T>) buildNotEmptyValidator((NotEmpty) annotation, valueClass);
} else if (annotationClass == NotNull.class) {
return (MinijaxConstraintDescriptor<T>) buildNotNullValidator((NotNull) annotation);
} else if (annotationClass == Pattern.class) {
return (MinijaxConstraintDescriptor<T>) buildPatternValidator((Pattern) annotation, valueClass);
} else if (annotationClass == Size.class) {
return (MinijaxConstraintDescriptor<T>) buildSizeValidator((Size) annotation, valueClass);
} else {
throw new ValidationException("Unrecognized constraint annotation: " + annotation);
}
}
Aggregations