Search in sources :

Example 1 with DecimalMin

use of javax.validation.constraints.DecimalMin in project AngularBeans by bessemHmidi.

the class BeanValidationProcessor method processBeanValidationParsing.

public void processBeanValidationParsing(Method method) {
    String modelName = CommonUtils.obtainFieldNameFromAccessor(method.getName());
    Annotation[] scannedAnnotations = method.getAnnotations();
    buffer.append("\nif (modelName === '").append(modelName).append("') {");
    for (Annotation a : scannedAnnotations) {
        if (!validationAnnotations.contains(a.annotationType())) {
            continue;
        }
        String name = (a.annotationType().getName());
        switch(name) {
            case "javax.validation.constraints.NotNull":
                buffer.append("\nentries[e].setAttribute('required', 'true');");
                break;
            case "org.hibernate.validator.constraints.Email":
                buffer.append("\nentries[e].setAttribute('type', 'email');");
                break;
            case "javax.validation.constraints.Pattern":
                String regex = ((Pattern) a).regexp();
                buffer.append("\nentries[e].setAttribute('ng-pattern', '").append(regex).append("');");
                break;
            case "javax.validation.constraints.Size":
                buffer.append("\nentries[e].setAttribute('required', 'true');");
                Size sizeConstraint = (Size) a;
                int maxLength = sizeConstraint.max();
                int minLength = sizeConstraint.min();
                if (minLength < 0) {
                    throw new IllegalArgumentException("The min parameter cannot be negative.");
                }
                if (maxLength < 0) {
                    throw new IllegalArgumentException("The max parameter cannot be negative.");
                }
                if (minLength > 0) {
                    buffer.append("\nentries[e].setAttribute('ng-minlength', '").append(minLength).append("');");
                }
                if (maxLength < Integer.MAX_VALUE) {
                    buffer.append("\nentries[e].setAttribute('ng-maxlength', '").append(maxLength).append("');");
                }
                break;
            case "javax.validation.constraints.DecimalMin":
                String dMin = ((DecimalMin) a).value();
                buffer.append("\nentries[e].setAttribute('min', '").append(dMin).append("');");
                break;
            case "javax.validation.constraints.DecimalMax":
                String dMax = ((DecimalMax) a).value();
                buffer.append("\nentries[e].setAttribute('max', '").append(dMax).append("');");
                break;
            case "javax.validation.constraints.Min":
                long min = ((Min) a).value();
                buffer.append("\nentries[e].setAttribute('min', '").append(min).append("');");
                break;
            case "javax.validation.constraints.Max":
                long max = ((Max) a).value();
                buffer.append("\nentries[e].setAttribute('max', '").append(max).append("');");
                break;
            default:
                break;
        }
    }
    buffer.append("\n}");
}
Also used : Pattern(javax.validation.constraints.Pattern) DecimalMin(javax.validation.constraints.DecimalMin) Min(javax.validation.constraints.Min) DecimalMax(javax.validation.constraints.DecimalMax) Max(javax.validation.constraints.Max) Size(javax.validation.constraints.Size) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) DecimalMax(javax.validation.constraints.DecimalMax)

Example 2 with DecimalMin

use of javax.validation.constraints.DecimalMin 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)

Aggregations

Annotation (java.lang.annotation.Annotation)2 DecimalMax (javax.validation.constraints.DecimalMax)2 DecimalMin (javax.validation.constraints.DecimalMin)2 Max (javax.validation.constraints.Max)2 Min (javax.validation.constraints.Min)2 Pattern (javax.validation.constraints.Pattern)2 Size (javax.validation.constraints.Size)2 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1