Class JakartaValidationModule

java.lang.Object
com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule
All Implemented Interfaces:
com.github.victools.jsonschema.generator.Module

public class JakartaValidationModule extends Object implements com.github.victools.jsonschema.generator.Module
JSON Schema Generation Module: based on annotations from the jakarta.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 Summary

    Constructors
    Constructor
    Description
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    applyToConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder builder)
     
    forValidationGroups(Class<?>... validationGroups)
    Add validation groups to be considered.
    protected <A extends Annotation>
    A
    getAnnotationFromFieldOrGetter(com.github.victools.jsonschema.generator.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.
    protected Boolean
    isNullable(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine whether a given field or method is annotated to be not nullable.
    protected boolean
    isRequired(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine whether a given field or method is deemed to be required in its parent type.
    protected void
    overrideInstanceAttributes(tools.jackson.databind.node.ObjectNode memberAttributes, com.github.victools.jsonschema.generator.MemberScope<?,?> member, com.github.victools.jsonschema.generator.SchemaGenerationContext context)
    Implementation of the functional InstanceAttributeOverrideV2 interface.
    protected Integer
    resolveArrayMaxItems(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a given array type's maximum number of items.
    protected Integer
    resolveArrayMinItems(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a given array type's minimum number of items.
    protected List<Object>
    resolveEnum(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Look-up the finite list of possible values.
    protected BigDecimal
    resolveNumberExclusiveMaximum(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a number type's maximum (exclusive) value.
    protected BigDecimal
    resolveNumberExclusiveMinimum(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a number type's minimum (exclusive) value.
    protected BigDecimal
    resolveNumberInclusiveMaximum(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a number type's maximum (inclusive) value.
    protected BigDecimal
    resolveNumberInclusiveMinimum(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a number type's minimum (inclusive) value.
    protected String
    resolveStringFormat(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a given text type's format.
    protected Integer
    resolveStringMaxLength(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a given text type's maximum number of characters.
    protected Integer
    resolveStringMinLength(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a given text type's minimum number of characters.
    protected String
    resolveStringPattern(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
    Determine a given text type's pattern.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JakartaValidationModule

      public JakartaValidationModule(JakartaValidationOption... options)
      Constructor.
      Parameters:
      options - features to enable
  • Method Details

    • forValidationGroups

      public JakartaValidationModule 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)
    • applyToConfigBuilder

      public void applyToConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder builder)
      Specified by:
      applyToConfigBuilder in interface com.github.victools.jsonschema.generator.Module
    • getAnnotationFromFieldOrGetter

      protected <A extends Annotation> A getAnnotationFromFieldOrGetter(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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
      • NotEmpty
    • resolveArrayMaxItems

      protected Integer resolveArrayMaxItems(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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(com.github.victools.jsonschema.generator.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
    • resolveEnum

      protected List<Object> resolveEnum(com.github.victools.jsonschema.generator.MemberScope<?,?> member)
      Look-up the finite list of possible values.
      Parameters:
      member - field/method to determine allowed values for
      Returns:
      applicable "const"/"enum" values or null
      Since:
      4.36.0
      See Also:
      • AssertTrue
      • AssertFalse
    • overrideInstanceAttributes

      protected void overrideInstanceAttributes(tools.jackson.databind.node.ObjectNode memberAttributes, com.github.victools.jsonschema.generator.MemberScope<?,?> member, com.github.victools.jsonschema.generator.SchemaGenerationContext context)
      Implementation of the functional InstanceAttributeOverrideV2 interface.
      Parameters:
      memberAttributes - already collected type attributes to add the min/max properties to
      member - the field or method for which additional attributes may be collected
      context - generation context
      Since:
      4.28.0 setting "minProperties" and "maxProperties" for "Map" types