cutelyst 4.0.0
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-2023 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
11using namespace Cutelyst;
12
13ValidatorRule::ValidatorRule(const QString &field,
14 const ValidatorMessages &messages,
15 const QString &defValKey,
16 QByteArrayView validatorName)
17 : d_ptr(new ValidatorRulePrivate(field, messages, defValKey, validatorName))
18{
19}
20
21ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd)
22 : d_ptr(&dd)
23{
24}
25
27
28QString 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 QString l;
52 Q_D(const ValidatorRule);
53 if (d->messages.label) {
54 if (d->translationContext.size()) {
55 l = c->translate(d->translationContext.data(), d->messages.label);
56 } else {
57 l = QString::fromUtf8(d->messages.label);
58 }
59 }
60 return l;
61}
62
63QString ValidatorRule::validationError(Cutelyst::Context *c, const QVariant &errorData) const
64{
65 QString error;
66 Q_UNUSED(errorData)
67 Q_D(const ValidatorRule);
68 if (d->messages.validationError) {
69 if (d->translationContext.size()) {
70 error = c->translate(d->translationContext.data(), d->messages.validationError);
71 } else {
72 error = QString::fromUtf8(d->messages.validationError);
73 }
74 } else {
75 error = genericValidationError(c, errorData);
76 }
77 return error;
78}
79
80QString ValidatorRule::genericValidationError(Context *c, const QVariant &errorData) const
81{
82 QString error;
83 Q_UNUSED(errorData)
84 const QString _label = label(c);
85 if (!_label.isEmpty()) {
86 error = c->translate("Cutelyst::ValidatorRule",
87 "The input data in the “%1” field is not acceptable.")
88 .arg(_label);
89 } else {
90 error = c->translate("Cutelyst::ValidatorRule", "The input data is not acceptable.");
91 }
92 return error;
93}
94
95QString ValidatorRule::parsingError(Cutelyst::Context *c, const QVariant &errorData) const
96{
97 QString error;
98 Q_D(const ValidatorRule);
99 Q_UNUSED(errorData)
100 if (d->messages.parsingError) {
101 if (d->translationContext.size()) {
102 error = c->translate(d->translationContext.data(), d->messages.parsingError);
103 } else {
104 error = QString::fromUtf8(d->messages.parsingError);
105 }
106 } else {
107 error = genericParsingError(c, errorData);
108 }
109 return error;
110}
111
112QString ValidatorRule::genericParsingError(Cutelyst::Context *c, const QVariant &errorData) const
113{
114 QString error;
115 Q_UNUSED(errorData)
116 const QString _label = label(c);
117 if (!_label.isEmpty()) {
118 error = c->translate("Cutelyst::ValidatorRule",
119 "The input data in the “%1“ field could not be parsed.")
120 .arg(_label);
121 } else {
122 error = c->translate("Cutelyst::ValidatorRule", "The input data could not be parsed.");
123 }
124 return error;
125}
126
127QString ValidatorRule::validationDataError(Context *c, const QVariant &errorData) const
128{
129 QString error;
130 Q_D(const ValidatorRule);
131 Q_UNUSED(errorData)
132 if (d->messages.validationDataError) {
133 if (d->translationContext.size()) {
134 error = c->translate(d->translationContext.data(), d->messages.validationDataError);
135 } else {
136 error = QString::fromUtf8(d->messages.validationDataError);
137 }
138 } else {
139 error = genericValidationDataError(c, errorData);
140 }
141 return error;
142}
143
144QString ValidatorRule::genericValidationDataError(Context *c, const QVariant &errorData) const
145{
146 QString error;
147 Q_UNUSED(errorData)
148 const QString _label = label(c);
149 if (!_label.isEmpty()) {
150 error = c->translate("Cutelyst::ValidatorRule",
151 "Missing or invalid validation data for the “%1” field.")
152 .arg(_label);
153 } else {
154 error = c->translate("Cutelyst::ValidatorRule", "Missing or invalid validation data.");
155 }
156 return error;
157}
158
160{
161 Q_ASSERT_X(c, "getting default value", "invalid context object");
162 Q_ASSERT_X(result, "getting default value", "invalid result object");
163 Q_D(const ValidatorRule);
164 if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
165 result->value.setValue(c->stash(d->defValKey));
166 qCDebug(C_VALIDATOR).noquote().nospace()
167 << d->validatorName << ": Using default value " << result->value << " for field \""
168 << field() << "\" at " << c->controllerName() << "::" << c->actionName();
169 }
170}
171
173{
174 Q_D(const ValidatorRule);
175 return QString::fromLatin1(d->validatorName) +
176 QLatin1String(": Validation failed for field \"") + field() + QLatin1String("\" at ") +
177 c->controllerName() + QLatin1String("::") + c->actionName() + QLatin1Char(':');
178}
179
180bool ValidatorRule::trimBefore() const noexcept
181{
182 Q_D(const ValidatorRule);
183 return d->trimBefore;
184}
185
186void ValidatorRule::setTrimBefore(bool trimBefore) noexcept
187{
188 Q_D(ValidatorRule);
189 d->trimBefore = trimBefore;
190}
191
192void ValidatorRule::setTranslationContext(QLatin1String trContext) noexcept
193{
194 Q_D(ValidatorRule);
195 d->translationContext = trContext;
196}
The Cutelyst Context.
Definition: context.h:38
void stash(const QVariantHash &unite)
Definition: context.cpp:553
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Base class for all validator rules.
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage if validation failed.
QString field() const noexcept
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString(), QByteArrayView validatorName=nullptr)
Constructs a new ValidatorRule with the given parameters.
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
bool trimBefore() const noexcept
Returns true if the field value should be trimmed before validation.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49