From 150735848dcc7519e20e030b9b3330f54ed2c83e Mon Sep 17 00:00:00 2001 From: "Jonathan S. Fonseca" <87537000+JonathanSF98@users.noreply.github.com> Date: Sat, 25 May 2024 10:30:01 -0300 Subject: [PATCH] Intention to present an alternative to improve the aforementioned method (#2034) --- drogon_ctl/templates/model_cc.csp | 3 +-- orm_lib/tests/mysql/Blog.cc | 4 +--- orm_lib/tests/mysql/Category.cc | 4 +--- orm_lib/tests/mysql/Tag.cc | 4 +--- orm_lib/tests/mysql/Users.cc | 28 +++++++--------------------- orm_lib/tests/mysql/Wallets.cc | 4 +--- orm_lib/tests/postgresql/Blog.cc | 4 +--- orm_lib/tests/postgresql/Category.cc | 4 +--- orm_lib/tests/postgresql/Tag.cc | 4 +--- orm_lib/tests/postgresql/Users.cc | 28 +++++++--------------------- orm_lib/tests/postgresql/Wallets.cc | 4 +--- 11 files changed, 23 insertions(+), 68 deletions(-) diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index d6510cc0..4f2fa504 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -1534,8 +1534,7 @@ if(!col.notNull_){%> if(col.colType_ == "std::string" && col.colLength_>0) { %> - // asString().length() creates a string object, is there any better way to validate the length? - if(pJson.isString() && pJson.asString().length() > {%col.colLength_%}) + if(pJson.isString() && std::strlen(pJson.asCString()) > {%col.colLength_%}) { err="String length exceeds limit for the " + fieldName + diff --git a/orm_lib/tests/mysql/Blog.cc b/orm_lib/tests/mysql/Blog.cc index c7f74d2d..f935cde5 100644 --- a/orm_lib/tests/mysql/Blog.cc +++ b/orm_lib/tests/mysql/Blog.cc @@ -673,9 +673,7 @@ bool Blog::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 30) + if (pJson.isString() && std::strlen(pJson.asCString()) > 30) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 30)"; diff --git a/orm_lib/tests/mysql/Category.cc b/orm_lib/tests/mysql/Category.cc index 37102a0f..a82c879b 100644 --- a/orm_lib/tests/mysql/Category.cc +++ b/orm_lib/tests/mysql/Category.cc @@ -510,9 +510,7 @@ bool Category::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 30) + if (pJson.isString() && std::strlen(pJson.asCString()) > 30) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 30)"; diff --git a/orm_lib/tests/mysql/Tag.cc b/orm_lib/tests/mysql/Tag.cc index 06709fc8..38bf6038 100644 --- a/orm_lib/tests/mysql/Tag.cc +++ b/orm_lib/tests/mysql/Tag.cc @@ -509,9 +509,7 @@ bool Tag::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 30) + if (pJson.isString() && std::strlen(pJson.asCString()) > 30) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 30)"; diff --git a/orm_lib/tests/mysql/Users.cc b/orm_lib/tests/mysql/Users.cc index 1281ba4d..10e8567f 100644 --- a/orm_lib/tests/mysql/Users.cc +++ b/orm_lib/tests/mysql/Users.cc @@ -1674,9 +1674,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 32) + if (pJson.isString() && std::strlen(pJson.asCString()) > 32) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 32)"; @@ -1694,9 +1692,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 64) + if (pJson.isString() && std::strlen(pJson.asCString()) > 64) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 64)"; @@ -1714,9 +1710,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 64) + if (pJson.isString() && std::strlen(pJson.asCString()) > 64) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 64)"; @@ -1734,9 +1728,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 20) + if (pJson.isString() && std::strlen(pJson.asCString()) > 20) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 20)"; @@ -1754,9 +1746,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 50) + if (pJson.isString() && std::strlen(pJson.asCString()) > 50) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 50)"; @@ -1774,9 +1764,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 32) + if (pJson.isString() && std::strlen(pJson.asCString()) > 32) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 32)"; @@ -1794,9 +1782,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 20) + if (pJson.isString() && std::strlen(pJson.asCString()) > 20) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 20)"; diff --git a/orm_lib/tests/mysql/Wallets.cc b/orm_lib/tests/mysql/Wallets.cc index fce22a7b..1225cdfd 100644 --- a/orm_lib/tests/mysql/Wallets.cc +++ b/orm_lib/tests/mysql/Wallets.cc @@ -677,9 +677,7 @@ bool Wallets::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 32) + if (pJson.isString() && std::strlen(pJson.asCString()) > 32) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 32)"; diff --git a/orm_lib/tests/postgresql/Blog.cc b/orm_lib/tests/postgresql/Blog.cc index c3260ae2..d7106766 100644 --- a/orm_lib/tests/postgresql/Blog.cc +++ b/orm_lib/tests/postgresql/Blog.cc @@ -672,9 +672,7 @@ bool Blog::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 30) + if (pJson.isString() && std::strlen(pJson.asCString()) > 30) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 30)"; diff --git a/orm_lib/tests/postgresql/Category.cc b/orm_lib/tests/postgresql/Category.cc index 5fe6d337..d15a76c4 100644 --- a/orm_lib/tests/postgresql/Category.cc +++ b/orm_lib/tests/postgresql/Category.cc @@ -509,9 +509,7 @@ bool Category::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 30) + if (pJson.isString() && std::strlen(pJson.asCString()) > 30) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 30)"; diff --git a/orm_lib/tests/postgresql/Tag.cc b/orm_lib/tests/postgresql/Tag.cc index 9c52dd7d..26689637 100644 --- a/orm_lib/tests/postgresql/Tag.cc +++ b/orm_lib/tests/postgresql/Tag.cc @@ -508,9 +508,7 @@ bool Tag::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 30) + if (pJson.isString() && std::strlen(pJson.asCString()) > 30) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 30)"; diff --git a/orm_lib/tests/postgresql/Users.cc b/orm_lib/tests/postgresql/Users.cc index f92e889e..0b9754d2 100644 --- a/orm_lib/tests/postgresql/Users.cc +++ b/orm_lib/tests/postgresql/Users.cc @@ -1656,9 +1656,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 32) + if (pJson.isString() && std::strlen(pJson.asCString()) > 32) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 32)"; @@ -1676,9 +1674,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 64) + if (pJson.isString() && std::strlen(pJson.asCString()) > 64) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 64)"; @@ -1696,9 +1692,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 64) + if (pJson.isString() && std::strlen(pJson.asCString()) > 64) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 64)"; @@ -1716,9 +1710,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 20) + if (pJson.isString() && std::strlen(pJson.asCString()) > 20) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 20)"; @@ -1736,9 +1728,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 50) + if (pJson.isString() && std::strlen(pJson.asCString()) > 50) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 50)"; @@ -1756,9 +1746,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 32) + if (pJson.isString() && std::strlen(pJson.asCString()) > 32) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 32)"; @@ -1793,9 +1781,7 @@ bool Users::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 20) + if (pJson.isString() && std::strlen(pJson.asCString()) > 20) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 20)"; diff --git a/orm_lib/tests/postgresql/Wallets.cc b/orm_lib/tests/postgresql/Wallets.cc index 7245fe14..647bd994 100644 --- a/orm_lib/tests/postgresql/Wallets.cc +++ b/orm_lib/tests/postgresql/Wallets.cc @@ -676,9 +676,7 @@ bool Wallets::validJsonOfField(size_t index, err = "Type error in the " + fieldName + " field"; return false; } - // asString().length() creates a string object, is there any better - // way to validate the length? - if (pJson.isString() && pJson.asString().length() > 32) + if (pJson.isString() && std::strlen(pJson.asCString()) > 32) { err = "String length exceeds limit for the " + fieldName + " field (the maximum value is 32)";