Skip to content

Commit

Permalink
Validator plugin: add new ValidatorCharNotAllowed
Browse files Browse the repository at this point in the history
Simple validator that checks if the input value contains a character
from a list of forbidden characters.
  • Loading branch information
buschmann23 committed Oct 28, 2019
1 parent 270bc10 commit 4eb2e71
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cutelyst/Plugins/Utils/Validator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ set(plugin_validator_SRC
validatorbetween_p.h
validatorboolean.cpp
validatorboolean_p.h
validatorcharnotallowed.cpp
validatorcharnotallowed_p.h
validatorconfirmed.cpp
validatorconfirmed_p.h
validatordate.cpp
Expand Down Expand Up @@ -103,6 +105,7 @@ set(plugin_validator_HEADERS
validatorbefore.h
validatorbetween.h
validatorboolean.h
validatorcharnotallowed.h
validatorconfirmed.h
validatordate.h
validatordatetime.h
Expand Down
1 change: 1 addition & 0 deletions Cutelyst/Plugins/Utils/Validator/Validators
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "validatorbefore.h"
#include "validatorbetween.h"
#include "validatorboolean.h"
#include "validatorcharnotallowed.h"
#include "validatorconfirmed.h"
#include "validatordate.h"
#include "validatordatetime.h"
Expand Down
103 changes: 103 additions & 0 deletions Cutelyst/Plugins/Utils/Validator/validatorcharnotallowed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "validatorcharnotallowed_p.h"

using namespace Cutelyst;

ValidatorCharNotAllowed::ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages, const QString &defValKey) :
ValidatorRule(*new ValidatorCharNotAllowedPrivate(field, forbiddenChars, messages, defValKey))
{

}

ValidatorCharNotAllowed::~ValidatorCharNotAllowed()
{

}

bool ValidatorCharNotAllowed::validate(const QString &value, const QString &forbiddenChars, QChar *foundChar)
{
bool valid = true;

for (const QChar &forbiddenChar : forbiddenChars) {
if (value.contains(forbiddenChar)) {
valid = false;
if (foundChar) {
*foundChar = forbiddenChar;
}
break;
}
}

return valid;
}

ValidatorReturnType ValidatorCharNotAllowed::validate(Context *c, const ParamsMultiMap &params) const
{
ValidatorReturnType result;

Q_D(const ValidatorCharNotAllowed);

const QString v = value(params);
if (!v.isEmpty()) {
if (Q_LIKELY(!d->forbiddenChars.isEmpty())) {
QChar foundChar;
if (Q_LIKELY(ValidatorCharNotAllowed::validate(v, d->forbiddenChars, &foundChar))) {
result.value.setValue<QString>(v);
} else {
result.errorMessage = validationError(c, foundChar);
}
} else {
qCWarning(C_VALIDATOR) << "ValidatorCharNotAllowed: Empty validation data for field" << field() << "at" << c->controllerName() << "::" << c->actionName();
result.errorMessage = validationDataError(c);
}
} else {
defaultValue(c, &result, "ValidatorCharNotAllowed");
}

return result;
}

QString ValidatorCharNotAllowed::genericValidationError(Context *c, const QVariant &errorData) const
{
QString error;
const QChar foundChar = errorData.toChar();
Q_D(const ValidatorCharNotAllowed);
const QString _label = label(c);
if (_label.isEmpty()) {
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "Must not contain the following characters: “%1”. But contains the following illegal character: “%2”.").arg(d->forbiddenChars, QString(foundChar));
} else {
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The text in the “%1“ field must not contain the following characters: “%2“. But contains the following illegal character: “%3”.").arg(_label, d->forbiddenChars, QString(foundChar));
}

return error;
}

QString ValidatorCharNotAllowed::genericValidationDataError(Context *c, const QVariant &errorData) const
{
QString error;
Q_UNUSED(errorData)
const QString _label = label(c);
if (_label.isEmpty()) {
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for this field is empty.");
} else {
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for the “%1“ field is empty.").arg(_label);
}
return error;
}
101 changes: 101 additions & 0 deletions Cutelyst/Plugins/Utils/Validator/validatorcharnotallowed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef CUTELYSTVALIDATORCHARNOTALLOWED_H
#define CUTELYSTVALIDATORCHARNOTALLOWED_H

#include <Cutelyst/cutelyst_global.h>
#include "validatorrule.h"

namespace Cutelyst {

class ValidatorCharNotAllowedPrivate;

/*!
* \ingroup plugins-utils-validator-rules
* \class ValidatorCharNotAllowed validatorcharnotallowed.h <Cutelyst/Plugins/Utils/validatorcharnotallowed.h>
* \brief Validates an input field for not allowed characters.
*
* The \a field under validation is not allowed to contain a list of characters.
* The list of not allowed characters is set as a QString to the \a forbiddenChars.
*
* \note Unless \link Validator::validate() validation\endlink is started with \link Validator::NoTrimming NoTrimming\endlink,
* whitespaces will be removed from the beginning and the end of the input value before validation.
* If the \a field's value is empty or if the \a field is missing in the input data, the validation will succeed without
* performing the validation itself. Use one of the \link ValidatorRequired required validators \endlink to require the
* field to be present and not empty.
*
* \sa Validator for general usage of validators.
*
* \sa ValidatorRegularExpression
*/
class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorCharNotAllowed : public ValidatorRule
{
public:
/*!
* \brief Constructs a new char not allowed validator.
*
* \param field Name of the input field to validate.
* \param forbiddenChars List of characters not allowed in the input field.
* \param messages Custom error messages if validation fails.
* \param defValKey \link Context::stash() Stash \endlink key containing a default value if input field is empty. This value will \b NOT be validated.
*/
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages = ValidatorMessages(), const QString &defValKey = QString());

/*!
* \brief Deconstructs the char not allowed validator.
*/
~ValidatorCharNotAllowed() override;

/*!
* \ingroup plugins-utils-validator-rules
* \brief Returns \c true if \a value does not contain any of the charachters in \a forbiddenChars
* \param value The value to validate.
* \param forbiddenChars The list of forbidden characters.
* \param foundChar If set, it will contain the first found chararacter that is forbidden.
* \return \c true if the \a value does not contain any of the \a forbiddenChars
*/
static bool validate(const QString &value, const QString &forbiddenChars, QChar *foundChar = nullptr);

protected:
/*!
* \brief Performs the validation and returns the result.
*
* If validation succeeded, ValidatorReturnType::value will contain the input paramter
* value as QString.
*/
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override;

/*!
* \brief Returns a generic error message if validation failed.
*/
QString genericValidationError(Context *c, const QVariant &errorData = QVariant()) const override;

/*!
* \brief Returns a generic error if the list of forbidden characters is empty.
*/
QString genericValidationDataError(Context *c, const QVariant &errorData = QVariant()) const override;

private:
Q_DECLARE_PRIVATE(ValidatorCharNotAllowed)
Q_DISABLE_COPY(ValidatorCharNotAllowed)
};

}

#endif // CUTELYSTVALIDATORCHARNOTALLOWED_H
40 changes: 40 additions & 0 deletions Cutelyst/Plugins/Utils/Validator/validatorcharnotallowed_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef CUTELYSTVALIDATORCHARNOTALLOWED_P_H
#define CUTELYSTVALIDATORCHARNOTALLOWED_P_H

#include "validatorcharnotallowed.h"
#include "validatorrule_p.h"

namespace Cutelyst {

class ValidatorCharNotAllowedPrivate : public ValidatorRulePrivate
{
public:
ValidatorCharNotAllowedPrivate(const QString &f, const QString &fcs, const ValidatorMessages &m, const QString &dvk) :
ValidatorRulePrivate(f, m, dvk),
forbiddenChars(fcs)
{}

QString forbiddenChars;
};

}

#endif // CUTELYSTVALIDATORCHARNOTALLOWED_P_H
2 changes: 2 additions & 0 deletions Cutelyst/Plugins/Utils/Validator/validatorregularexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class ValidatorRegularExpressionPrivate;
* field to be present and not empty.
*
* \sa Validator for general usage of validators.
*
* \sa ValidatorCharNotAllowed
*/
class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRegularExpression : public ValidatorRule
{
Expand Down
19 changes: 19 additions & 0 deletions i18n/plugin_utils_validator.de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@
<translation>Der Wert im Feld „%1“ kann nicht als boolescher Wert interpretiert werden.</translation>
</message>
</context>
<context>
<name>Cutelyst::ValidatorCharNotAllowed</name>
<message>
<source>Must not contain the following characters: “%1”. But contains the following illegal character: “%2”.</source>
<translation>Darf die folgenden Zeichen nicht enthalten: „%1“. Enthält aber das folgende nicht erlaubte Zeichen: „%2“.</translation>
</message>
<message>
<source>The text in the “%1“ field must not contain the following characters: “%2“. But contains the following illegal character: “%3”.</source>
<translation>Das Feld „%1“ darf die folgenden Zeichen nicht enthalten: „%2“. Es enthält aber das folgende nicht erlaubte Zeichen: „%3“.</translation>
</message>
<message>
<source>The list of illegal characters for this field is empty.</source>
<translation>Die Liste nicht erlaubter Zeichen für dieses Feld ist leer.</translation>
</message>
<message>
<source>The list of illegal characters for the “%1“ field is empty.</source>
<translation>Die Liste nicht erlaubter Zeichen für das Feld „%1“ ist leer.</translation>
</message>
</context>
<context>
<name>Cutelyst::ValidatorConfirmed</name>
<message>
Expand Down
19 changes: 19 additions & 0 deletions i18n/plugin_utils_validator.en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Cutelyst::ValidatorCharNotAllowed</name>
<message>
<source>Must not contain the following characters: “%1”. But contains the following illegal character: “%2”.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>The text in the “%1“ field must not contain the following characters: “%2“. But contains the following illegal character: “%3”.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>The list of illegal characters for this field is empty.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>The list of illegal characters for the “%1“ field is empty.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Cutelyst::ValidatorConfirmed</name>
<message>
Expand Down
15 changes: 15 additions & 0 deletions tests/testvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ class ValidatorTest : public Controller
checkResponse(c, v.validate(c));
}

// ***** Endpoint for ValidatorCharNotAllowed ******
C_ATTR(charNotAllowed, :Local :AutoArgs)
void charNotAllowed(Context *c) {
Validator v({new ValidatorCharNotAllowed(QStringLiteral("char_not_allowed_field"), QStringLiteral("#%*."), m_validatorMessages)});
checkResponse(c, v.validate(c));
}

// ***** Endpoint for ValidatorConfirmed ******
C_ATTR(confirmed, :Local :AutoArgs)
void confirmed(Context *c) {
Expand Down Expand Up @@ -1194,6 +1201,14 @@ void TestValidator::testController_data()
QTest::newRow("boolean-empty") << QStringLiteral("/boolean?boolean_field=") << headers << QByteArray() << valid;


// **** Start testing ValidatorCharNotAllowed *****

QTest::newRow("charnotallowed-empty") << QStringLiteral("/charNotAllowed?char_not_allowed_field=") << headers << QByteArray() << valid;

QTest::newRow("charnotallowed-valid") << QStringLiteral("/charNotAllowed?char_not_allowed_field=holladiewaldfee") << headers << QByteArray() << valid;

QTest::newRow("charnotallowed-invalid") << QStringLiteral("/charNotAllowed?char_not_allowed_field=holla.die.waldfee") << headers << QByteArray() << invalid;



// **** Start testing ValidatorConfirmed *****
Expand Down

0 comments on commit 4eb2e71

Please sign in to comment.