Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (c) 2014, Francis Galiegue (
  * This software is dual-licensed under:
  * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
  *   later version;
  * - the Apache Software License (ASL) version 2.0.
 * The text of this file and of both licenses is available at the root of this
 * project or, if you have the jar distribution, in directory META-INF/, under
 * the names LGPL-3.0.txt and ASL-2.0.txt respectively.
 * Direct link to the sources:
 * - LGPL 3.0:
 * - ASL 2.0:
package com.github.fge.jsonschema.examples;
Tenth example: registering schemas

link to source code

In this example, we register a custom schema with a given URI, and initiate the com.github.fge.jsonschema.main.JsonSchema instance using that URI. This is done by customizing a com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration and registering schemas using com.github.fge.jsonschema.core.load.configuration.LoadingConfigurationBuilder.preloadSchema(java.lang.String,com.fasterxml.jackson.databind.JsonNode).

The only necessary condition for the URI is for it to be an absolute JSON reference (see com.github.fge.jsonschema.core.ref.JsonRef.isAbsolute()), and you can register as many schemas as you want. Here, we register both schemas from Example5. You will notice that the scheme for these URIs is xxx: it does not matter in the slightest that it is not a supported scheme by default, the schema is registered all the same.

This also shows that reference resolution still works in such a case, since the mntent schema is referred to via a relative URI from the fstab schema.

public final class Example10
    private static final String URI_BASE = "xxx://";
    public static void main(final String... args)
        throws IOExceptionProcessingException
        final LoadingConfigurationBuilder builder
            = LoadingConfiguration.newBuilder();
        JsonNode node;
        String uri;
        node = Utils.loadResource("/split/fstab.json");
        uri =  + "fstab.json";
        node = Utils.loadResource("/split/mntent.json");
        uri =  + "mntent.json";
        final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
        final JsonSchema schema
            = factory.getJsonSchema( + "fstab.json");
        final JsonNode good = Utils.loadResource("/fstab-good.json");
        final JsonNode bad = Utils.loadResource("/fstab-bad.json");
        final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
        ProcessingReport report;
        report = schema.validate(good);
        report = schema.validate(bad);
        report = schema.validate(bad2);
New to GrepCode? Check out our FAQ X