cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatormax.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatormax_p.h"
7
8using namespace Cutelyst;
9
10ValidatorMax::ValidatorMax(const QString &field,
11 QMetaType::Type type,
12 const QVariant &max,
13 const Cutelyst::ValidatorMessages &messages,
14 const QString &defValKey)
15 : ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
16{
17}
18
20
22{
24
25 const QString v = value(params);
26
27 if (!v.isEmpty()) {
28 Q_D(const ValidatorMax);
29 bool ok = false;
30 bool valid = false;
31
32 switch (d->type) {
33 case QMetaType::Char:
34 case QMetaType::Short:
35 case QMetaType::Int:
36 case QMetaType::Long:
37 case QMetaType::LongLong:
38 {
39 const qlonglong val = c->locale().toLongLong(v, &ok);
40 if (Q_UNLIKELY(!ok)) {
41 result.errorMessage = parsingError(c);
42 qCWarning(C_VALIDATOR).noquote().nospace()
43 << debugString(c) << " Failed to parse \"" << v << "\" into an integer number";
44 } else {
45 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
46 if (Q_UNLIKELY(!ok)) {
47 result.errorMessage = validationDataError(c, 1);
48 qCWarning(C_VALIDATOR).noquote()
49 << debugString(c) << "Invalid maximum comparison value";
50 } else {
51 if (val > max) {
52 result.errorMessage =
54 QVariantMap{{QStringLiteral("val"), val},
55 {QStringLiteral("max"), max}});
56 qCDebug(C_VALIDATOR).noquote()
57 << debugString(c) << val << "is not smaller than" << max;
58 } else {
59 valid = true;
60 }
61 }
62 }
63 } break;
64 case QMetaType::UChar:
65 case QMetaType::UShort:
66 case QMetaType::UInt:
67 case QMetaType::ULong:
68 case QMetaType::ULongLong:
69 {
70 const qulonglong val = v.toULongLong(&ok);
71 if (Q_UNLIKELY(!ok)) {
72 result.errorMessage = parsingError(c);
73 qCWarning(C_VALIDATOR).noquote().nospace()
74 << debugString(c) << " Failed to parse \"" << v
75 << "\" into an unsigned integer number";
76 } else {
77 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
78 if (Q_UNLIKELY(!ok)) {
79 result.errorMessage = validationDataError(c, 1);
80 qCWarning(C_VALIDATOR).noquote()
81 << debugString(c) << "Invalid maximum comparison value";
82 } else {
83 if (val > max) {
84 result.errorMessage =
86 QVariantMap{{QStringLiteral("val"), val},
87 {QStringLiteral("max"), max}});
88 qCDebug(C_VALIDATOR).noquote()
89 << debugString(c) << val << "is not smaller than" << max;
90 } else {
91 valid = true;
92 }
93 }
94 }
95 } break;
96 case QMetaType::Float:
97 case QMetaType::Double:
98 {
99 const double val = v.toDouble(&ok);
100 if (Q_UNLIKELY(!ok)) {
101 result.errorMessage = parsingError(c);
102 qCWarning(C_VALIDATOR).noquote().nospace()
103 << debugString(c) << " Failed to parse \"" << v
104 << "\" into a floating point number";
105 } else {
106 const double max = d->extractDouble(c, params, d->max, &ok);
107 if (Q_UNLIKELY(!ok)) {
108 result.errorMessage = validationDataError(c, 1);
109 qCWarning(C_VALIDATOR).noquote()
110 << debugString(c) << "Invalid maximum comparison value";
111 } else {
112 if (val > max) {
113 result.errorMessage =
115 QVariantMap{{QStringLiteral("val"), val},
116 {QStringLiteral("max"), max}});
117 qCDebug(C_VALIDATOR).noquote()
118 << debugString(c) << val << "is not smaller than" << max;
119 } else {
120 valid = true;
121 }
122 }
123 }
124 } break;
125 case QMetaType::QString:
126 {
127 const auto val = static_cast<qlonglong>(v.length());
128 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
129 if (Q_UNLIKELY(!ok)) {
130 result.errorMessage = validationDataError(c, 1);
131 qCWarning(C_VALIDATOR).noquote()
132 << debugString(c) << "Invalid maximum comparison value";
133 } else {
134 if (val > max) {
136 c, QVariantMap{{QStringLiteral("val"), val}, {QStringLiteral("max"), max}});
137 qCDebug(C_VALIDATOR).noquote()
138 << debugString(c) << "String length" << val << "is not shorter than" << max;
139 } else {
140 valid = true;
141 }
142 }
143 } break;
144 default:
145 qCWarning(C_VALIDATOR).noquote()
146 << debugString(c) << "The comparison type" << d->type << "is not supported";
147 result.errorMessage = validationDataError(c, 0);
148 break;
149 }
150
151 if (valid) {
152 if (d->type != QMetaType::QString) {
153 const QVariant _v = d->valueToNumber(c, v, d->type);
154 if (_v.isValid()) {
155 result.value = _v;
156 } else {
157 result.errorMessage = parsingError(c);
158 }
159 } else {
160 result.value.setValue(v);
161 }
162 }
163 } else {
164 defaultValue(c, &result);
165 }
166
167 return result;
168}
169
170QString ValidatorMax::genericValidationError(Cutelyst::Context *c, const QVariant &errorData) const
171{
172 QString error;
173
174 Q_D(const ValidatorMax);
175
176 const QVariantMap map = errorData.toMap();
177 QString max;
178 switch (d->type) {
179 case QMetaType::Char:
180 case QMetaType::Short:
181 case QMetaType::Int:
182 case QMetaType::Long:
183 case QMetaType::LongLong:
184 case QMetaType::QString:
185 max = c->locale().toString(map.value(QStringLiteral("max")).toLongLong());
186 break;
187 case QMetaType::UChar:
188 case QMetaType::UShort:
189 case QMetaType::UInt:
190 case QMetaType::ULong:
191 case QMetaType::ULongLong:
192 max = c->locale().toString(map.value(QStringLiteral("max")).toULongLong());
193 break;
194 case QMetaType::Float:
195 case QMetaType::Double:
196 max = c->locale().toString(map.value(QStringLiteral("max")).toDouble());
197 break;
198 default:
199 error = validationDataError(c);
200 return error;
201 }
202
203 const QString _label = label(c);
204
205 if (_label.isEmpty()) {
206 if (d->type == QMetaType::QString) {
207 error = c->translate("Cutelyst::ValidatorMax",
208 "The text must be shorter than %1 characters.")
209 .arg(max);
210 } else {
211 error =
212 c->translate("Cutelyst::ValidatorMax", "The value must be lower than %1.").arg(max);
213 }
214 } else {
215 if (d->type == QMetaType::QString) {
216 error = c->translate("Cutelyst::ValidatorMax",
217 "The text in the “%1“ field must be shorter than %2 characters.")
218 .arg(_label, max);
219 } else {
220 error = c->translate("Cutelyst::ValidatorMax",
221 "The value in the “%1” field must be lower than %2.")
222 .arg(_label, max);
223 }
224 }
225
226 return error;
227}
228
229QString ValidatorMax::genericValidationDataError(Context *c, const QVariant &errorData) const
230{
231 QString error;
232
233 int field = errorData.toInt();
234 const QString _label = label(c);
235
236 if (field == 0) {
237 Q_D(const ValidatorMax);
238 if (_label.isEmpty()) {
239 error = c->translate("Cutelyst::ValidatorMax",
240 "The comparison type with ID %1 is not supported.")
241 .arg(static_cast<int>(d->type));
242 } else {
243 error =
244 c->translate("Cutelyst::ValidatorMax",
245 "The comparison type with ID %1 for the “%2” field is not supported.")
246 .arg(QString::number(static_cast<int>(d->type)), _label);
247 }
248 } else if (field == 1) {
249 if (_label.isEmpty()) {
250 error = c->translate("Cutelyst::ValidatorMax",
251 "The maximum comparison value is not valid.");
252 } else {
253 error = c->translate("Cutelyst::ValidatorMax",
254 "The maximum comparison value for the “%1” field is not valid.")
255 .arg(_label);
256 }
257 }
258
259 return error;
260}
261
262QString ValidatorMax::genericParsingError(Context *c, const QVariant &errorData) const
263{
264 QString error;
265 Q_UNUSED(errorData)
266 Q_D(const ValidatorMax);
267
268 const QString _label = label(c);
269 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
270 if (_label.isEmpty()) {
271 error = c->translate("Cutelyst::ValidatorMax",
272 "Failed to parse the input value into a floating point number.");
273 } else {
274 error = c->translate("Cutelyst::ValidatorMax",
275 "Failed to parse the input value for the “%1” field into a "
276 "floating point number.")
277 .arg(_label);
278 }
279 } else {
280 if (_label.isEmpty()) {
281 error = c->translate("Cutelyst::ValidatorMax",
282 "Failed to parse the input value into an integer number.");
283 } else {
284 error =
285 c->translate(
286 "Cutelyst::ValidatorMax",
287 "Failed to parse the input value for the “%1” field into an integer number.")
288 .arg(_label);
289 }
290 }
291
292 return error;
293}
The Cutelyst Context.
Definition: context.h:38
QLocale locale() const noexcept
Definition: context.cpp:453
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Checks if a value is not bigger or longer than a maximum value.
Definition: validatormax.h:46
~ValidatorMax() override
Deconstructs the max validator.
ValidatorMax(const QString &field, QMetaType::Type type, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new max validator.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
Base class for all validator rules.
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.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
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.
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