All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eel.kitchen.jsonschema.examples.Example8 Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
/*
 * Copyright (c) 2012, Francis Galiegue 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package org.eel.kitchen.jsonschema.examples;

import com.fasterxml.jackson.databind.JsonNode;
import org.eel.kitchen.jsonschema.format.FormatAttribute;
import org.eel.kitchen.jsonschema.main.JsonSchema;
import org.eel.kitchen.jsonschema.main.JsonSchemaFactory;
import org.eel.kitchen.jsonschema.metaschema.KeywordRegistries;
import org.eel.kitchen.jsonschema.metaschema.KeywordRegistry;
import org.eel.kitchen.jsonschema.metaschema.SchemaURIs;
import org.eel.kitchen.jsonschema.ref.JsonRef;
import org.eel.kitchen.jsonschema.report.Message;
import org.eel.kitchen.jsonschema.report.ValidationReport;
import org.eel.kitchen.jsonschema.util.NodeType;

import java.io.IOException;
import java.util.UUID;

import static org.eel.kitchen.jsonschema.main.JsonSchemaFactory.*;

/**
 * Eighth example: augmenting schemas with custom format attributes
 *
 * 

link to source code

* *

link to schema

* *

This example adds a custom format attribute named {@code uuid}, which * checks whether a string instance is a valid UUID.

* *

This kind of customization requires three steps:

* *
    *
  • grabbing a {@link KeywordRegistry};
  • *
  • augmenting it as needed;
  • *
  • registering this registry to the {@link JsonSchemaFactory} (via a * {@link Builder}, as for all customizations) with a matching URI.
  • *
* *

Here, we choose to augment draft v3. We can fetch the URI and default * keyword registry for this version using {@link * KeywordRegistries#draftV3Core()} and {@link SchemaURIs#draftV3Core()} * respectively. Note that the third argument to {@link * Builder#addKeywordRegistry(JsonRef, KeywordRegistry, boolean)} is {@code * true}, since we want to make this schema the default.

* *

Note also that the schema has no {@code $schema} defined; as a result, the * default schema is used (it is not recommended to omit {@code $schema} * in your own schemas).

* *

Adding a custom format attribute is done by extending the {@link * FormatAttribute} class.

* *

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

*/ public final class Example8 extends ExampleBase { public static void main(final String... args) throws IOException { final JsonNode customSchema = loadResource("/custom-fmt.json"); final JsonNode good = loadResource("/custom-fmt-good.json"); final JsonNode bad = loadResource("/custom-fmt-bad.json"); final JsonRef ref = SchemaURIs.draftV3Core(); final KeywordRegistry registry = KeywordRegistries.draftV3Core(); registry.addFormatAttribute("uuid", UUIDFormatAttribute.getInstance()); final JsonSchemaFactory factory = new JsonSchemaFactory.Builder() .addKeywordRegistry(ref, registry, true).build(); final JsonSchema schema = factory.fromSchema(customSchema); ValidationReport report; report = schema.validate(good); printReport(report); report = schema.validate(bad); printReport(report); } private static final class UUIDFormatAttribute extends FormatAttribute { private static final FormatAttribute INSTANCE = new UUIDFormatAttribute(); private UUIDFormatAttribute() { super(NodeType.STRING); } public static FormatAttribute getInstance() { return INSTANCE; } @Override public void checkValue(final String fmt, final ValidationReport report, final JsonNode value) { try { UUID.fromString(value.textValue()); } catch (IllegalArgumentException ignored) { final Message.Builder msg = newMsg(fmt).addInfo("value", value) .setMessage("string is not a valid UUID"); report.addMessage(msg.build()); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy