Search in sources :

Example 6 with Size

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;
}
Also used : Size(javax.validation.constraints.Size) ObjectStrategy(uk.co.jemos.podam.api.ObjectStrategy) Annotation(java.lang.annotation.Annotation)

Example 7 with Size

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());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) HashMap(java.util.HashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) DecimalMin(javax.validation.constraints.DecimalMin) NotNull(javax.validation.constraints.NotNull) Annotation(java.lang.annotation.Annotation) DecimalMin(javax.validation.constraints.DecimalMin) Min(javax.validation.constraints.Min) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) DecimalMax(javax.validation.constraints.DecimalMax)

Example 8 with Size

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());
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) DecimalMin(javax.validation.constraints.DecimalMin) Min(javax.validation.constraints.Min) HashMap(java.util.HashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) DecimalMin(javax.validation.constraints.DecimalMin) NotNull(javax.validation.constraints.NotNull) Annotation(java.lang.annotation.Annotation) DecimalMax(javax.validation.constraints.DecimalMax)

Example 9 with Size

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());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) StringProperty(io.swagger.models.properties.StringProperty) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) DecimalMax(javax.validation.constraints.DecimalMax)

Example 10 with Size

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);
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) ValidationException(javax.validation.ValidationException) Constraint(javax.validation.Constraint) Max(javax.validation.constraints.Max) Size(javax.validation.constraints.Size) AssertTrue(javax.validation.constraints.AssertTrue) NotNull(javax.validation.constraints.NotNull) Min(javax.validation.constraints.Min) NotBlank(javax.validation.constraints.NotBlank) AssertFalse(javax.validation.constraints.AssertFalse) NotEmpty(javax.validation.constraints.NotEmpty)

Aggregations

Size (javax.validation.constraints.Size)11 Annotation (java.lang.annotation.Annotation)7 Max (javax.validation.constraints.Max)7 Min (javax.validation.constraints.Min)7 Pattern (javax.validation.constraints.Pattern)7 DecimalMax (javax.validation.constraints.DecimalMax)6 DecimalMin (javax.validation.constraints.DecimalMin)6 HashMap (java.util.HashMap)5 NotNull (javax.validation.constraints.NotNull)3 IridaSequenceFile (ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile)2 IridaSequenceFilePair (ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists (com.google.common.collect.Lists)2 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)2 ArrayProperty (io.swagger.models.properties.ArrayProperty)2 StringProperty (io.swagger.models.properties.StringProperty)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2