Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
   *
   * 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 both licenses is available under the src/resources/ directory of
  * this project (under the names LGPL-3.0.txt and ASL-2.0.txt respectively).
  *
  * Direct link to the sources:
  *
  * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
  * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
  */
 
 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
         = MessageBundles.getBundle(JsonSchemaConfigurationBundle.class);
     private static final MessageBundle CORE_BUNDLE
         = MessageBundles.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 IllegalStateException("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