Cutelyst  2.3.0
validator.cpp
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validator_p.h"
20 #include <Cutelyst/context.h>
21 #include <Cutelyst/request.h>
22 #include <Cutelyst/application.h>
23 #include <QLoggingCategory>
24 
25 using namespace Cutelyst;
26 
27 Q_LOGGING_CATEGORY(C_VALIDATOR, "cutelyst.utils.validator")
28 
29 Validator::Validator(QLatin1String translationContext) :
30  d_ptr(new ValidatorPrivate(translationContext))
31 {
32 }
33 
34 #ifdef Q_COMPILER_INITIALIZER_LISTS
35 Validator::Validator(std::initializer_list<ValidatorRule *> validators, QLatin1String translationContext) :
36  d_ptr(new ValidatorPrivate(validators, translationContext))
37 {
38 }
39 #endif
40 
42 {
43 }
44 
46 {
47  Q_D(Validator);
48  d->params.clear();
49  if (!d->validators.empty()) {
50  qDeleteAll(d->validators.begin(), d->validators.end());
51  d->validators.clear();
52  }
53 }
54 
55 Cutelyst::ValidatorResult Validator::validate(Context *c, ValidatorFlags flags) const
56 {
57  ValidatorResult result;
58 
59  Q_ASSERT(c);
60 
61  ParamsMultiMap params;
62  if (flags.testFlag(BodyParamsOnly)) {
63  params = c->req()->bodyParameters();
64  } else if (flags.testFlag(QueryParamsOnly)) {
65  params = c->req()->queryParameters();
66  } else {
67  params = c->req()->queryParameters();
68  params.unite(c->req()->bodyParameters());
69  }
70 
71  result = validate(c, params, flags);
72 
73  return result;
74 }
75 
76 ValidatorResult Validator::validate(Context *c, const ParamsMultiMap &params, ValidatorFlags flags) const
77 {
78  ValidatorResult result;
79 
80  Q_ASSERT(c);
81  Q_D(const Validator);
82 
83  if (d->validators.empty()) {
84  qCWarning(C_VALIDATOR) << "Validation started with empty validator list.";
85  return result;
86  }
87 
88  if (params.empty()) {
89  qCWarning(C_VALIDATOR) << "Validation started with empty parameters.";
90  }
91 
92  const bool stopOnFirstError = flags.testFlag(StopOnFirstError);
93  const bool noTrimming = flags.testFlag(NoTrimming);
94 
95  for (ValidatorRule *v : d->validators) {
96 
97  if (noTrimming) {
98  v->setTrimBefore(false);
99  }
100 
101  const ValidatorReturnType singleResult = v->validate(c, params);
102 
103  if (singleResult.extra.isValid()) {
104  result.addExtra(v->field(), singleResult.extra);
105  }
106 
107  if (singleResult) {
108  result.addValue(v->field(), singleResult.value);
109  } else {
110  result.addError(v->field(), singleResult.errorMessage);
111  if (stopOnFirstError) {
112  break;
113  }
114  }
115  }
116 
117  if (!result && flags.testFlag(FillStashOnError)) {
118  c->setStash(QStringLiteral("validationErrorStrings"), result.errorStrings());
119  c->setStash(QStringLiteral("validationErrors"), QVariant::fromValue(result.errors()));
120 
121  if (!params.isEmpty()) {
122  QMap<QString,QString>::const_iterator i = params.constBegin();
123  while (i != params.constEnd()) {
124  if (!i.key().contains(QStringLiteral("password"), Qt::CaseInsensitive)) {
125  c->setStash(i.key(), i.value());
126  }
127  ++i;
128  }
129  }
130  }
131 
132  return result;
133 }
134 
136 {
137  Q_D(Validator);
138  v->setTranslationContext(d->translationContext);
139  d->validators.push_back(v);
140 }
141 
143 {
144  app->loadTranslations(QStringLiteral("plugin_utils_validator"));
145 }
QMap< QString, QString > ParamsMultiMap
ValidatorResult validate(Context *c, ValidatorFlags flags=NoSpecialBehavior) const
Starts the validation process on Context c and returns a ValidatorResult.
Definition: validator.cpp:55
Validator(QLatin1String translationContext=QLatin1String())
Constructs a new Validator.
Definition: validator.cpp:29
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:207
void loadTranslations(const QString &filename, const QString &directory=QString(), const QString &prefix=QString(), const QString &suffix=QString())
ParamsMultiMap bodyParameters() const
Definition: request.cpp:209
The Cutelyst Context.
Definition: context.h:50
QHash< QString, QStringList > errors() const
Returns a dictionary containing fields with errors.
QStringList errorStrings() const
Returns a list of all error messages.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
Validation processor for input data.
Definition: validator.h:239
void addError(const QString &field, const QString &message)
Adds new error information to the internal QHash.
void addValue(const QString &field, const QVariant &value)
Adds a new value extracted from the specified input field.
virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const =0
Starts the validation and returns the result.
ParamsMultiMap queryParameters() const
Definition: request.cpp:245
void clear()
Clears all internal data.
Definition: validator.cpp:45
void addValidator(ValidatorRule *v)
Adds a new validator to the list of validators.
Definition: validator.cpp:135
static void loadTranslations(Application *app)
Loads the translations for the plugin.
Definition: validator.cpp:142
QString field() const
Returns the name of the field to validate.
The Cutelyst Application.
Definition: application.h:55
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
Provides information about performed validations.
void addExtra(const QString &field, const QVariant &extra)
Adds new extra data that came up when validating the input field.
~Validator()
Desconstructs the Validator and all added ValidatorRule objects.
Definition: validator.cpp:41