Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2012, 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.validator;
 
 
 import java.util.Map;
 import java.util.Set;

Validator called for object instance children

Unlike what happens with arrays, a same child/value instance of an object may have to satisfy more than one schema. For a given property name, the list of schemas is constructed as follows:

  • if the property name has an exact match in properties, the corresponding schema is added to the list;
  • for all regexes in patternProperties, if the property name matches the regex, the corresponding schema is added to the list;
  • if, at this point, the list is empty, then the contents of additionalProperties is added to the list (an empty schema if additionalProperties is either true or nonexistent).
 
 final class ObjectValidator
     implements JsonValidator
 {
     private final JsonNode additionalProperties;
     private final Map<StringJsonNodeproperties;
     private final Map<StringJsonNodepatternProperties;
 
     ObjectValidator(final JsonNode schema)
     {
         JsonNode node;
 
         node = schema.path("additionalProperties");
          = node.isObject() ? node
             : JacksonUtils.emptyObject();
 
         node = schema.path("properties");
          = JacksonUtils.asMap(node);
 
         node = schema.path("patternProperties");
          = JacksonUtils.asMap(node);
     }
 
     @Override
     public void validate(final ValidationContext context,
         final ValidationReport reportfinal JsonNode instance)
     {
         final JsonPointer pwd = report.getPath();
         final Map<StringJsonNodemap = JacksonUtils.asMap(instance);
 
         for (final Map.Entry<StringJsonNodeentrymap.entrySet()) {
             report.setPath(pwd.append(entry.getKey()));
             if (!validateOne(contextreportentry))
                 break;
         }
 
         report.setPath(pwd);
     }
 
     private boolean validateOne(final ValidationContext context,
         final ValidationReport reportfinal Map.Entry<StringJsonNodeentry)
     {
         final String key = entry.getKey();
         final JsonNode value = entry.getValue();
         final Set<JsonNodesubSchemas = getSchemas(key);
 
         JsonValidator validator;
 
         for (final JsonNode subSchemasubSchemas) {
             validator = context.newValidator(subSchema);
             validator.validate(contextreportvalue);
            if (report.hasFatalError())
                return false;
        }
        return true;
    }
    Set<JsonNodegetSchemas(final String key)
    {
        final Set<JsonNoderet = Sets.newHashSet();
        if (.containsKey(key))
            ret.add(.get(key));
        for (final Map.Entry<StringJsonNodeentry:
            .entrySet())
            if (RhinoHelper.regMatch(entry.getKey(), key))
                ret.add(entry.getValue());
        if (ret.isEmpty())
            ret.add();
        return ImmutableSet.copyOf(ret);
    }
New to GrepCode? Check out our FAQ X