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 -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyToConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder builder) forValidationGroups(Class<?>... validationGroups) Add validation groups to be considered.protected <A extends Annotation>
AgetAnnotationFromFieldOrGetter(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 BooleanisNullable(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine whether a given field or method is annotated to be not nullable.protected booleanisRequired(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine whether a given field or method is deemed to be required in its parent type.protected voidoverrideInstanceAttributes(tools.jackson.databind.node.ObjectNode memberAttributes, com.github.victools.jsonschema.generator.MemberScope<?, ?> member, com.github.victools.jsonschema.generator.SchemaGenerationContext context) Implementation of the functionalInstanceAttributeOverrideV2interface.protected IntegerresolveArrayMaxItems(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a given array type's maximum number of items.protected IntegerresolveArrayMinItems(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a given array type's minimum number of items.resolveEnum(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Look-up the finite list of possible values.protected BigDecimalresolveNumberExclusiveMaximum(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a number type's maximum (exclusive) value.protected BigDecimalresolveNumberExclusiveMinimum(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a number type's minimum (exclusive) value.protected BigDecimalresolveNumberInclusiveMaximum(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a number type's maximum (inclusive) value.protected BigDecimalresolveNumberInclusiveMinimum(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a number type's minimum (inclusive) value.protected StringresolveStringFormat(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a given text type's format.protected IntegerresolveStringMaxLength(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a given text type's maximum number of characters.protected IntegerresolveStringMinLength(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a given text type's minimum number of characters.protected StringresolveStringPattern(com.github.victools.jsonschema.generator.MemberScope<?, ?> member) Determine a given text type's pattern.
-
Constructor Details
-
JakartaValidationModule
Constructor.- Parameters:
options- features to enable
-
-
Method Details
-
forValidationGroups
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:
applyToConfigBuilderin interfacecom.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 annotationvalidationGroupsLookup- 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
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:
-
SizeNotEmpty
-
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:
-
SizeNotEmptyNotBlank
-
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:
-
MinDecimalMinPositiveOrZero
-
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:
-
DecimalMinPositive
-
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:
-
MaxDecimalMax.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:
-
AssertTrueAssertFalse
-
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 functionalInstanceAttributeOverrideV2interface.- Parameters:
memberAttributes- already collected type attributes to add the min/max properties tomember- the field or method for which additional attributes may be collectedcontext- generation context- Since:
- 4.28.0 setting "minProperties" and "maxProperties" for "Map" types
-