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;
 import java.util.UUID;

Eighth example: augmenting schemas with custom format attributes

link to source code

link to schema

This example adds a custom format attribute named uuid, which checks whether a string instance is a valid UUID.

For this, you need to write an implementation of com.github.fge.jsonschema.format.FormatAttribute, registering it in a com.github.fge.jsonschema.library.Library, and feed that library to a com.github.fge.jsonschema.cfg.ValidationConfiguration which you submit to the com.github.fge.jsonschema.main.JsonSchemaFactory.

Here, we choose to augment the draft v4 library, which we get hold of using com.github.fge.jsonschema.library.DraftV4Library.get(); we thaw it, add the new attribute and freeze it again. We also choose to make this new library the default by using com.github.fge.jsonschema.cfg.ValidationConfigurationBuilder.setDefaultLibrary(java.lang.String,com.github.fge.jsonschema.library.Library).

Note also that the schema has no $schema defined; as a result, the default library is used (it is not recommended to omit $schema in your schemas, however).

Two sample files are given: the first (link) is valid, the other (link) isn't (the provided id for the second array element is invalid).

 public final class Example8
     public static void main(final String... args)
         throws IOExceptionProcessingException
         final JsonNode customSchema = Utils.loadResource("/custom-fmt.json");
         final JsonNode good = Utils.loadResource("/custom-fmt-good.json");
         final JsonNode bad = Utils.loadResource("/custom-fmt-bad.json");
          * Build a new library with our added format attribute
         final Library library = DraftV4Library.get().thaw()
             .addFormatAttribute("uuid", UUIDFormatAttribute.getInstance())
          * Build a new message bundle with our added error message
         final String key = "invalidUUID";
         final String value = "input is not a valid UUID";
         final MessageSource source = MapMessageSource.newBuilder()
         final MessageBundle bundle
             = MessageBundles.getBundle(JsonSchemaValidationBundle.class)
         * Build our dedicated validation configuration
        final ValidationConfiguration cfg = ValidationConfiguration.newBuilder()
        final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
        final JsonSchema schema = factory.getJsonSchema(customSchema);
        ProcessingReport report;
        report = schema.validate(good);
        report = schema.validate(bad);
    private static final class UUIDFormatAttribute
        extends AbstractFormatAttribute
        private static final FormatAttribute INSTANCE
            = new UUIDFormatAttribute();
        private UUIDFormatAttribute()
        public static FormatAttribute getInstance()
            return ;
        public void validate(final ProcessingReport report,
            final  MessageBundle bundlefinal FullData data)
            throws ProcessingException
            final String value = data.getInstance().getNode().textValue();
            try {
            } catch (IllegalArgumentException ignored) {
New to GrepCode? Check out our FAQ X