use of org.eclipse.wst.json.core.internal.validation.JSONValidationInfo in project webtools.sourceediting by eclipse.
the class Validator method validate.
private void validate(IJSONNode node, JsonObject schema, Member member, JSONValidationInfo valinfo) {
if (IJSONSchemaNode.ALL_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
validate(node, (JsonObject) value, valinfo);
}
}
}
if (IJSONSchemaNode.ANY_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, (JsonObject) value, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
return;
}
}
}
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
if (IJSONSchemaNode.ONE_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
int count = 0;
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, (JsonObject) value, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
count = count + 1;
}
}
}
if (count != 1) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
}
if (IJSONSchemaNode.NOT.equals(member.getName()) && member.getValue() instanceof JsonObject) {
JsonObject json = (JsonObject) member.getValue();
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, json, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
}
if (IJSONSchemaNode.TYPE.equals(member.getName())) {
validateType(node, member, valinfo);
}
if (IJSONSchemaNode.ENUM.equals(member.getName())) {
validateEnum(node, schema, valinfo);
}
if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
if (IJSONSchemaNode.REQUIRED.equals(member.getName())) {
validateRequired(node, schema, valinfo);
}
if (IJSONSchemaNode.MAX_PROPERTIES.equals(member.getName())) {
validateMaxProperties(node, schema, valinfo);
}
if (IJSONSchemaNode.MIN_PROPERTIES.equals(member.getName())) {
validateMinProperties(node, schema, valinfo);
}
if (IJSONSchemaNode.ADDITIONAL_PROPERTIES.equals(member.getName())) {
validateAdditionalProperties(node, schema, member.getValue(), valinfo);
}
}
if (node.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONValue value = ((IJSONPair) node).getValue();
JSONSchemaType[] types = JSONSchemaNode.getType(schema.get(IJSONSchemaNode.TYPE));
if (value != null) {
if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && isType(types, JSONSchemaType.String)) {
validateString(node, schema, member, valinfo, value);
}
if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && (isType(types, JSONSchemaType.Integer) || isType(types, JSONSchemaType.Number))) {
validateNumber(node, schema, member, valinfo, value);
}
if (value.getNodeType() == IJSONNode.ARRAY_NODE && isType(types, JSONSchemaType.Array)) {
validateArray(node, schema, member, valinfo, value);
}
}
}
}
use of org.eclipse.wst.json.core.internal.validation.JSONValidationInfo in project webtools.sourceediting by eclipse.
the class Validator method validate.
@Override
public ValidationReport validate(String uri, InputStream inputstream, NestedValidatorContext context, ValidationResult result) {
JSONValidator validator = JSONValidator.getInstance();
JSONValidationConfiguration configuration = new JSONValidationConfiguration();
JSONValidationReport valreport = validator.validate(uri, inputstream, configuration, result, context);
String prefs = JSONCorePlugin.getDefault().getBundle().getSymbolicName();
IEclipsePreferences modelPreferences = InstanceScope.INSTANCE.getNode(prefs);
boolean validateSchema = modelPreferences.getBoolean(JSONCorePreferenceNames.SCHEMA_VALIDATION, false);
if (validateSchema) {
IJSONModel model = null;
try {
IStructuredModel temp = getModel(uri);
if (!(temp instanceof IJSONModel)) {
return valreport;
}
model = (IJSONModel) temp;
IJSONSchemaDocument schemaDocument = SchemaProcessorRegistryReader.getInstance().getSchemaDocument(model);
if (schemaDocument != null) {
JSONValidationInfo valinfo = null;
if (valreport instanceof JSONValidationInfo) {
valinfo = (JSONValidationInfo) valreport;
} else {
valinfo = new JSONValidationInfo(uri);
}
validate(model, schemaDocument, valinfo);
// valreport.getValidationMessages();
return valreport;
}
} catch (IOException e) {
logWarning(e);
return valreport;
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
return valreport;
}
Aggregations