
com.github.fge.jsonschema.old.syntax.common.EnumSyntaxChecker 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.old.syntax.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.keyword.common.EnumKeywordValidator;
import com.github.fge.jsonschema.keyword.common.UniqueItemsKeywordValidator;
import com.github.fge.jsonschema.report.Message;
import com.github.fge.jsonschema.old.syntax.AbstractSyntaxChecker;
import com.github.fge.jsonschema.old.syntax.SyntaxChecker;
import com.github.fge.jsonschema.old.syntax.SyntaxValidator;
import com.github.fge.jsonschema.util.NodeType;
import com.github.fge.jsonschema.util.equivalence.JsonSchemaEquivalence;
import com.google.common.base.Equivalence;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Set;
/**
* Syntax validator for the {@code enum} keyword
*
* At the moment, this class has the same bug as {@link EnumKeywordValidator}
* and {@link UniqueItemsKeywordValidator} with regards to numeric instances.
*
*/
public final class EnumSyntaxChecker
extends AbstractSyntaxChecker
{
private static final Equivalence EQUIVALENCE
= JsonSchemaEquivalence.getInstance();
private static final SyntaxChecker INSTANCE = new EnumSyntaxChecker();
private EnumSyntaxChecker()
{
super("enum", NodeType.ARRAY);
}
public static SyntaxChecker getInstance()
{
return INSTANCE;
}
@Override
public void checkValue(final SyntaxValidator validator,
final List messages, final JsonNode schema)
{
final JsonNode enumNode = schema.get(keyword);
final Message.Builder msg = newMsg();
if (enumNode.size() == 0) {
msg.setMessage("an enum array must have at least one element");
messages.add(msg.build());
return;
}
/*
* NOTE: we choose NOT to display the culprit element. The (admittedly
* convoluted) reason is that said element, as per enum rules,
* may be an arbitrary JSON document -- ie, as large as you can fathom.
*
* TODO: we may do with displaying the index in the array, that's better
* than nothing...
*/
final Set> values = Sets.newHashSet();
for (final JsonNode value: enumNode) {
if (values.add(EQUIVALENCE.wrap(value)))
continue;
msg.setMessage("elements in the array are not unique");
messages.add(msg.build());
return;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy