More improvements to form validation.

This commit is contained in:
Relintai 2021-08-20 19:18:26 +02:00
parent 3bc7bd98c6
commit f8ba0b26c5
5 changed files with 115 additions and 83 deletions

View File

@ -5,7 +5,7 @@
//FormFieldEntry
bool FormFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
return true;
}
@ -17,10 +17,10 @@ FormFieldEntry::~FormFieldEntry() {
//FormExistsFieldEntry
bool FormExistsFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormExistsFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
if (data == "") {
if (errors) {
errors->push_back(not_exists_error);
errors->push_back(field->human_name + not_exists_error);
}
return false;
@ -30,14 +30,14 @@ bool FormExistsFieldEntry::validate(Request *request, const std::string &field_n
}
FormExistsFieldEntry::FormExistsFieldEntry() {
not_exists_error = "Form field need to exists!";
not_exists_error = " field need to exists!";
}
FormExistsFieldEntry::~FormExistsFieldEntry() {
}
//FormIntFieldEntry
bool FormIntFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormIntFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
//https://stackoverflow.com/questions/2844817/how-do-i-check-if-a-c-string-is-an-int
if (data.empty()) {
@ -46,7 +46,7 @@ bool FormIntFieldEntry::validate(Request *request, const std::string &field_name
if (((!isdigit(data[0])) && (data[0] != '-') && (data[0] != '+'))) {
if (errors) {
errors->push_back(not_int_error);
errors->push_back(field->human_name + not_int_error);
}
return false;
@ -59,7 +59,7 @@ bool FormIntFieldEntry::validate(Request *request, const std::string &field_name
if (!is_int) {
if (errors) {
errors->push_back(not_int_error);
errors->push_back(field->human_name + not_int_error);
}
}
@ -67,7 +67,7 @@ bool FormIntFieldEntry::validate(Request *request, const std::string &field_name
}
FormIntFieldEntry::FormIntFieldEntry() {
not_int_error = "Field needs to be an integer.";
not_int_error = " needs to be an integer.";
}
FormIntFieldEntry::~FormIntFieldEntry() {
@ -75,7 +75,7 @@ FormIntFieldEntry::~FormIntFieldEntry() {
//FormFloatFieldEntry
bool FormFloatFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormFloatFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
if (data.empty()) {
return true;
}
@ -87,7 +87,7 @@ bool FormFloatFieldEntry::validate(Request *request, const std::string &field_na
if (!is_float) {
if (errors) {
errors->push_back(not_float_error);
errors->push_back(field->human_name + not_float_error);
}
}
@ -95,18 +95,18 @@ bool FormFloatFieldEntry::validate(Request *request, const std::string &field_na
}
FormFloatFieldEntry::FormFloatFieldEntry() {
not_float_error = "Field needs to be an floating point number.";
not_float_error = " needs to be an floating point number.";
}
FormFloatFieldEntry::~FormFloatFieldEntry() {
}
//FormAlphaFieldEntry
bool FormAlphaFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormAlphaFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (!isalpha(data[i])) {
if (errors) {
errors->push_back(not_alpha_error);
errors->push_back(field->human_name + not_alpha_error);
}
return false;
@ -117,18 +117,18 @@ bool FormAlphaFieldEntry::validate(Request *request, const std::string &field_na
}
FormAlphaFieldEntry::FormAlphaFieldEntry() {
not_alpha_error = "Field needs to only contain caharcters.";
not_alpha_error = " needs to only contain caharcters.";
}
FormAlphaFieldEntry::~FormAlphaFieldEntry() {
}
//FormAlphaNumericFieldEntry
bool FormAlphaNumericFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormAlphaNumericFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (!isalnum(data[i])) {
if (errors) {
errors->push_back(not_alpha_numeric_error);
errors->push_back(field->human_name + not_alpha_numeric_error);
}
return false;
@ -139,36 +139,37 @@ bool FormAlphaNumericFieldEntry::validate(Request *request, const std::string &f
}
FormAlphaNumericFieldEntry::FormAlphaNumericFieldEntry() {
not_alpha_numeric_error = "Field needs to only contain caharcters of numbers.";
not_alpha_numeric_error = " needs to only contain caharcters of numbers.";
}
FormAlphaNumericFieldEntry::~FormAlphaNumericFieldEntry() {
}
//FormNeedsLowercaseCharacterFieldEntry
bool FormNeedsLowercaseCharacterFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedsLowercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (islower(data[i])) {
if (errors) {
errors->push_back(does_not_have_lowercase_error);
}
return true;
}
}
if (errors) {
errors->push_back(field->human_name + does_not_have_lowercase_error);
}
return false;
}
FormNeedsLowercaseCharacterFieldEntry::FormNeedsLowercaseCharacterFieldEntry() {
does_not_have_lowercase_error = "Field needs at least one lowercase character!";
does_not_have_lowercase_error = " needs at least one lowercase character!";
}
FormNeedsLowercaseCharacterFieldEntry::~FormNeedsLowercaseCharacterFieldEntry() {
}
//FormNeedsUppercaseCharacterFieldEntry
bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (isupper(data[i])) {
return true;
@ -176,21 +177,21 @@ bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const std
}
if (errors) {
errors->push_back(does_not_have_uppercase_error);
errors->push_back(field->human_name + does_not_have_uppercase_error);
}
return false;
}
FormNeedsUppercaseCharacterFieldEntry::FormNeedsUppercaseCharacterFieldEntry() {
does_not_have_uppercase_error = "Field needs at least one uppercase character!";
does_not_have_uppercase_error = " needs at least one uppercase character!";
}
FormNeedsUppercaseCharacterFieldEntry::~FormNeedsUppercaseCharacterFieldEntry() {
}
//FormNeedsOtherCharacterFieldEntry
bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (!isalnum(data[i])) {
return true;
@ -198,26 +199,26 @@ bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const std::st
}
if (errors) {
errors->push_back(does_not_have_other_error);
errors->push_back(field->human_name + does_not_have_other_error);
}
return false;
}
FormNeedsOtherCharacterFieldEntry::FormNeedsOtherCharacterFieldEntry() {
does_not_have_other_error = "Field needs at least one other character!";
does_not_have_other_error = " needs at least one other character!";
}
FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() {
}
//FormMinimumLengthFieldEntry
bool FormMinimumLengthFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormMinimumLengthFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
if (data.size() <= min_length) {
if (errors) {
std::stringstream ss;
ss << does_not_have_min_length_errorf << min_length << does_not_have_min_length_errors;
ss << field->human_name << does_not_have_min_length_errorf << min_length << does_not_have_min_length_errors;
errors->push_back(ss.str());
}
@ -229,7 +230,7 @@ bool FormMinimumLengthFieldEntry::validate(Request *request, const std::string &
}
FormMinimumLengthFieldEntry::FormMinimumLengthFieldEntry() {
does_not_have_min_length_errorf = "Field needs at least ";
does_not_have_min_length_errorf = " needs at least ";
does_not_have_min_length_errors = " characters!";
min_length = 5;
@ -239,12 +240,12 @@ FormMinimumLengthFieldEntry::~FormMinimumLengthFieldEntry() {
//FormMaximumLengthFieldEntry
bool FormMaximumLengthFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormMaximumLengthFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
if (data.size() >= max_length) {
if (errors) {
std::stringstream ss;
ss << does_not_have_max_length_errorf << max_length << does_not_have_max_length_errors;
ss << field->human_name << does_not_have_max_length_errorf << max_length << does_not_have_max_length_errors;
errors->push_back(ss.str());
}
@ -256,7 +257,7 @@ bool FormMaximumLengthFieldEntry::validate(Request *request, const std::string &
}
FormMaximumLengthFieldEntry::FormMaximumLengthFieldEntry() {
does_not_have_max_length_errorf = "Field needs at most ";
does_not_have_max_length_errorf = " needs at most ";
does_not_have_max_length_errors = " characters!";
max_length = 10;
@ -266,10 +267,10 @@ FormMaximumLengthFieldEntry::~FormMaximumLengthFieldEntry() {
//FormEmailFieldEntry
bool FormEmailFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormEmailFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
if (data.size() == 0) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -277,7 +278,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
if (!isalpha(data[0])) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -290,7 +291,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
if (data[i] == '.') {
if (dot_pos != -1) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -304,7 +305,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
if (dot_pos == -1) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -314,7 +315,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
if (data[i] == '@') {
if (at_pos != -1) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -328,7 +329,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
if (at_pos == -1) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -341,7 +342,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
if (!isalnum(data[i])) {
if (errors) {
errors->push_back(email_format_error);
errors->push_back(field->human_name + email_format_error);
}
return false;
@ -352,17 +353,17 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
}
FormEmailFieldEntry::FormEmailFieldEntry() {
email_format_error = "Field is invalid!";
email_format_error = " is invalid!";
}
FormEmailFieldEntry::~FormEmailFieldEntry() {
}
//FormNeedToMatchOtherFieldEntry
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
if (data != request->get_parameter(other_field)) {
if (errors) {
errors->push_back(does_not_match_error + field_name + ".");
errors->push_back(field->human_name + does_not_match_error + field->name + ".");
}
return false;
@ -372,7 +373,7 @@ bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const std::strin
}
FormNeedToMatchOtherFieldEntry::FormNeedToMatchOtherFieldEntry() {
does_not_match_error = "Field does not match ";
does_not_match_error = " does not match ";
}
FormNeedToMatchOtherFieldEntry::~FormNeedToMatchOtherFieldEntry() {
}
@ -428,7 +429,7 @@ FormField *FormField::need_minimum_length(const int min_length) {
}
FormField *FormField::need_maximum_length(const int max_length) {
FormMaximumLengthFieldEntry *f = new FormMaximumLengthFieldEntry();
f->max_length =max_length;
f->max_length = max_length;
add_entry(f);
return this;
@ -457,7 +458,7 @@ bool FormField::validate(Request *request, std::vector<std::string> *errors) {
bool valid = true;
for (int i = 0; i < fields.size(); ++i) {
if (!fields[i]->validate(request, name, param, errors)) {
if (!fields[i]->validate(request, this, param, errors)) {
valid = false;
}
}
@ -494,9 +495,10 @@ void FormValidator::add_field(FormField *field) {
fields.push_back(field);
}
FormField *FormValidator::new_field(const std::string &name) {
FormField *FormValidator::new_field(const std::string &name, const std::string &human_name) {
FormField *f = new FormField();
f->name = name;
f->human_name = human_name;
fields.push_back(f);

View File

@ -7,10 +7,11 @@
#include <vector>
class Request;
class FormField;
class FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormFieldEntry();
virtual ~FormFieldEntry();
@ -18,7 +19,7 @@ public:
class FormExistsFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormExistsFieldEntry();
~FormExistsFieldEntry();
@ -28,7 +29,7 @@ public:
class FormIntFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormIntFieldEntry();
~FormIntFieldEntry();
@ -38,7 +39,7 @@ public:
class FormFloatFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormFloatFieldEntry();
~FormFloatFieldEntry();
@ -48,7 +49,7 @@ public:
class FormAlphaFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormAlphaFieldEntry();
~FormAlphaFieldEntry();
@ -58,7 +59,7 @@ public:
class FormAlphaNumericFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormAlphaNumericFieldEntry();
~FormAlphaNumericFieldEntry();
@ -68,7 +69,7 @@ public:
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormNeedsLowercaseCharacterFieldEntry();
~FormNeedsLowercaseCharacterFieldEntry();
@ -78,7 +79,7 @@ public:
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormNeedsUppercaseCharacterFieldEntry();
~FormNeedsUppercaseCharacterFieldEntry();
@ -88,7 +89,7 @@ public:
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormNeedsOtherCharacterFieldEntry();
~FormNeedsOtherCharacterFieldEntry();
@ -98,7 +99,7 @@ public:
class FormMinimumLengthFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormMinimumLengthFieldEntry();
~FormMinimumLengthFieldEntry();
@ -111,7 +112,7 @@ public:
class FormMaximumLengthFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormMaximumLengthFieldEntry();
~FormMaximumLengthFieldEntry();
@ -124,7 +125,7 @@ public:
class FormEmailFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormEmailFieldEntry();
~FormEmailFieldEntry();
@ -134,7 +135,7 @@ public:
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
FormNeedToMatchOtherFieldEntry();
~FormNeedToMatchOtherFieldEntry();
@ -149,6 +150,7 @@ public:
class FormField {
public:
std::string name;
std::string human_name;
FormField *need_to_exist();
FormField *need_to_be_int();
@ -180,7 +182,7 @@ public:
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
void add_field(FormField *field);
FormField *new_field(const std::string &name);
FormField *new_field(const std::string &name, const std::string &human_name);
FormValidator();
virtual ~FormValidator();

View File

@ -25,6 +25,8 @@ DBBasedUserManager::DBBasedUserManager() :
UserManager() {
printf("Using DBBasedUserManager.\n");
User::create_validators();
}
DBBasedUserManager::~DBBasedUserManager() {

View File

@ -137,6 +137,16 @@ void User::handle_login_request_default(Request *request) {
std::string pass_val = "";
if (request->get_method() == HTTP_METHOD_POST) {
//this is probbaly not needed
//it's ok for now as I need to test the validators more
std::vector<std::string> errors;
_login_validator->validate(request, &errors);
for (int i = 0; i < errors.size(); ++i) {
error_str += errors[i] + "<br>";
}
//not needed end
uname_val = request->get_parameter("username");
pass_val = request->get_parameter("password");
@ -214,6 +224,15 @@ void User::handle_register_request_default(Request *request) {
std::string pass_check_val = "";
if (request->get_method() == HTTP_METHOD_POST) {
std::vector<std::string> errors;
_registration_validator->validate(request, &errors);
for (int i = 0; i < errors.size(); ++i) {
error_str += errors[i] + "<br>";
}
uname_val = request->get_parameter("username");
email_val = request->get_parameter("email");
pass_val = request->get_parameter("password");
@ -389,29 +408,33 @@ void User::handle_delete_request(Request *request) {
}
void User::create_validators() {
if (!_login_validator) {
//Login
_login_validator = new FormValidator();
_login_validator->new_field("username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
FormField *pw = _login_validator->new_field("password");
_login_validator->new_field("username", "Username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
FormField *pw = _login_validator->new_field("password", "Password");
pw->need_to_exist();
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
pw->need_minimum_length(5);
}
if (!_registration_validator) {
//Registration
_registration_validator = new FormValidator();
_registration_validator->new_field("username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
_registration_validator->new_field("email")->need_to_exist()->need_to_be_email();
_registration_validator->new_field("username", "Username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
_registration_validator->new_field("email", "Email")->need_to_exist()->need_to_be_email();
pw = _registration_validator->new_field("password");
FormField *pw = _registration_validator->new_field("password", "Password");
pw->need_to_exist();
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
pw->need_minimum_length(5);
_registration_validator->new_field("password_check")->need_to_match("password");
_registration_validator->new_field("password_check", "Password check")->need_to_match("password");
_registration_validator->new_field("email")->need_to_exist()->need_to_be_email();
_registration_validator->new_field("email", "Email")->need_to_exist()->need_to_be_email();
}
}
User::User() :
@ -431,3 +454,6 @@ User::User() :
User::~User() {
unregister_sessions();
}
FormValidator *User::_login_validator = nullptr;
FormValidator *User::_registration_validator = nullptr;

View File

@ -48,7 +48,7 @@ public:
virtual void handle_logout_request(Request *request);
virtual void handle_delete_request(Request *request);
virtual void create_validators();
static void create_validators();
void register_sessions();
void unregister_sessions();
@ -56,8 +56,8 @@ public:
User();
~User();
FormValidator *_login_validator;
FormValidator *_registration_validator;
static FormValidator *_login_validator;
static FormValidator *_registration_validator;
};
#endif