dev.harrel.jsonschema.VocabularyValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-schema Show documentation
Show all versions of json-schema Show documentation
Library for JSON schema validation
The newest version!
package dev.harrel.jsonschema;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
final class VocabularyValidator {
void validateVocabularies(Dialect dialect, Map vocabularyObject) {
if (vocabularyObject == null || dialect.getSpecificationVersion().getOrder() <= SpecificationVersion.DRAFT7.getOrder()) {
return;
}
List missingRequiredVocabularies = dialect.getRequiredVocabularies().stream()
.filter(vocab -> !vocabularyObject.getOrDefault(vocab, false))
.collect(Collectors.toList());
if (!missingRequiredVocabularies.isEmpty()) {
throw new VocabularyException(String.format("Required vocabularies %s were missing or marked optional in $vocabulary object", missingRequiredVocabularies));
}
List unsupportedRequiredVocabularies = vocabularyObject.entrySet().stream()
.filter(Map.Entry::getValue)
.map(Map.Entry::getKey)
.filter(vocab -> !dialect.getSupportedVocabularies().contains(vocab))
.collect(Collectors.toList());
if (!unsupportedRequiredVocabularies.isEmpty()) {
throw new VocabularyException(String.format("Following vocabularies %s are required but not supported", unsupportedRequiredVocabularies));
}
}
}