Class JavaxValidationModule

  • All Implemented Interfaces:
    Module

    public class JavaxValidationModule
    extends Object
    implements Module
    JSON Schema Generation Module: based on annotations from the javax.validation.constraints package.
    • Determine whether a member is not nullable, base assumption being that all fields and method return values are nullable if not annotated.
    • Optionally: also indicate all explicitly not nullable fields/methods to be required.
    • Populate "minItems" and "maxItems" for containers (i.e. arrays and collections).
    • Populate "minLength", "maxLength" and "format" for strings.
    • Optionally: populate "pattern" for strings.
    • Populate "minimum"/"exclusiveMinimum" and "maximum"/"exclusiveMaximum" for numbers.
    • Constructor Detail

      • JavaxValidationModule

        public JavaxValidationModule​(JavaxValidationOption... options)
        Constructor.
        Parameters:
        options - features to enable
    • Method Detail

      • forValidationGroups

        public JavaxValidationModule forValidationGroups​(Class<?>... validationGroups)
        Add validation groups to be considered.
        • Never calling this method will result in all annotations to be picked-up.
        • Calling this without parameters will only consider those annotations where no groups are defined.
        • Calling this with not-null parameters will only consider those annotations without defined groups or where at least one matches.
        Parameters:
        validationGroups - validation groups to consider
        Returns:
        this module instance (for chaining)
      • getAnnotationFromFieldOrGetter

        protected <A extends Annotation> A getAnnotationFromFieldOrGetter​(MemberScope<?,​?> member,
                                                                          Class<A> annotationClass,
                                                                          Function<A,​Class<?>[]> validationGroupsLookup)
        Retrieves the annotation instance of the given type, either from the field itself or (if not present) from its getter.
        If the given field/method represents only a container item of the actual declared type, that container item's annotations are being checked.
        Type Parameters:
        A - type of annotation
        Parameters:
        member - field or method to retrieve annotation instance from (or from a field's getter or getter method's field)
        annotationClass - type of annotation
        validationGroupsLookup - how to look-up the associated validation groups of an annotation instance
        Returns:
        annotation instance (or null)
        See Also:
        MemberScope.getAnnotationConsideringFieldAndGetterIfSupported(Class), MemberScope.getContainerItemAnnotationConsideringFieldAndGetterIfSupported(Class)
      • isNullable

        protected Boolean isNullable​(MemberScope<?,​?> member)
        Determine whether a given field or method is annotated to be not nullable.
        Parameters:
        member - the field or method to check
        Returns:
        whether member is annotated as nullable or not (returns null if not specified: assumption it is nullable then)
      • isRequired

        protected boolean isRequired​(MemberScope<?,​?> member)
        Determine whether a given field or method is deemed to be required in its parent type.
        Parameters:
        member - the field or method to check
        Returns:
        whether member is deemed to be required or not
      • resolveArrayMinItems

        protected Integer resolveArrayMinItems​(MemberScope<?,​?> member)
        Determine a given array type's minimum number of items.
        Parameters:
        member - the field or method to check
        Returns:
        specified minimum number of array items (or null)
        See Also:
        Size
      • resolveArrayMaxItems

        protected Integer resolveArrayMaxItems​(MemberScope<?,​?> member)
        Determine a given array type's maximum number of items.
        Parameters:
        member - the field or method to check
        Returns:
        specified maximum number of array items (or null)
        See Also:
        Size
      • resolveStringMinLength

        protected Integer resolveStringMinLength​(MemberScope<?,​?> member)
        Determine a given text type's minimum number of characters.
        Parameters:
        member - the field or method to check
        Returns:
        specified minimum number of characters (or null)
        See Also:
        Size, NotEmpty, NotBlank
      • resolveStringMaxLength

        protected Integer resolveStringMaxLength​(MemberScope<?,​?> member)
        Determine a given text type's maximum number of characters.
        Parameters:
        member - the field or method to check
        Returns:
        specified minimum number of characters (or null)
        See Also:
        Size
      • resolveStringFormat

        protected String resolveStringFormat​(MemberScope<?,​?> member)
        Determine a given text type's format.
        Parameters:
        member - the field or method to check
        Returns:
        specified format (or null)
        See Also:
        Email
      • resolveStringPattern

        protected String resolveStringPattern​(MemberScope<?,​?> member)
        Determine a given text type's pattern.
        Parameters:
        member - the field or method to check
        Returns:
        specified pattern (or null)
        See Also:
        Pattern
      • resolveNumberInclusiveMinimum

        protected BigDecimal resolveNumberInclusiveMinimum​(MemberScope<?,​?> member)
        Determine a number type's minimum (inclusive) value.
        Parameters:
        member - the field or method to check
        Returns:
        specified inclusive minimum value (or null)
        See Also:
        Min, DecimalMin, PositiveOrZero
      • resolveNumberExclusiveMinimum

        protected BigDecimal resolveNumberExclusiveMinimum​(MemberScope<?,​?> member)
        Determine a number type's minimum (exclusive) value.
        Parameters:
        member - the field or method to check
        Returns:
        specified exclusive minimum value (or null)
        See Also:
        DecimalMin, Positive
      • resolveNumberInclusiveMaximum

        protected BigDecimal resolveNumberInclusiveMaximum​(MemberScope<?,​?> member)
        Determine a number type's maximum (inclusive) value.
        Parameters:
        member - the field or method to check
        Returns:
        specified inclusive maximum value (or null)
        See Also:
        Max, DecimalMax.inclusive(), NegativeOrZero
      • resolveNumberExclusiveMaximum

        protected BigDecimal resolveNumberExclusiveMaximum​(MemberScope<?,​?> member)
        Determine a number type's maximum (exclusive) value.
        Parameters:
        member - the field or method to check
        Returns:
        specified exclusive maximum value (or null)
        See Also:
        DecimalMax.inclusive(), Negative