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

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

There is a newer version: 2.2.6
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 com.github.fge.jsonschema.examples;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.cfg.ValidationConfigurationBuilder;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.format.AbstractFormatAttribute;
import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.library.DraftV4Library;
import com.github.fge.jsonschema.library.Library;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.jsonschema.report.ProcessingReport;
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.

* *

For this, you need to write an implementation of {@link FormatAttribute}, * registering it in a {@link Library}, and feed that library to a {@link * ValidationConfiguration} which you submit to the {@link JsonSchemaFactory}. *

* *

Here, we choose to augment the draft v4 library, which we get hold of * using {@link 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 {@link * ValidationConfigurationBuilder#setDefaultLibrary(String, Library)}.

* *

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

* *

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, ProcessingException { final JsonNode customSchema = loadResource("/custom-fmt.json"); final JsonNode good = loadResource("/custom-fmt-good.json"); final JsonNode bad = loadResource("/custom-fmt-bad.json"); final Library library = DraftV4Library.get().thaw() .addFormatAttribute("uuid", UUIDFormatAttribute.getInstance()) .freeze(); final ValidationConfiguration cfg = ValidationConfiguration.newBuilder() .setDefaultLibrary("http://my.site/myschema#", library).freeze(); final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder() .setValidationConfiguration(cfg).freeze(); final JsonSchema schema = factory.getJsonSchema(customSchema); ProcessingReport report; report = schema.validate(good); printReport(report); report = schema.validate(bad); printReport(report); } private static final class UUIDFormatAttribute extends AbstractFormatAttribute { private static final FormatAttribute INSTANCE = new UUIDFormatAttribute(); private UUIDFormatAttribute() { super("uuid", NodeType.STRING); } public static FormatAttribute getInstance() { return INSTANCE; } @Override public void validate(final ProcessingReport report, final FullData data) throws ProcessingException { final String value = data.getInstance().getNode().textValue(); try { UUID.fromString(value); } catch (IllegalArgumentException ignored) { report.error(newMsg(data, "input is not a valid UUID") .put("input", value)); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy