json schema, show validation result to the user, correct validation for field

This commit is contained in:
irengrig
2016-02-16 00:49:36 +01:00
parent 0de4647f9a
commit 70848c8a5b
3 changed files with 25 additions and 5 deletions
@@ -7,6 +7,7 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.CollectConsumer;
import com.jetbrains.jsonSchema.impl.JsonSchemaReader;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -14,6 +15,7 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
/**
* @author Irina.Chernushina on 2/2/2016.
@@ -97,8 +99,18 @@ public class JsonSchemaConfigurable extends NamedConfigurable<JsonSchemaMappings
if (StringUtil.isEmptyOrSpaces(myDisplayName)) throw new ConfigurationException("Schema name is empty");
if (StringUtil.isEmptyOrSpaces(myView.getSchemaSubPath())) throw new ConfigurationException("Schema path is empty");
if (!Comparing.equal(myView.getSchemaSubPath(), mySchema.getRelativePathToSchema())) {
if (!JsonSchemaReader.isJsonSchema(new File(myProject.getBasePath(), myView.getSchemaSubPath()).getPath())) {
throw new ConfigurationException("Can not read JSON schema from file");
final CollectConsumer<String> collectConsumer = new CollectConsumer<>();
final File file = new File(myProject.getBasePath(), myView.getSchemaSubPath());
try {
if (!JsonSchemaReader.isJsonSchema(FileUtil.loadFile(file), collectConsumer)) {
final String message;
if (collectConsumer.getResult().isEmpty()) message = "Can not read JSON schema from file (Unknown reason)";
else message = "Can not read JSON schema from file: " + StringUtil.join(collectConsumer.getResult(), "; ");
throw new ConfigurationException(message);
}
}
catch (IOException e) {
throw new ConfigurationException("Can not read JSON schema from file: " + e.getMessage());
}
}
}
@@ -17,6 +17,7 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.CollectConsumer;
import com.intellij.util.IconUtil;
import com.jetbrains.jsonSchema.ide.JsonSchemaService;
import com.jetbrains.jsonSchema.impl.JsonSchemaReader;
@@ -383,8 +384,12 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple
myError = "Problem during reading JSON schema from '" + myFile.getName() + "': " + e1.getMessage();
return false;
}
if (!JsonSchemaReader.isJsonSchema(myText)) {
myError = "JSON Schema not found in '" + myFile.getName() + "'";
final CollectConsumer<String> collectConsumer = new CollectConsumer<>();
if (!JsonSchemaReader.isJsonSchema(myText, collectConsumer)) {
myError = "JSON Schema not found or contain error in '" + myFile.getName() + "'";
if (!collectConsumer.getResult().isEmpty()) {
myError += ": " + StringUtil.join(collectConsumer.getResult(), "; ");
}
return false;
}
if (!myLoadText) myText = null;
@@ -4,6 +4,7 @@ import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.intellij.util.Consumer;
import com.intellij.util.ThrowablePairConsumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -34,13 +35,15 @@ public class JsonSchemaReader {
return object;
}
public static boolean isJsonSchema(@NotNull final String string) {
public static boolean isJsonSchema(@NotNull final String string, Consumer<String> errorConsumer) {
try {
new JsonSchemaReader().read(new java.io.StringReader(string));
return true;
} catch (IOException e) {
errorConsumer.consume(e.getMessage());
return false;
} catch (Exception e) {
errorConsumer.consume(e.getMessage());
return false;
}
}