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); setCentralWidget(verticalSplitter);
setStatusBar(statusBar); setStatusBar(statusBar);
connect(m_documentEditor, SIGNAL(textChanged()), this, SLOT(refreshDocumentJson())); connect(m_documentEditor, SIGNAL(textChanged()), this, SLOT(refreshJson()));
connect(m_schemaEditor, SIGNAL(textChanged()), this, SLOT(refreshSchemaJson())); connect(m_schemaEditor, SIGNAL(textChanged()), this, SLOT(refreshJson()));
refreshJson();
} }
QTextEdit * Window::createEditor(bool readOnly) QTextEdit * Window::createEditor(bool readOnly)
@ -114,50 +116,55 @@ QToolBar * Window::createToolBar()
return toolbar; return toolbar;
} }
void Window::refreshDocumentJson() void Window::refreshJson()
{ {
const auto doc = m_documentEditor->toPlainText().toUtf8(); QString errors;
if (doc.isEmpty()) {
m_errors->setText(""); 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 schema = m_schemaEditor->toPlainText().toUtf8();
const auto doc = m_documentEditor->toPlainText().toUtf8();
if (schema.isEmpty()) { 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; 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; QJsonParseError error;
auto schemaDoc = QJsonDocument::fromJson(m_schemaEditor->toPlainText().toUtf8(), &error); m_documentJson = QJsonDocument::fromJson(doc, &error);
if (schemaDoc.isNull()) { if (m_documentJson.isNull()) {
m_errors->setText(QString("Schema error: ") + error.errorString()); errors += QString("Document error: ") + error.errorString() + "\n\n";
}
}
if (!errors.isEmpty()) {
m_errors->setText(errors);
return; return;
} }
try { try {
valijson::adapters::QtJsonAdapter adapter(schemaDoc.object()); valijson::adapters::QtJsonAdapter adapter(m_schemaJson.object());
valijson::SchemaParser parser; valijson::SchemaParser parser;
delete m_schema; delete m_schema;
m_schema = new valijson::Schema(); m_schema = new valijson::Schema();
parser.populateSchema(adapter, *m_schema); parser.populateSchema(adapter, *m_schema);
m_errors->setText("");
validate(); validate();
} catch (std::runtime_error & error) { } catch (std::runtime_error & error) {
delete m_schema; delete m_schema;
@ -190,7 +197,7 @@ void Window::validate()
{ {
valijson::ValidationResults results; valijson::ValidationResults results;
valijson::Validator validator; valijson::Validator validator;
valijson::adapters::QtJsonAdapter adapter(m_document.object()); valijson::adapters::QtJsonAdapter adapter(m_documentJson.object());
if (validator.validate(*m_schema, adapter, &results)) { if (validator.validate(*m_schema, adapter, &results)) {
m_errors->setText("Document is valid."); m_errors->setText("Document is valid.");

View File

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