mirror of
https://github.com/tristanpenman/valijson.git
synced 2024-12-12 10:13:51 +01:00
Improve error messaging when parsing schemas and documents
This commit is contained in:
parent
dd32f66df8
commit
d34f78b435
@ -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()) {
|
||||
m_errors->setText("");
|
||||
return;
|
||||
}
|
||||
QString errors;
|
||||
m_errors->setText("");
|
||||
|
||||
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("");
|
||||
return;
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
QJsonParseError error;
|
||||
auto schemaDoc = QJsonDocument::fromJson(m_schemaEditor->toPlainText().toUtf8(), &error);
|
||||
if (schemaDoc.isNull()) {
|
||||
m_errors->setText(QString("Schema error: ") + error.errorString());
|
||||
return;
|
||||
if (doc.isEmpty()) {
|
||||
if (!schema.isEmpty()) {
|
||||
errors += "Document error: must not be empty\n\n";
|
||||
}
|
||||
} else {
|
||||
QJsonParseError error;
|
||||
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.");
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user