summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexei Rousskikh <ext-alexei.rousskikh@nokia.com>2012-03-01 13:37:03 -0500
committerAndrew Christian <andrew.christian@nokia.com>2012-03-05 12:43:06 +0100
commit5c55284cceea22fcd4768f4b41fcfb7e0f893d0c (patch)
tree5de7023664357de4d690d198a303b95b5184db84
parentd55adf7a57be778fdf60363f682b77c2bc51f434 (diff)
improved error message; added a non-existent schema error
Change-Id: Icb56fb4d4b3034da10abda9047687aad90d44a23 Reviewed-by: Andrew Christian <andrew.christian@nokia.com>
-rw-r--r--src/qtjsonschema/jsonobjecttypes_impl_p.h10
-rw-r--r--src/qtjsonschema/jsonobjecttypes_p.h1
-rw-r--r--src/qtjsonschema/schemamanager_impl_p.h8
-rw-r--r--src/qtjsonschema/schemaobject_p.h3
-rw-r--r--tests/auto/jsonschema/tst_jsonschema.cpp3
5 files changed, 20 insertions, 5 deletions
diff --git a/src/qtjsonschema/jsonobjecttypes_impl_p.h b/src/qtjsonschema/jsonobjecttypes_impl_p.h
index 74b2afb..052dac8 100644
--- a/src/qtjsonschema/jsonobjecttypes_impl_p.h
+++ b/src/qtjsonschema/jsonobjecttypes_impl_p.h
@@ -307,6 +307,16 @@ inline QJsonValue JsonObjectTypes::Value::value() const
return QJsonValue();
}
+/*!
+ Returns value in human-readable format for error reporting
+*/
+inline QString JsonObjectTypes::Value::data() const
+{
+ QString str;
+ QDebug(&str) << value();
+ return str;
+}
+
inline const QJsonObject JsonObjectTypes::Value::map() const
{
Q_ASSERT(m_type == Map);
diff --git a/src/qtjsonschema/jsonobjecttypes_p.h b/src/qtjsonschema/jsonobjecttypes_p.h
index 2bef1a5..11f0fbe 100644
--- a/src/qtjsonschema/jsonobjecttypes_p.h
+++ b/src/qtjsonschema/jsonobjecttypes_p.h
@@ -129,6 +129,7 @@ public:
inline bool compare(const Value &) const;
inline QJsonValue value() const;
+ inline QString data() const; // human-readable format
private:
inline const QJsonObject map() const;
diff --git a/src/qtjsonschema/schemamanager_impl_p.h b/src/qtjsonschema/schemamanager_impl_p.h
index deb629e..951b31a 100644
--- a/src/qtjsonschema/schemamanager_impl_p.h
+++ b/src/qtjsonschema/schemamanager_impl_p.h
@@ -103,10 +103,12 @@ inline T SchemaManager<T,TT>::ensureCompiled(const QString &schemaName, MapSchem
template<class T, class TT>
inline T SchemaManager<T,TT>::validate(const QString &schemaName, T object)
{
- if (!contains(schemaName))
- return T();
-
TypesService callbacks(this);
+ if (!contains(schemaName)) {
+ callbacks.setValidationError(QString::fromLatin1("Schema '%1' not found.").arg(schemaName));
+ return callbacks.error();
+ }
+
MapSchemaPair schemaPair = m_schemas.value(schemaName);
if (ensureCompiled(schemaName, &schemaPair, &callbacks).isEmpty()) {
// schema compiled successfully can validate an object
diff --git a/src/qtjsonschema/schemaobject_p.h b/src/qtjsonschema/schemaobject_p.h
index b230e5a..548988a 100644
--- a/src/qtjsonschema/schemaobject_p.h
+++ b/src/qtjsonschema/schemaobject_p.h
@@ -185,11 +185,10 @@ class SchemaPrivate : public QSharedData
{
bool result = doCheck(value);
if (!result) {
- bool ok;
// TODO it is tricky, as we do not have access to source code of this schema.
// maybe we can set "additional, hidden " source property in each schema property, or some nice hash?
m_schema->m_callbacks->setValidationError(QLatin1String("Schema validation error: ") +
- QString::fromLatin1(m_errorMessage).arg(value.toString(&ok)));
+ QString::fromLatin1(m_errorMessage).arg(value.data()));
}
return result;
}
diff --git a/tests/auto/jsonschema/tst_jsonschema.cpp b/tests/auto/jsonschema/tst_jsonschema.cpp
index 383284b..379d928 100644
--- a/tests/auto/jsonschema/tst_jsonschema.cpp
+++ b/tests/auto/jsonschema/tst_jsonschema.cpp
@@ -122,6 +122,9 @@ void tst_JsonSchema::schemaTest()
item.insert("create-test0", 1);
item.insert("another-field", QLatin1String("uuid:{zxcvbnm}"));
+ result = validator.validateSchema("should_fail", item); // should fail - schema does not exist
+ QVERIFY(!result);
+
result = validator.validateSchema("SchemaTestObject", item);
//qDebug() << "VALID validation result: " << result;
QVERIFY(result);