cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validator.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validator_p.h"
7 
8 #include <Cutelyst/application.h>
9 #include <Cutelyst/context.h>
10 #include <Cutelyst/request.h>
11 
12 #include <QLoggingCategory>
13 
14 using namespace Cutelyst;
15 
16 Q_LOGGING_CATEGORY(C_VALIDATOR, "cutelyst.utils.validator", QtWarningMsg)
17 
18 Validator::Validator(const char *translationContext)
19  : d_ptr(new ValidatorPrivate(translationContext))
20 {
21 }
22 
23 Validator::Validator(std::initializer_list<ValidatorRule *> validators,
24  const char *translationContext)
25  : d_ptr(new ValidatorPrivate(validators, translationContext))
26 {
27 }
28 
29 Validator::~Validator() = default;
30 
32 {
33  Q_D(Validator);
34  d->params.clear();
35  if (!d->validators.empty()) {
36  qDeleteAll(d->validators.begin(), d->validators.end());
37  d->validators.clear();
38  }
39 }
40 
41 Cutelyst::ValidatorResult Validator::validate(Context *c, ValidatorFlags flags) const
42 {
43  ValidatorResult result;
44 
45  Q_ASSERT(c);
46 
47  ParamsMultiMap params;
48  if (flags.testFlag(BodyParamsOnly)) {
49  params = c->req()->bodyParameters();
50  } else if (flags.testFlag(QueryParamsOnly)) {
51  params = c->req()->queryParameters();
52  } else {
53  params = c->req()->queryParameters();
54  params.unite(c->req()->bodyParameters());
55  }
56 
57  result = validate(c, params, flags);
58 
59  return result;
60 }
61 
63  Validator::validate(Context *c, const ParamsMultiMap &params, ValidatorFlags flags) const
64 {
65  ValidatorResult result;
66 
67  Q_ASSERT(c);
68  Q_D(const Validator);
69 
70  if (d->validators.empty()) {
71  qCWarning(C_VALIDATOR) << "Validation started with empty validator list.";
72  return result;
73  }
74 
75  if (params.empty()) {
76  qCWarning(C_VALIDATOR) << "Validation started with empty parameters.";
77  }
78 
79  const bool stopOnFirstError = flags.testFlag(StopOnFirstError);
80  const bool noTrimming = flags.testFlag(NoTrimming);
81 
82  for (ValidatorRule *v : d->validators) {
83 
84  if (noTrimming) {
85  v->setTrimBefore(false);
86  }
87 
88  const ValidatorReturnType singleResult = v->validate(c, params);
89 
90  if (singleResult.extra.isValid()) {
91  result.addExtra(v->field(), singleResult.extra);
92  }
93 
94  if (singleResult) {
95  result.addValue(v->field(), singleResult.value);
96  } else {
97  result.addError(v->field(), singleResult.errorMessage);
98  if (stopOnFirstError) {
99  break;
100  }
101  }
102  }
103 
104  if (!result && flags.testFlag(FillStashOnError)) {
105  c->setStash(u"validationErrorStrings"_qs, result.errorStrings());
106  c->setStash(u"validationErrors"_qs, QVariant::fromValue(result.errors()));
107 
108  if (!params.isEmpty()) {
109  auto i = params.constBegin();
110  while (i != params.constEnd()) {
111  if (!i.key().contains(u"password"_qs, Qt::CaseInsensitive)) {
112  c->setStash(i.key(), i.value());
113  }
114  ++i;
115  }
116  }
117  }
118 
119  return result;
120 }
121 
123 {
124  Q_D(Validator);
125  v->setTranslationContext(d->translationContext);
126  d->validators.push_back(v);
127 }
128 
130 {
131  app->loadTranslations(u"plugin_utils_validator"_qs);
132 }
The Cutelyst application.
Definition: application.h:66
void loadTranslations(const QString &filename, const QString &directory={}, const QString &prefix={}, const QString &suffix={})
The Cutelyst Context.
Definition: context.h:42
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:212
Request * req
Definition: context.h:66
ParamsMultiMap bodyParameters() const
Definition: request.cpp:219
ParamsMultiMap queryParameters() const
Definition: request.cpp:255
Provides information about performed validations.
void addExtra(const QString &field, const QVariant &extra)
void addValue(const QString &field, const QVariant &value)
QHash< QString, QStringList > errors() const noexcept
void addError(const QString &field, const QString &message)
QStringList errorStrings() const
Base class for all validator rules.
QString field() const noexcept
virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const =0
Validation processor for input data.
Definition: validator.h:274
ValidatorResult validate(Context *c, ValidatorFlags flags=NoSpecialBehavior) const
Definition: validator.cpp:41
Validator(const char *translationContext=nullptr)
Definition: validator.cpp:18
void addValidator(ValidatorRule *v)
Definition: validator.cpp:122
static void loadTranslations(Application *app)
Definition: validator.cpp:129
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap::const_iterator constBegin() const const
QMultiMap::const_iterator constEnd() const const
bool empty() const const
bool isEmpty() const const
QMultiMap< Key, T > & unite(QMultiMap< Key, T > &&other)
CaseInsensitive
QVariant fromValue(T &&value)
bool isValid() const const
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49