diff options
author | Jacob Keeler <jacob.keeler@livioradio.com> | 2017-05-04 12:18:40 -0400 |
---|---|---|
committer | Jacob Keeler <jacob.keeler@livioradio.com> | 2017-05-04 12:18:40 -0400 |
commit | 884e897941a600c11b7fcfa3d8cf59ce15eb3571 (patch) | |
tree | 1d5b0d1abbdcdd7c2661a59b3c10b6110fbf53a3 /src/components/smart_objects/src | |
parent | dc9732721e1690159fb56e0b5f5e9d643c9a9d44 (diff) | |
download | sdl_core-884e897941a600c11b7fcfa3d8cf59ce15eb3571.tar.gz |
Invalid data responses now return useful error messages in `info` field
Diffstat (limited to 'src/components/smart_objects/src')
8 files changed, 112 insertions, 27 deletions
diff --git a/src/components/smart_objects/src/always_false_schema_item.cc b/src/components/smart_objects/src/always_false_schema_item.cc index 59e50e10c2..a08ad32b1b 100644 --- a/src/components/smart_objects/src/always_false_schema_item.cc +++ b/src/components/smart_objects/src/always_false_schema_item.cc @@ -41,7 +41,9 @@ utils::SharedPtr<CAlwaysFalseSchemaItem> CAlwaysFalseSchemaItem::create() { return new CAlwaysFalseSchemaItem(); } -Errors::eType CAlwaysFalseSchemaItem::validate(const SmartObject& object) { +Errors::eType CAlwaysFalseSchemaItem::validate(const SmartObject& object, + std::string& errorMessage) { + errorMessage.assign("Generic error"); return Errors::ERROR; } } // namespace NsSmartObjects diff --git a/src/components/smart_objects/src/always_true_schema_item.cc b/src/components/smart_objects/src/always_true_schema_item.cc index a474b0e157..0b1b8af0c5 100644 --- a/src/components/smart_objects/src/always_true_schema_item.cc +++ b/src/components/smart_objects/src/always_true_schema_item.cc @@ -39,7 +39,8 @@ utils::SharedPtr<CAlwaysTrueSchemaItem> CAlwaysTrueSchemaItem::create() { return new CAlwaysTrueSchemaItem(); } -Errors::eType CAlwaysTrueSchemaItem::validate(const SmartObject& object) { +Errors::eType CAlwaysTrueSchemaItem::validate(const SmartObject& object, + std::string& errorMessage) { return Errors::OK; } diff --git a/src/components/smart_objects/src/array_schema_item.cc b/src/components/smart_objects/src/array_schema_item.cc index 105cacb25c..ad341d9b49 100644 --- a/src/components/smart_objects/src/array_schema_item.cc +++ b/src/components/smart_objects/src/array_schema_item.cc @@ -41,23 +41,47 @@ utils::SharedPtr<CArraySchemaItem> CArraySchemaItem::create( return new CArraySchemaItem(ElementSchemaItem, MinSize, MaxSize); } -Errors::eType CArraySchemaItem::validate(const SmartObject& Object) { +Errors::eType CArraySchemaItem::validate(const SmartObject& Object, + std::string& errorMessage) { if (SmartType_Array != Object.getType()) { + if (!Object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + Object.getKey() + + "\". "); + } + errorMessage += "Incorrect type, expected: " + + SmartObject::typeToString(SmartType_Array) + ", got: " + + SmartObject::typeToString(Object.getType()); return Errors::INVALID_VALUE; } size_t sizeLimit; const size_t array_len = Object.length(); if (mMinSize.getValue(sizeLimit) && (array_len < sizeLimit)) { + if (!Object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + Object.getKey() + + "\". "); + } + std::stringstream stream; + stream << "Got array of size: " << array_len + << ", minimum allowed: " << sizeLimit; + errorMessage += stream.str(); return Errors::OUT_OF_RANGE; } if (mMaxSize.getValue(sizeLimit) && (array_len > sizeLimit)) { + if (!Object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + Object.getKey() + + "\". "); + } + std::stringstream stream; + stream << "Got array of size: " << array_len + << ", maximum allowed: " << sizeLimit; + errorMessage += stream.str(); return Errors::OUT_OF_RANGE; } for (size_t i = 0u; i < array_len; ++i) { const Errors::eType result = - mElementSchemaItem->validate(Object.getElement(i)); + mElementSchemaItem->validate(Object.getElement(i), errorMessage); if (Errors::OK != result) { return result; } diff --git a/src/components/smart_objects/src/object_schema_item.cc b/src/components/smart_objects/src/object_schema_item.cc index f0c17faaea..dae4ebbe26 100644 --- a/src/components/smart_objects/src/object_schema_item.cc +++ b/src/components/smart_objects/src/object_schema_item.cc @@ -56,8 +56,16 @@ utils::SharedPtr<CObjectSchemaItem> CObjectSchemaItem::create( return new CObjectSchemaItem(members); } -Errors::eType CObjectSchemaItem::validate(const SmartObject& object) { +Errors::eType CObjectSchemaItem::validate(const SmartObject& object, + std::string& errorMessage) { if (SmartType_Map != object.getType()) { + if (!object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + object.getKey() + + "\". "); + } + errorMessage += "Incorrect type, expected: " + + SmartObject::typeToString(SmartType_Map) + ", got: " + + SmartObject::typeToString(object.getType()); return Errors::INVALID_VALUE; } @@ -71,12 +79,18 @@ Errors::eType CObjectSchemaItem::validate(const SmartObject& object) { std::set<std::string>::const_iterator key_it = object_keys.find(key); if (object_keys.end() == key_it) { if (member.mIsMandatory) { + if (!object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + object.getKey() + + "\". "); + } + errorMessage += "Missing mandatory parameter: \"" + key + "\""; return Errors::MISSING_MANDATORY_PARAMETER; } continue; } const SmartObject& field = object.getElement(key); - const Errors::eType result = member.mSchemaItem->validate(field); + const Errors::eType result = + member.mSchemaItem->validate(field, errorMessage); if (Errors::OK != result) { return result; } diff --git a/src/components/smart_objects/src/schema_item.cc b/src/components/smart_objects/src/schema_item.cc index 8c0bc0edb3..5e3614ae06 100644 --- a/src/components/smart_objects/src/schema_item.cc +++ b/src/components/smart_objects/src/schema_item.cc @@ -35,7 +35,8 @@ namespace NsSmartDeviceLink { namespace NsSmartObjects { -Errors::eType ISchemaItem::validate(const SmartObject& object) { +Errors::eType ISchemaItem::validate(const SmartObject& object, + std::string& errorMessage) { return Errors::ERROR; } diff --git a/src/components/smart_objects/src/smart_object.cc b/src/components/smart_objects/src/smart_object.cc index 5c90de5671..dcd0ff9f91 100644 --- a/src/components/smart_objects/src/smart_object.cc +++ b/src/components/smart_objects/src/smart_object.cc @@ -50,17 +50,18 @@ namespace NsSmartObjects { **/ static const char* invalid_cstr_value = ""; -SmartObject::SmartObject() : m_type(SmartType_Null), m_schema() { +SmartObject::SmartObject() : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; } SmartObject::SmartObject(const SmartObject& Other) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; duplicate(Other); } -SmartObject::SmartObject(SmartType Type) : m_type(SmartType_Null), m_schema() { +SmartObject::SmartObject(SmartType Type) + : m_type(SmartType_Null), m_schema(), m_key(NULL) { switch (Type) { case SmartType_Null: break; @@ -104,6 +105,8 @@ SmartObject::SmartObject(SmartType Type) : m_type(SmartType_Null), m_schema() { SmartObject::~SmartObject() { cleanup_data(); + delete m_key; + m_key = NULL; } SmartObject& SmartObject::operator=(const SmartObject& Other) { @@ -113,7 +116,7 @@ SmartObject& SmartObject::operator=(const SmartObject& Other) { } bool SmartObject::operator==(const SmartObject& Other) const { - if (m_type != Other.m_type) + if (m_type != Other.m_type || m_key != Other.m_key) return false; switch (m_type) { @@ -168,7 +171,7 @@ bool SmartObject::operator==(const SmartObject& Other) const { } SmartObject::SmartObject(int32_t InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_integer(InitialValue); } @@ -225,7 +228,7 @@ int64_t SmartObject::convert_int() const { } SmartObject::SmartObject(uint32_t InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_integer(InitialValue); } @@ -254,7 +257,7 @@ bool SmartObject::operator==(const uint32_t Value) const { } SmartObject::SmartObject(int64_t InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_integer(InitialValue); } @@ -282,7 +285,7 @@ SmartObject& SmartObject::operator=(const uint64_t NewValue) { } SmartObject::SmartObject(double InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_double(InitialValue); } @@ -328,7 +331,7 @@ double SmartObject::convert_double() const { } SmartObject::SmartObject(bool InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_bool(InitialValue); } @@ -373,7 +376,7 @@ bool SmartObject::convert_bool() const { } SmartObject::SmartObject(char InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_char(InitialValue); } @@ -422,13 +425,13 @@ char SmartObject::convert_char() const { // ============================================================= SmartObject::SmartObject(const custom_str::CustomString& InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_string(InitialValue); } SmartObject::SmartObject(const std::string& InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_string(custom_str::CustomString(InitialValue)); } @@ -508,7 +511,7 @@ custom_str::CustomString SmartObject::convert_custom_string() const { // ============================================================= SmartObject::SmartObject(const char* const InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_cstr(InitialValue); return; @@ -538,7 +541,7 @@ void SmartObject::set_value_cstr(const char* NewValue) { // BINARY TYPE SUPPORT // ============================================================= SmartObject::SmartObject(const SmartBinary& InitialValue) - : m_type(SmartType_Null), m_schema() { + : m_type(SmartType_Null), m_schema(), m_key(NULL) { m_data.str_value = NULL; set_value_binary(InitialValue); } @@ -723,6 +726,10 @@ void SmartObject::duplicate(const SmartObject& OtherObject) { } m_schema = OtherObject.m_schema; + if (OtherObject.m_key) { + setKey(*OtherObject.m_key); + } + cleanup_data(); m_type = newType; @@ -838,6 +845,15 @@ SmartType SmartObject::getType() const { return m_type; } +void SmartObject::setKey(const std::string& NewKey) { + delete m_key; + m_key = new std::string(NewKey); +} + +std::string SmartObject::getKey() const { + return (m_key == NULL) ? "" : *m_key; +} + std::string SmartObject::OperatorToTransform(const SmartMap::value_type& pair) { return pair.first; } @@ -869,11 +885,13 @@ bool SmartObject::erase(const std::string& Key) { } bool SmartObject::isValid() const { - return (Errors::OK == m_schema.validate(*this)); + std::string errorMessage; + + return (Errors::OK == m_schema.validate(*this, errorMessage)); } -Errors::eType SmartObject::validate() { - return m_schema.validate(*this); +Errors::eType SmartObject::validate(std::string& errorMessage) { + return m_schema.validate(*this, errorMessage); } void SmartObject::setSchema(const CSmartSchema& schema) { diff --git a/src/components/smart_objects/src/smart_schema.cc b/src/components/smart_objects/src/smart_schema.cc index be24ceb076..6c18f70fd2 100644 --- a/src/components/smart_objects/src/smart_schema.cc +++ b/src/components/smart_objects/src/smart_schema.cc @@ -40,8 +40,9 @@ CSmartSchema::CSmartSchema() : mSchemaItem(CAlwaysTrueSchemaItem::create()) {} CSmartSchema::CSmartSchema(const ISchemaItemPtr SchemaItem) : mSchemaItem(SchemaItem) {} -Errors::eType CSmartSchema::validate(const SmartObject& object) const { - return mSchemaItem->validate(object); +Errors::eType CSmartSchema::validate(const SmartObject& object, + std::string& errorMessage) const { + return mSchemaItem->validate(object, errorMessage); } void CSmartSchema::setSchemaItem(const ISchemaItemPtr schemaItem) { diff --git a/src/components/smart_objects/src/string_schema_item.cc b/src/components/smart_objects/src/string_schema_item.cc index 3fac3a6cde..88373cc172 100644 --- a/src/components/smart_objects/src/string_schema_item.cc +++ b/src/components/smart_objects/src/string_schema_item.cc @@ -45,8 +45,16 @@ utils::SharedPtr<CStringSchemaItem> CStringSchemaItem::create( return new CStringSchemaItem(MinLength, MaxLength, DefaultValue); } -Errors::eType CStringSchemaItem::validate(const SmartObject& Object) { +Errors::eType CStringSchemaItem::validate(const SmartObject& Object, + std::string& errorMessage) { if (SmartType_String != Object.getType()) { + if (!Object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + Object.getKey() + + "\". "); + } + errorMessage += "Incorrect type, expected: " + + SmartObject::typeToString(SmartType_String) + ", got: " + + SmartObject::typeToString(Object.getType()); return Errors::INVALID_VALUE; } @@ -54,9 +62,25 @@ Errors::eType CStringSchemaItem::validate(const SmartObject& Object) { size_t length; if (mMinLength.getValue(length) && (value.size() < length)) { + if (!Object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + Object.getKey() + + "\". "); + } + std::stringstream stream; + stream << "Got string of size: " << value.size() + << ", minimum allowed: " << length; + errorMessage += stream.str(); return Errors::OUT_OF_RANGE; } if (mMaxLength.getValue(length) && (value.size() > length)) { + if (!Object.getKey().empty()) { + errorMessage.assign("Validation failed for \"" + Object.getKey() + + "\". "); + } + std::stringstream stream; + stream << "Got string of size: " << value.size() + << ", maximum allowed: " << length; + errorMessage += stream.str(); return Errors::OUT_OF_RANGE; } return Errors::OK; |