Cutelyst  1.11.0
validator.cpp
1 /*
2  * Copyright (C) 2017 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 <QtCore/QLoggingCategory>
23 
24 using namespace Cutelyst;
25 
26 Q_LOGGING_CATEGORY(C_VALIDATOR, "cutelyst.utils.validator")
27 
29  d_ptr(new ValidatorPrivate)
30 {
31 }
32 
33 #ifdef Q_COMPILER_INITIALIZER_LISTS
34 Validator::Validator(std::initializer_list<ValidatorRule *> validators) :
35  d_ptr(new ValidatorPrivate(validators))
36 {
37 }
38 
39 Validator::Validator(std::initializer_list<ValidatorRule *> validators, std::initializer_list<std::pair<QString, QString> > labelDictionary) :
40  d_ptr(new ValidatorPrivate(validators, labelDictionary))
41 {
42 }
43 #endif
44 
46 {
47 }
48 
50 {
51  Q_D(Validator);
52  d->params.clear();
53  if (!d->validators.empty()) {
54  qDeleteAll(d->validators.begin(), d->validators.end());
55  d->validators.clear();
56  }
57 }
58 
59 Cutelyst::ValidatorResult Validator::validate(Context *c, ValidatorFlags flags) const
60 {
61  ValidatorResult result;
62 
63  if (!c) {
64  qCWarning(C_VALIDATOR) << "No valid Context set, aborting validation.";
65  return result;
66  }
67 
68  const ParamsMultiMap params = c->request()->parameters();
69  result = validate(params, flags);
70 
71  if (!result && flags.testFlag(FillStashOnError)) {
72  c->setStash(QStringLiteral("validationErrorStrings"), result.errorStrings());
73  c->setStash(QStringLiteral("validationErrors"), QVariant::fromValue(result.errors()));
74 
75  if (!params.isEmpty()) {
76  QMap<QString,QString>::const_iterator i = params.constBegin();
77  while (i != params.constEnd()) {
78  if (!i.key().contains(QStringLiteral("password"), Qt::CaseInsensitive)) {
79  c->setStash(i.key(), i.value());
80  }
81  ++i;
82  }
83  }
84  }
85 
86  return result;
87 }
88 
89 ValidatorResult Validator::validate(const ParamsMultiMap &params, ValidatorFlags flags) const
90 {
91  ValidatorResult result;
92 
93  Q_D(const Validator);
94 
95  if (d->validators.empty()) {
96  qCWarning(C_VALIDATOR) << "Validation started with empty validator list.";
97  return result;
98  }
99 
100  if (params.isEmpty()) {
101  qCWarning(C_VALIDATOR) << "Validation started with empty parameters.";
102  }
103 
104  const bool stopOnFirstError = flags.testFlag(StopOnFirstError);
105  const bool noTrimming = flags.testFlag(NoTrimming);
106 
107  for (ValidatorRule *v : d->validators) {
108  v->setParameters(params);
109 
110  if (v->label().isEmpty()) {
111  v->setLabel(d->labelDict.value(v->field()));
112  }
113 
114  if (noTrimming) {
115  v->setTrimBefore(false);
116  }
117 
118  const QString singleResult = v->validate();
119 
120  if (!singleResult.isEmpty()) {
121  result.addError(v->field(), singleResult);
122 
123  if (stopOnFirstError) {
124  return result;
125  }
126  }
127  }
128 
129  return result;
130 }
131 
133 {
134  Q_D(Validator);
135  d->validators.push_back(v);
136 }
137 
138 void Validator::setLabelDictionary(const QHash<QString, QString> &labelDict)
139 {
140  Q_D(Validator);
141  d->labelDict = labelDict;
142 }
143 
144 void Validator::addLabelDictionary(const QHash<QString, QString> &labelDict)
145 {
146  Q_D(Validator);
147  if (!labelDict.isEmpty()) {
148  QHash<QString, QString>::const_iterator i = labelDict.constBegin();
149  while (i != labelDict.constEnd()) {
150  d->labelDict.insert(i.key(), i.value());
151  }
152  }
153 }
154 
155 void Validator::addLabel(const QString &field, const QString &label)
156 {
157  Q_D(Validator);
158  d->labelDict.insert(field, label);
159 }
QMap< QString, QString > ParamsMultiMap
QString field() const
Returns the name of the field to validate.
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:208
The Cutelyst Context.
Definition: context.h:50
QStringList errorStrings() const
Returns a list of error messages.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validators.
Validation processor for input data.
Definition: validator.h:195
void addError(const QString &field, const QString &message)
Adds new error information to the.
void addLabel(const QString &field, const QString &label)
Adds a single entry to the label dictionary.
Definition: validator.cpp:155
virtual QString validate() const =0
Returns an empty string on success, otherwise a string containing the error message.
QHash< QString, QStringList > errors() const
Returns a dictionary containing fields with errors.
Validator()
Constructs a new Validator.
Definition: validator.cpp:28
void setTrimBefore(bool trimBefore)
Set to false to not trim input value before validation.
QString label() const
Returns the human readable field label used for generic error messages.
void clear()
Clears all internal data.
Definition: validator.cpp:49
void addValidator(ValidatorRule *v)
Adds a new validator to the list of validators.
Definition: validator.cpp:132
void setParameters(const ParamsMultiMap &params)
Sets the request parameters to validate.
void setLabel(const QString &label)
Sets human readable field label for generic error messages.
void addLabelDictionary(const QHash< QString, QString > &labelDict)
Adds a dictionary to the label dictionary.
Definition: validator.cpp:144
Contains the result of Validator.
void setLabelDictionary(const QHash< QString, QString > &labelDict)
Sets a new label dictionary.
Definition: validator.cpp:138
ValidatorResult validate(Context *c, ValidatorFlags flags=NoSpecialBehavior) const
Starts the validation process on Context c and returns a ValidatorResult.
Definition: validator.cpp:59
~Validator()
Desconstructs the Validator and all added ValidatorRule objects.
Definition: validator.cpp:45