Interface IJsonSchemaTransformerBuilder
public interface IJsonSchemaTransformerBuilder
A builder that lets you configure a set of transformation steps to be applied to a JSON schema. Each method in this
build adds a transformation step, which will be applied in the order the methods were called. Once you are done, call
build() to create a transformer instance that can be used to transform JSON
schemas.- Since:
- 8.5.0
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Creates a newIJsonSchemaTransformerinstance with the configured transformation steps and configuration.Adds a transformation that converts all "oneOf" keywords to "anyOf" keywords, keeping the same subschemas.Adds a transformation step that forbids additional properties in all objects, i.e. sets "additionalProperties" to false in all applicable schemas.Adds a transformation step that makes all properties required.keepFormats(Iterable<?> formats) Adds a transformation that keeps only the given supported formats for the "format" keyword.keepKeywords(Iterable<?> keywords) Adds a transformation step that keeps only the given keywords in all objects in the schema, removing all other keywords.keepKnownKeywords(Iterable<?> keywords) Adds a transformation that keeps only the given keywords in all objects in the schema, removing all other keywords.Adds a transformation step that moves the "const" keyword into an "enum" keyword with a single value; and removes the "const" keyword.Adds a transformation step that moves the "examples" keyword into the "description" keyword, appending the examples to the description.prettyPrint(int indent) Sets whether to pretty-print the output JSON schema, and if so, with how many spaces to indent.removeFormats(Iterable<?> formats) Adds a transformation that removes "format" keyword if its value is one of the given formats.removeKeywords(Iterable<?> keywords) Adds a transformation that removes the given keywords from all objects in the schema.Removes from the "required" array all property names that do not exist in the "properties" object are not matched by a "patternProperties".Adds a transformation that removes all unknown keywords from all objects in the schema, i.e. keywords other than those defined by the JSON schema specification.Adds a transformation that removes type-related constraints that cannot apply to the schema's type.requireRootObject(String rootPropertyName) Adds a transformation that requires the root schema to be an object schema that allows only objects, i.e.upgradeToSchemaVersion(EJsonSchemaVersion targetVersion) Adds a transformation that upgrades the schema to the given target version, applying necessary transformations as needed.
-
Method Details
-
build
IJsonSchemaTransformer build()Creates a newIJsonSchemaTransformerinstance with the configured transformation steps and configuration. Subsequent modifications to this builder will not affect the created instance.- Returns:
- The built JSON schema transformer.
-
convertOneOfToAnyOf
Adds a transformation that converts all "oneOf" keywords to "anyOf" keywords, keeping the same subschemas. This can be useful for tools that do not support "oneOf", and "anyOf" is a close approximation. If the schema already has schemas for the "anyOf" keyword, the subschemas from "oneOf" are added to the existing "anyOf" array.- Returns:
- This builder instance for chaining method calls.
-
forbidAdditionalProperties
Adds a transformation step that forbids additional properties in all objects, i.e. sets "additionalProperties" to false in all applicable schemas.- Returns:
- This builder instance for chaining method calls.
-
forbidOptionalProperties
@CanIgnoreReturnValue IJsonSchemaTransformerBuilder forbidOptionalProperties(ForbidOptionalPropertiesMode mode) Adds a transformation step that makes all properties required.- Returns:
- This builder instance for chaining method calls.
-
keepFormats
Adds a transformation that keeps only the given supported formats for the "format" keyword. When the "format" keyword has a value that is not in the given list, it is removed.- Parameters:
formats- The list of supported formats to keep. Each item should either be a string or an object with atoString()that returns the format name. If empty, all "format" keywords are removed.- Returns:
- This builder instance for chaining method calls.
-
keepKeywords
Adds a transformation step that keeps only the given keywords in all objects in the schema, removing all other keywords.- Parameters:
keywords- The keywords to keep. Each item should either be a string or an object with atoString()that returns the keyword name.- Returns:
- This builder instance for chaining method calls.
-
keepKnownKeywords
Adds a transformation that keeps only the given keywords in all objects in the schema, removing all other keywords. Only affects known keywords as defined by the JSON schema specification, does not remove unknown keywords.- Parameters:
keywords- The keywords to keep. Each item should either be a string or an object with atoString()that returns the keyword name.- Returns:
- This builder instance for chaining method calls.
-
moveConstIntoEnum
Adds a transformation step that moves the "const" keyword into an "enum" keyword with a single value; and removes the "const" keyword. Can be useful for tools that only support the "enum" keyword, but not the "const" keyword.- Returns:
- This builder instance for chaining method calls.
-
moveExamplesIntoDescription
Adds a transformation step that moves the "examples" keyword into the "description" keyword, appending the examples to the description. Removes the "examples" keyword afterward. Can be useful for tools that do not support the "examples" keyword. For example, the schema{ "type": "string", "description": "A color", "examples": ["red", "green", "blue"] }would be transformed to{ "type": "string", "description": "A color\n\nExamples:\n- \"red\"\n- \"green\"\n- \"blue\"" }- Returns:
- This builder instance for chaining method calls.
-
prettyPrint
Sets whether to pretty-print the output JSON schema, and if so, with how many spaces to indent. Sets this to 0 to disable pretty-printing. Defaults to 0 (no pretty-printing).- Parameters:
indent- The number of spaces to indent.- Returns:
- This builder instance for chaining method calls.
-
removeFormats
Adds a transformation that removes "format" keyword if its value is one of the given formats.- Parameters:
formats- The list of supported formats to remove. Each item should either be a string or an object with atoString()that returns the format name. If empty, all "format" keywords are removed.- Returns:
- This builder instance for chaining method calls.
-
removeKeywords
Adds a transformation that removes the given keywords from all objects in the schema.- Parameters:
keywords- The keywords to remove. Each item should either be a string or an object with atoString()that returns the keyword name.- Returns:
- This builder instance for chaining method calls.
-
removeRequiredWithoutDefinition
Removes from the "required" array all property names that do not exist in the "properties" object are not matched by a "patternProperties". This can be useful to clean up schemas with "additionalProperties" set to "false" when you expect each property to have additional constraints.- Returns:
- This builder instance for chaining method calls.
-
removeUnknownKeywords
Adds a transformation that removes all unknown keywords from all objects in the schema, i.e. keywords other than those defined by the JSON schema specification. UseremoveKeywords(Iterable)to remove specific known keywords.- Returns:
- This builder instance for chaining method calls.
-
removeUnneededTypeConstraints
Adds a transformation that removes type-related constraints that cannot apply to the schema's type. For example, removes "maxLength" when the "type" keyword is set and does not include "string".- Returns:
- This builder instance for chaining method calls.
-
requireRootObject
Adds a transformation that requires the root schema to be an object schema that allows only objects, i.e. "type": "object". Does nothing if the root schema already fulfills this requirement. Otherwise, wraps the root schema in a new object schema with a single property that has the original root schema as its schema. That property is marked as required, and the object schema does not allow additional properties. For example:{ "type": "string" } --> { "type": "object", "properties": { "root": { "type": "string" } }, "required": ["root"], "additionalProperties": false }- Parameters:
rootPropertyName- The name of the property in the new root object schema that contains the original root schema.- Returns:
- This builder instance for chaining method calls.
-
upgradeToSchemaVersion
@CanIgnoreReturnValue IJsonSchemaTransformerBuilder upgradeToSchemaVersion(EJsonSchemaVersion targetVersion) Adds a transformation that upgrades the schema to the given target version, applying necessary transformations as needed. For example, Draft 2020-12 introduces "prefixItems" to replace the array form of "items", so this performs that conversion when upgrading to Draft 2020-12 or later. Also sets the "$schema" property to the appropriate value for the target version.- Parameters:
targetVersion- The target schema version to upgrade to.- Returns:
- This builder instance for chaining method calls.
-