CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-github-fge--json-schema-validator

A comprehensive Java implementation of the JSON Schema validation specification supporting both draft v4 and v3 with complete validation capabilities and extensibility.

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration and Customization

Advanced configuration options for customizing validation behavior, including schema loading, validation rules, library management, and message customization. The configuration system allows fine-grained control over how schemas are loaded, validated, and reported.

Capabilities

ValidationConfiguration

Central configuration for controlling validation behavior and rule sets.

/**
 * Immutable configuration for validation behavior and library management
 */
public final class ValidationConfiguration {
    /**
     * Get default validation configuration
     * @return Default ValidationConfiguration instance
     */
    public static ValidationConfiguration byDefault();
    
    /**
     * Create new configuration builder
     * @return ValidationConfigurationBuilder for customization
     */
    public static ValidationConfigurationBuilder newBuilder();
    
    /**
     * Check if format validation is enabled
     * @return true if format attributes are validated
     */
    public boolean getUseFormat();
    
    /**
     * Get the default keyword library
     * @return Library containing default validation keywords
     */
    public Library getDefaultLibrary();
    
    /**
     * Get all configured libraries mapped by schema URI
     * @return Map of JsonRef to Library instances
     */
    public Map<JsonRef, Library> getLibraries();
    
    /**
     * Get message bundle for syntax error messages
     * @return MessageBundle for syntax validation messages
     */
    public MessageBundle getSyntaxMessages();
    
    /**
     * Get message bundle for validation error messages
     * @return MessageBundle for validation error messages
     */
    public MessageBundle getValidationMessages();
    
    /**
     * Create mutable copy for modification
     * @return ValidationConfigurationBuilder with current settings
     */
    public ValidationConfigurationBuilder thaw();
}

ValidationConfigurationBuilder

Builder for creating customized validation configurations.

/**
 * Builder for creating custom validation configurations
 */
public final class ValidationConfigurationBuilder {
    /**
     * Set the default JSON Schema version to use
     * @param version SchemaVersion (DRAFTV3 or DRAFTV4)
     * @return This builder for method chaining
     */
    public ValidationConfigurationBuilder setDefaultVersion(SchemaVersion version);
    
    /**
     * Set default library for validation keywords
     * @param uri Schema URI string
     * @param library Library instance to use as default
     * @return This builder for method chaining
     */
    public ValidationConfigurationBuilder setDefaultLibrary(String uri, Library library);
    
    /**
     * Add custom library for specific schema version
     * @param uri Schema URI string
     * @param library Library instance to register
     * @return This builder for method chaining
     */
    public ValidationConfigurationBuilder addLibrary(String uri, Library library);
    
    /**
     * Enable or disable format attribute validation
     * @param useFormat true to enable format validation
     * @return This builder for method chaining
     */
    public ValidationConfigurationBuilder setUseFormat(boolean useFormat);
    
    /**
     * Set custom message bundle for syntax errors
     * @param syntaxMessages MessageBundle for syntax error messages
     * @return This builder for method chaining
     */
    public ValidationConfigurationBuilder setSyntaxMessages(MessageBundle syntaxMessages);
    
    /**
     * Set custom message bundle for validation errors
     * @param validationMessages MessageBundle for validation error messages
     * @return This builder for method chaining
     */
    public ValidationConfigurationBuilder setValidationMessages(MessageBundle validationMessages);
    
    /**
     * Create immutable configuration instance
     * @return ValidationConfiguration with applied settings
     */
    public ValidationConfiguration freeze();
}

Usage Examples:

import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.cfg.ValidationConfigurationBuilder;
import com.github.fge.jsonschema.library.DraftV4Library;
import com.github.fge.jsonschema.core.messages.JsonSchemaValidationBundle;

// Custom validation configuration
ValidationConfiguration config = ValidationConfiguration.newBuilder()
    .setDefaultVersion(SchemaVersion.DRAFTV4)
    .setUseFormat(false)  // Disable format validation for performance
    .addLibrary("http://json-schema.org/draft-04/schema#", DraftV4Library.get())
    .freeze();

// Use custom configuration with factory
JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setValidationConfiguration(config)
    .freeze();

// Configuration with custom messages
MessageBundle customMessages = JsonSchemaValidationBundle.getInstance();
ValidationConfiguration msgConfig = ValidationConfiguration.newBuilder()
    .setSyntaxMessages(customMessages)
    .setValidationMessages(customMessages)
    .freeze();

// Modify existing configuration
ValidationConfiguration existing = ValidationConfiguration.byDefault();
ValidationConfiguration modified = existing.thaw()
    .setUseFormat(true)
    .freeze();

LoadingConfiguration

Configuration for controlling how schemas are loaded and dereferenced.

/**
 * Configuration for schema loading and URI resolution behavior
 */
public final class LoadingConfiguration {
    /**
     * Get default loading configuration
     * @return Default LoadingConfiguration instance
     */
    public static LoadingConfiguration byDefault();
    
    /**
     * Create new loading configuration builder
     * @return LoadingConfigurationBuilder for customization
     */
    public static LoadingConfigurationBuilder newBuilder();
    
    /**
     * Get URI translator configuration
     * @return URITranslatorConfiguration for URI handling
     */
    public URITranslatorConfiguration getTranslatorConfiguration();
    
    /**
     * Get dereferencing mode
     * @return Dereferencing mode (CANONICAL or INLINE)
     */
    public Dereferencing getDereferencing();
    
    /**
     * Get schema downloaders for different URI schemes
     * @return Map of scheme to URIDownloader instances
     */
    public Map<String, URIDownloader> getDownloaders();
    
    /**
     * Create mutable copy for modification
     * @return LoadingConfigurationBuilder with current settings
     */
    public LoadingConfigurationBuilder thaw();
}

/**
 * Builder for creating custom loading configurations
 */
public final class LoadingConfigurationBuilder {
    /**
     * Set dereferencing mode for JSON reference resolution
     * @param dereferencing Dereferencing mode (CANONICAL or INLINE)
     * @return This builder for method chaining
     */
    public LoadingConfigurationBuilder dereferencing(Dereferencing dereferencing);
    
    /**
     * Set URI translator configuration for namespace handling
     * @param translatorConfiguration URITranslatorConfiguration instance
     * @return This builder for method chaining
     */
    public LoadingConfigurationBuilder setURITranslatorConfiguration(URITranslatorConfiguration translatorConfiguration);
    
    /**
     * Add custom URI downloader for specific scheme
     * @param scheme URI scheme (http, https, file, etc.)
     * @param downloader URIDownloader implementation
     * @return This builder for method chaining
     */
    public LoadingConfigurationBuilder addScheme(String scheme, URIDownloader downloader);
    
    /**
     * Create immutable loading configuration
     * @return LoadingConfiguration with applied settings
     */
    public LoadingConfiguration freeze();
}

Usage Examples:

import com.github.fge.jsonschema.core.load.Dereferencing;
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration;
import com.github.fge.jsonschema.core.load.uri.URITranslatorConfiguration;

// Configure inline dereferencing for schemas with id-based references
LoadingConfiguration inlineConfig = LoadingConfiguration.newBuilder()
    .dereferencing(Dereferencing.INLINE)
    .freeze();

// Set up URI namespace for relative schema references
URITranslatorConfiguration uriConfig = URITranslatorConfiguration.newBuilder()
    .setNamespace("resource:/schemas/")
    .freeze();

LoadingConfiguration namespaceConfig = LoadingConfiguration.newBuilder()
    .setURITranslatorConfiguration(uriConfig)
    .freeze();

// Use with factory
JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setLoadingConfiguration(inlineConfig)
    .freeze();

// Custom URI downloader for specific scheme
LoadingConfiguration customDownloadConfig = LoadingConfiguration.newBuilder()
    .addScheme("custom", new CustomURIDownloader())
    .freeze();

SchemaVersion Enumeration

/**
 * Supported JSON Schema specification versions
 */
public enum SchemaVersion {
    /**
     * JSON Schema Draft v3 specification
     */
    DRAFTV3("http://json-schema.org/draft-03/schema#"),
    
    /**
     * JSON Schema Draft v4 specification (default)
     */
    DRAFTV4("http://json-schema.org/draft-04/schema#");
    
    /**
     * Get the schema URI for this version
     * @return Schema URI string
     */
    public String getLocation();
}

URITranslatorConfiguration

Configuration for URI translation and namespace handling.

/**
 * Configuration for URI translation and namespace resolution
 */
public final class URITranslatorConfiguration {
    /**
     * Create new URI translator configuration builder
     * @return URITranslatorConfigurationBuilder for customization
     */
    public static URITranslatorConfigurationBuilder newBuilder();
    
    /**
     * Get the configured namespace URI
     * @return Namespace URI string or null if not set
     */
    public String getNamespace();
    
    /**
     * Get path redirections map
     * @return Map of path redirections
     */
    public Map<String, String> getPathRedirections();
}

/**
 * Builder for URI translator configuration
 */
public final class URITranslatorConfigurationBuilder {
    /**
     * Set namespace for relative URI resolution
     * @param namespace Base namespace URI
     * @return This builder for method chaining
     */
    public URITranslatorConfigurationBuilder setNamespace(String namespace);
    
    /**
     * Add path redirection mapping
     * @param from Source path pattern
     * @param to Target path pattern
     * @return This builder for method chaining
     */
    public URITranslatorConfigurationBuilder addPathRedirection(String from, String to);
    
    /**
     * Create immutable URI translator configuration
     * @return URITranslatorConfiguration with applied settings
     */
    public URITranslatorConfiguration freeze();
}

Dereferencing Modes

/**
 * JSON reference dereferencing modes
 */
public enum Dereferencing {
    /**
     * Canonical dereferencing using $ref resolution
     */
    CANONICAL,
    
    /**
     * Inline dereferencing using id-based resolution
     */
    INLINE;
}

Complete Configuration Examples

Example 1: Custom Validation with Disabled Format Validation

// Create configuration that disables format validation for performance
ValidationConfiguration config = ValidationConfiguration.newBuilder()
    .setUseFormat(false)
    .setDefaultVersion(SchemaVersion.DRAFTV4)
    .freeze();

JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setValidationConfiguration(config)
    .freeze();

JsonSchema schema = factory.getJsonSchema(schemaNode);

Example 2: Schema Loading with Namespace

// Set up namespace for loading schemas from a specific location
URITranslatorConfiguration uriConfig = URITranslatorConfiguration.newBuilder()
    .setNamespace("resource:/com/example/schemas/")
    .freeze();

LoadingConfiguration loadingConfig = LoadingConfiguration.newBuilder()
    .setURITranslatorConfiguration(uriConfig)
    .freeze();

JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setLoadingConfiguration(loadingConfig)
    .freeze();

// Now can load schemas using relative URIs
JsonSchema schema = factory.getJsonSchema("user-schema.json");

Example 3: Inline Dereferencing for ID-based Schemas

// Configure for schemas that use "id" properties for inline references
LoadingConfiguration inlineConfig = LoadingConfiguration.newBuilder()
    .dereferencing(Dereferencing.INLINE)
    .freeze();

JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setLoadingConfiguration(inlineConfig)
    .freeze();

Example 4: Complete Custom Configuration

// Comprehensive custom configuration
ValidationConfiguration validationConfig = ValidationConfiguration.newBuilder()
    .setDefaultVersion(SchemaVersion.DRAFTV4)
    .setUseFormat(true)
    .addLibrary("http://json-schema.org/draft-04/schema#", DraftV4Library.get())
    .freeze();

URITranslatorConfiguration uriConfig = URITranslatorConfiguration.newBuilder()
    .setNamespace("http://example.com/schemas/")
    .addPathRedirection("/old-path/", "/new-path/")
    .freeze();

LoadingConfiguration loadingConfig = LoadingConfiguration.newBuilder()
    .setURITranslatorConfiguration(uriConfig)
    .dereferencing(Dereferencing.CANONICAL)
    .freeze();

JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setValidationConfiguration(validationConfig)
    .setLoadingConfiguration(loadingConfig)
    .freeze();

Configuration Best Practices

  1. Performance: Disable format validation with setUseFormat(false) if you don't need format checking
  2. Reusability: Create factories with specific configurations and reuse them across your application
  3. Schema Organization: Use namespaces to organize schemas in a logical directory structure
  4. Version Control: Explicitly set schema versions to ensure consistent validation behavior
  5. Error Handling: Configure custom message bundles for internationalization or application-specific error messages

Install with Tessl CLI

npx tessl i tessl/maven-com-github-fge--json-schema-validator

docs

basic-validation.md

cli.md

configuration.md

extensions.md

format-validation.md

index.md

syntax-validation.md

tile.json