com.github.fge.jsonschema.validator.ObjectValidator 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.validator;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.report.ValidationReport;
import com.github.fge.jsonschema.util.RhinoHelper;
import com.github.fge.jsonschema.util.jackson.JacksonUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
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 {@code properties},
* the corresponding schema is added to the list;
* - for all regexes in {@code 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
* {@code additionalProperties} is added to the list (an empty schema if
* {@code additionalProperties} is either {@code true} or nonexistent).
*
*
*/
final class ObjectValidator
implements JsonValidator
{
private final JsonNode additionalProperties;
private final Map properties;
private final Map patternProperties;
ObjectValidator(final JsonNode schema)
{
JsonNode node;
node = schema.path("additionalProperties");
additionalProperties = node.isObject() ? node
: JacksonUtils.emptyObject();
node = schema.path("properties");
properties = JacksonUtils.asMap(node);
node = schema.path("patternProperties");
patternProperties = JacksonUtils.asMap(node);
}
@Override
public void validate(final ValidationContext context,
final ValidationReport report, final JsonNode instance)
{
final Map map = JacksonUtils.asMap(instance);
boolean keepGoing;
for (final Map.Entry entry: map.entrySet()) {
report.pushd(entry.getKey());
keepGoing = validateOne(context, report, entry);
report.popd();
if (!keepGoing)
break;
}
}
private boolean validateOne(final ValidationContext context,
final ValidationReport report, final Map.Entry entry)
{
final String key = entry.getKey();
final JsonNode value = entry.getValue();
final Set subSchemas = getSchemas(key);
JsonValidator validator;
for (final JsonNode subSchema: subSchemas) {
validator = context.newValidator(subSchema);
validator.validate(context, report, value);
if (report.hasFatalError())
return false;
}
return true;
}
@VisibleForTesting
Set getSchemas(final String key)
{
final Set ret = Sets.newHashSet();
if (properties.containsKey(key))
ret.add(properties.get(key));
for (final Map.Entry entry:
patternProperties.entrySet())
if (RhinoHelper.regMatch(entry.getKey(), key))
ret.add(entry.getValue());
if (ret.isEmpty())
ret.add(additionalProperties);
return ImmutableSet.copyOf(ret);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy