Improve error messaging when parsing schemas and documents

This commit is contained in:
Tristan Penman 2022-02-06 20:14:37 +11:00
parent dd32f66df8
commit d34f78b435
2 changed files with 44 additions and 37 deletions

View File

@ -47,8 +47,10 @@ Window::Window(QWidget * parent)
setCentralWidget(verticalSplitter);
setStatusBar(statusBar);
connect(m_documentEditor, SIGNAL(textChanged()), this, SLOT(refreshDocumentJson()));
connect(m_schemaEditor, SIGNAL(textChanged()), this, SLOT(refreshSchemaJson()));
connect(m_documentEditor, SIGNAL(textChanged()), this, SLOT(refreshJson()));
connect(m_schemaEditor, SIGNAL(textChanged()), this, SLOT(refreshJson()));
refreshJson();
}
QTextEdit * Window::createEditor(bool readOnly)
@ -114,50 +116,55 @@ QToolBar * Window::createToolBar()
return toolbar;
}
void Window::refreshDocumentJson()
void Window::refreshJson()
{
const auto doc = m_documentEditor->toPlainText().toUtf8();
if (doc.isEmpty()) {
QString errors;
m_errors->setText("");
return;
}
QJsonParseError error;
m_document = QJsonDocument::fromJson(doc, &error);
if (m_document.isNull()) {
m_errors->setText(QString("Document error: ") + error.errorString());
return;
}
if (m_schema) {
validate();
} else {
m_errors->setText("");
}
}
void Window::refreshSchemaJson()
{
const auto schema = m_schemaEditor->toPlainText().toUtf8();
const auto doc = m_documentEditor->toPlainText().toUtf8();
if (schema.isEmpty()) {
m_errors->setText("");
if (doc.isEmpty()) {
m_errors->setText(
"Please provide a schema and a document to be validated.\n\n"
"Note that this example uses QtJson, which does not consider non-array and "
"non-object values to be valid JSON documents.");
return;
} else {
errors += "Schema error: must not be empty\n\n";
}
} else {
QJsonParseError error;
m_schemaJson = QJsonDocument::fromJson(schema, &error);
if (m_schemaJson.isNull()) {
errors += QString("Schema error: ") + error.errorString() + "\n\n";
}
}
if (doc.isEmpty()) {
if (!schema.isEmpty()) {
errors += "Document error: must not be empty\n\n";
}
} else {
QJsonParseError error;
auto schemaDoc = QJsonDocument::fromJson(m_schemaEditor->toPlainText().toUtf8(), &error);
if (schemaDoc.isNull()) {
m_errors->setText(QString("Schema error: ") + error.errorString());
m_documentJson = QJsonDocument::fromJson(doc, &error);
if (m_documentJson.isNull()) {
errors += QString("Document error: ") + error.errorString() + "\n\n";
}
}
if (!errors.isEmpty()) {
m_errors->setText(errors);
return;
}
try {
valijson::adapters::QtJsonAdapter adapter(schemaDoc.object());
valijson::adapters::QtJsonAdapter adapter(m_schemaJson.object());
valijson::SchemaParser parser;
delete m_schema;
m_schema = new valijson::Schema();
parser.populateSchema(adapter, *m_schema);
m_errors->setText("");
validate();
} catch (std::runtime_error & error) {
delete m_schema;
@ -190,7 +197,7 @@ void Window::validate()
{
valijson::ValidationResults results;
valijson::Validator validator;
valijson::adapters::QtJsonAdapter adapter(m_document.object());
valijson::adapters::QtJsonAdapter adapter(m_documentJson.object());
if (validator.validate(*m_schema, adapter, &results)) {
m_errors->setText("Document is valid.");

View File

@ -22,8 +22,7 @@ public:
explicit Window(QWidget * parent = 0);
public slots:
void refreshDocumentJson();
void refreshSchemaJson();
void refreshJson();
void showOpenDocumentDialog();
void showOpenSchemaDialog();
@ -42,7 +41,8 @@ private:
QTextEdit * m_errors;
QJsonDocument m_document;
QJsonDocument m_documentJson;
QJsonDocument m_schemaJson;
valijson::Schema * m_schema;
};