cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrule.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrule_p.h"
7 
8 #include <Cutelyst/Context>
9 #include <Cutelyst/ParamsMultiMap>
10 
11 using namespace Cutelyst;
12 
14  const ValidatorMessages &messages,
15  const QString &defValKey,
16  QByteArrayView validatorName)
17  : d_ptr(new ValidatorRulePrivate(field, messages, defValKey, validatorName))
18 {
19 }
20 
21 ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd)
22  : d_ptr(&dd)
23 {
24 }
25 
27 
28 QString ValidatorRule::field() const noexcept
29 {
30  Q_D(const ValidatorRule);
31  return d->field;
32 }
33 
35 {
36  Q_D(const ValidatorRule);
37 
38  if (!d->field.isEmpty() && !params.empty()) {
39  if (d->trimBefore) {
40  return params.value(d->field).trimmed();
41  } else {
42  return params.value(d->field);
43  }
44  }
45 
46  return {};
47 }
48 
50 {
51  Q_D(const ValidatorRule);
52 
53  if (d->messages.label) {
54  return d->translationContext ? c->translate(d->translationContext, d->messages.label)
55  : c->qtTrId(d->messages.label);
56  }
57 
58  return {};
59 }
60 
62  const ParamsMultiMap &params,
63  Cutelyst::ValidatorRtFn cb) const
64 {
65  Q_UNUSED(params)
66  qCCritical(C_VALIDATOR).noquote()
67  << debugString(c) << "validateCb method is not implemented on this validator";
68  //% "The validateCb method is no implemented for this validator."
70  .errorMessage = c->qtTrId("cutelyst-valrule-cb-not-impl-err"), .value = {}, .extra = {}});
71 }
72 
74 {
75  Q_UNUSED(errorData)
76  Q_D(const ValidatorRule);
77 
78  if (d->messages.validationError) {
79  return d->translationContext
80  ? c->translate(d->translationContext, d->messages.validationError)
81  : c->qtTrId(d->messages.validationError);
82  }
83 
84  return genericValidationError(c, errorData);
85 }
86 
88 {
89  Q_UNUSED(errorData)
90  const QString _label = label(c);
91  if (_label.isEmpty()) {
92  //% "The input data is not acceptable."
93  return c->qtTrId("cutelyst-valrule-genvalerr");
94  } else {
95  //: %1 will be replaced by the field label
96  //% "The input data in the “%1” field is not acceptable."
97  return c->qtTrId("cutelyst-valrule-genvalerr-label").arg(_label);
98  }
99 }
100 
102 {
103  Q_D(const ValidatorRule);
104  Q_UNUSED(errorData)
105 
106  if (d->messages.parsingError) {
107  return d->translationContext ? c->translate(d->translationContext, d->messages.parsingError)
108  : c->qtTrId(d->messages.parsingError);
109  }
110 
111  return genericParsingError(c, errorData);
112 }
113 
115 {
116  Q_UNUSED(errorData)
117  const QString _label = label(c);
118  if (_label.isEmpty()) {
119  //% "The input data could not be parsed."
120  return c->qtTrId("cutelyst-valrule-genparseerr");
121  } else {
122  //: %1 will be replaced by the field label
123  //% "The input data in the “%1“ field could not be parsed."
124  return c->qtTrId("cutelyst-valrule-genparseerr-label").arg(_label);
125  }
126 }
127 
129 {
130  Q_D(const ValidatorRule);
131  Q_UNUSED(errorData)
132 
133  if (d->messages.validationDataError) {
134  return d->translationContext
135  ? c->translate(d->translationContext, d->messages.validationDataError)
136  : c->qtTrId(d->messages.validationDataError);
137  }
138 
139  return genericValidationDataError(c, errorData);
140 }
141 
143 {
144  Q_UNUSED(errorData)
145  const QString _label = label(c);
146  if (_label.isEmpty()) {
147  //% "Missing or invalid validation data."
148  return c->qtTrId("cutelyst-valrule-genvaldataerr");
149  } else {
150  //: %1 will be replaced by the field label
151  //% "Missing or invalid validation data for the “%1” field."
152  return c->qtTrId("cutelyst-valrule-genvaldataerr-label").arg(_label);
153  }
154 }
155 
157 {
158  Q_ASSERT_X(c, "getting default value", "invalid context object");
159  Q_ASSERT_X(result, "getting default value", "invalid result object");
160  Q_D(const ValidatorRule);
161  if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
162  result->value.setValue(c->stash(d->defValKey));
163  qCDebug(C_VALIDATOR).noquote().nospace()
164  << d->validatorName << ": Using default value " << result->value << " for field \""
165  << field() << "\" at " << c->controllerName() << "::" << c->actionName();
166  }
167 }
168 
170 {
171  Q_ASSERT_X(c, "getting default value", "invalid context object");
172  Q_D(const ValidatorRule);
173  if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
174  const auto defVal = c->stash(d->defValKey);
175  qCDebug(C_VALIDATOR).noquote().nospace()
176  << d->validatorName << ": Using default value " << defVal << " for field \"" << field()
177  << "\" at " << c->controllerName() << "::" << c->actionName();
178  cb({.errorMessage = {}, .value = defVal});
179  } else {
180  cb({});
181  }
182 }
183 
185 {
186  Q_D(const ValidatorRule);
187  return QString::fromLatin1(d->validatorName) +
188  QLatin1String(": Validation failed for field \"") + field() + QLatin1String("\" at ") +
189  c->controllerName() + u"::" + c->actionName() + u':';
190 }
191 
192 bool ValidatorRule::trimBefore() const noexcept
193 {
194  Q_D(const ValidatorRule);
195  return d->trimBefore;
196 }
197 
198 void ValidatorRule::setTrimBefore(bool trimBefore) noexcept
199 {
200  Q_D(ValidatorRule);
201  d->trimBefore = trimBefore;
202 }
203 
204 void ValidatorRule::setTranslationContext(const char *trContext) noexcept
205 {
206  Q_D(ValidatorRule);
207  d->translationContext = trContext;
208 }
virtual QString genericValidationDataError(Context *c, const QVariant &errorData={}) const
Stores custom error messages and the input field label.
ValidatorRule(const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr)
bool trimBefore() const noexcept
The Cutelyst Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
void stash(const QVariantHash &unite)
Definition: context.cpp:562
QString actionName
Definition: context.h:52
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:485
QString parsingError(Context *c, const QVariant &errorData={}) const
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
virtual QString genericValidationError(Context *c, const QVariant &errorData={}) const
bool empty() const const
QString field() const noexcept
virtual void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString fromLatin1(QByteArrayView str)
QString validationError(Context *c, const QVariant &errorData={}) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
virtual QString genericParsingError(Context *c, const QVariant &errorData={}) const
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString arg(Args &&... args) const const
QString controllerName
Definition: context.h:80
T value(const Key &key, const T &defaultValue) const const
void setValue(QVariant &&value)