Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2013, Francis Galiegue <fgaliegue@gmail.com>
   *
   * 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 <http://www.gnu.org/licenses/>.
  */
 
 package com.github.fge.jsonschema.main;
 
 
 import  javax.annotation.concurrent.Immutable;
 import java.util.Map;

The main validator provider

From an instance of this factory, you can obtain the following:

  • a SyntaxValidator, to validate schemas;
  • a JsonValidator, to validate an instance against a schema;
  • a JsonSchema, to validate instances against a fixed schema.

See also:
JsonSchemaFactoryBuilder
 
 @Immutable
 public final class JsonSchemaFactory
     implements Frozen<JsonSchemaFactoryBuilder>
 {
     private static final MessageBundle BUNDLE
         = MessageBundleFactory.getBundle(JsonSchemaConfigurationBundle.class);
     private static final MessageBundle CORE_BUNDLE
         = MessageBundleFactory.getBundle(JsonSchemaCoreMessageBundle.class);
 
     private static final Function<SchemaContextJsonRefFUNCTION
         = new Function<SchemaContextJsonRef>()
     {
         @Override
         public JsonRef apply(final SchemaContext input)
         {
             return input.getSchema().getDollarSchema();
         }
     };
 
     /*
      * Elements provided by the builder
      */
     final ReportProvider reportProvider;
 
     /*
      * Generated elements
      */
     private final SchemaLoader loader;
     private final JsonValidator validator;
     private final SyntaxValidator syntaxValidator;

    
Return a default factory

This default factory has validators for both draft v4 and draft v3. It defaults to draft v4.

Returns:
a factory with default settings
See also:
JsonSchemaFactoryBuilder.JsonSchemaFactoryBuilder()
    public static JsonSchemaFactory byDefault()
    {
        return newBuilder().freeze();
    }

    
Return a factory builder

Returns:
a JsonSchemaFactoryBuilder
    public static JsonSchemaFactoryBuilder newBuilder()
    {
        return new JsonSchemaFactoryBuilder();
    }

    
Package private constructor to build a factory out of a builder

Parameters:
builder the builder
See also:
JsonSchemaFactoryBuilder.freeze()
    {
         = builder.reportProvider;
         = builder.loadingCfg;
         = builder.validationCfg;
         = new SchemaLoader();
        final Processor<SchemaContextValidatorListprocessor
            = buildProcessor();
         = new JsonValidator(,
            new ValidationProcessor(processor), );
    }

    
Return the main schema/instance validator provided by this factory

Returns:
a JsonValidator
    public JsonValidator getValidator()
    {
        return ;
    }

    
Return the syntax validator provided by this factory

Returns:
a SyntaxValidator
    {
        return ;
    }

    
Build an instance validator tied to a schema

Note that the validity of the schema is not checked. Use getSyntaxValidator() if you are not sure.

Parameters:
schema the schema
Returns:
a JsonSchema
Throws:
ProcessingException schema is a MissingNode
NullPointerException schema is null
    public JsonSchema getJsonSchema(final JsonNode schema)
        throws ProcessingException
    {
        .checkNotNull(schema"nullSchema");
        return .buildJsonSchema(schema, JsonPointer.empty());
    }

    
Build an instance validator tied to a subschema from a main schema

Note that the validity of the schema is not checked. Use getSyntaxValidator() if you are not sure.

Parameters:
schema the schema
ptr a JSON Pointer as a string
Returns:
a JsonSchema
Throws:
ProcessingException ptr is not a valid JSON Pointer, or resolving the pointer against the schema leads to a MissingNode
NullPointerException schema is null, or pointer is null
    public JsonSchema getJsonSchema(final JsonNode schemafinal String ptr)
        throws ProcessingException
    {
        .checkNotNull(schema"nullSchema");
        .checkNotNull(ptr"nullPointer");
        final JsonPointer pointer;
        try {
            pointer = new JsonPointer(ptr);
            return .buildJsonSchema(schemapointer);
        } catch (JsonPointerException ignored) {
            // Cannot happen
        }
        throw new ProcessingError(new ProcessingMessage()
            .setMessage("How did I get there??"));
    }

    
Build an instance validator out of a schema loaded from a URI

Parameters:
uri the URI
Returns:
a JsonSchema
Throws:
ProcessingException failed to load from this URI
NullPointerException URI is null
    public JsonSchema getJsonSchema(final String uri)
        throws ProcessingException
    {
        .checkNotNull(uri"nullURI");
        return .buildJsonSchema(uri);
    }

    
Return the raw validation processor

This will allow you to chain the full validation processor with other processors of your choice. Useful if, for instance, you wish to add post checking which JSON Schema cannot do by itself.

Returns:
the processor.
    {
        return .getProcessor();
    }

    
Return a thawed instance of that factory

Returns:
a JsonSchemaFactoryBuilder
See also:
JsonSchemaFactoryBuilder.JsonSchemaFactoryBuilder(JsonSchemaFactory)
    @Override
    {
        return new JsonSchemaFactoryBuilder(this);
    }
    {
        final RefResolver resolver = new RefResolver();
        final Map<JsonRefLibrarylibraries = .getLibraries();
        final Library defaultLibrary = .getDefaultLibrary();
        final ValidationChain defaultChain
            = new ValidationChain(resolverdefaultLibrary);
        final ProcessorMap<JsonRefSchemaContextValidatorListmap
            = new ProcessorMap<JsonRefSchemaContextValidatorList>();
        map.setDefaultProcessor(defaultChain);
        JsonRef ref;
        ValidationChain chain;
        for (final Map.Entry<JsonRefLibraryentrylibraries.entrySet()) {
            ref = entry.getKey();
            chain = new ValidationChain(resolverentry.getValue(),
                );
            map.addEntry(refchain);
        }
        return map.getProcessor();
    }
New to GrepCode? Check out our FAQ X