Module for the jsonschema-generator – deriving JSON Schema attributes from jakarta.validation.constraints
annotations.
- Determine whether a member is not nullable, base assumption being that all fields and method return values are nullable if not annotated. Based on
@NotNull
/@Null
/@NotEmpty
/@NotBlank
. - Populate list of "required" fields/methods for objects if
JakartaValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED
and/orJakartaValidationOption.NOT_NULLABLE_METHOD_IS_REQUIRED
is/are being provided in constructor. - Populate "minItems" and "maxItems" for containers (i.e. arrays and collections). Based on
@Size
/@NotEmpty
. - Populate "minProperties" and "maxProperties" for
Map
types. Based on@Size
/@NotEmpty
. - Populate "minLength" and "maxLength" for strings. Based on
@Size
/@NotEmpty
/@NotBlank
. - Populate "format" for strings. Based on
@Email
, can be "email" or "idn-email" depending on whetherJakartaValidationOption.PREFER_IDN_EMAIL_FORMAT
is being provided in constructor. - Populate "pattern" for strings. Based on
@Pattern
/@Email
, when correspondingJakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS
is being provided in constructor. - Populate "minimum"/"exclusiveMinimum" for numbers. Based on
@Min
/@DecimalMin
/@Positive
/@PositiveOrZero
. - Populate "maximum"/"exclusiveMaximum" for numbers. Based on
@Max
/@DecimalMax
/@Negative
/@NegativeOrZero
. - Populate "enum"/"const" for booleans. Based on
@AssertTrue
/@AssertFalse
.
Schema attributes derived from validation annotations on fields are also applied to their respective getter methods.
Schema attributes derived from validation annotations on getter methods are also applied to their associated fields.
JavaDoc is being used throughout the codebase, offering contextual information in your respective IDE or being available online through services like javadoc.io.
Additional documentation can be found in the Project Wiki.
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jakarta-validation</artifactId>
<version>[4.21.0,5.0.0)</version>
</dependency>
Since version 4.7
, the release versions of the main generator library and this module are aligned.
It is recommended to use identical versions for both dependencies to ensure compatibility.
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule;
JakartaValidationModule module = new JakartaValidationModule();
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09)
.with(module);
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption;
JakartaValidationModule module = new JakartaValidationModule(JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
.with(module);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(YourClass.class);
System.out.println(jsonSchema.toString());