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

com.github.fge.jsonschema.examples.Example8 Maven / Gradle / Ivy

/*
 * 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 com.github.fge.jsonschema.examples;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.metaschema.BuiltinSchemas;
import com.github.fge.jsonschema.metaschema.MetaSchema;
import com.github.fge.jsonschema.report.Message;
import com.github.fge.jsonschema.report.ValidationReport;
import com.github.fge.jsonschema.util.NodeType;

import java.io.IOException;
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 {@code uuid}, which * checks whether a string instance is a valid UUID.

* *

This kind of customization requires three steps:

* *
    *
  • grabbing a * {@link com.github.fge.jsonschema.metaschema.MetaSchema.Builder};
  • *
  • augmenting it as needed;
  • *
  • registering it to the {@link JsonSchemaFactory} (via a {@link * com.github.fge.jsonschema.main.JsonSchemaFactory.Builder}, as for all * customizations).
  • *
* *

Here, we choose to augment the draft v3 core schema. We can base our new * meta-schema by using {@link MetaSchema#basedOn(BuiltinSchemas)} with * {@link BuiltinSchemas#DRAFTV3_CORE} as an argument, add our format attribute * to it, build it and add it to our factory, using {@link * com.github.fge.jsonschema.main.JsonSchemaFactory.Builder#addMetaSchema * (MetaSchema, boolean)}. Note that the * second argument is {@code true} so that our new meta-schema is regarded as * the default.

* *

Note also that the schema has no {@code $schema} defined; as a result, the * default meta-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 MetaSchema metaSchema = MetaSchema.basedOn(BuiltinSchemas.DRAFTV3_CORE) .addFormatAttribute("uuid", UUIDFormatAttribute.getInstance()) .build(); final JsonSchemaFactory factory = JsonSchemaFactory.builder() .addMetaSchema(metaSchema, 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 - 2025 Weber Informatics LLC | Privacy Policy