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