cutelyst  4.4.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 
8 using namespace Cutelyst;
9 
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 
19 ValidatorMin::~ValidatorMin() = default;
20 
22 {
23  ValidatorReturnType result;
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:
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)) {
49  c, static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidMin));
50  qCWarning(C_VALIDATOR).noquote()
51  << debugString(c) << "Invalid minimum comparison value";
52  } else {
53  if (val < min) {
54  result.errorMessage = validationError(c, min);
55  qCDebug(C_VALIDATOR).noquote()
56  << debugString(c) << val << "is not greater than" << min;
57  } else {
58  valid = true;
59  }
60  }
61  }
62  } break;
63  case QMetaType::UChar:
64  case QMetaType::UShort:
65  case QMetaType::UInt:
66  case QMetaType::ULong:
68  {
69  const qulonglong val = v.toULongLong(&ok);
70  if (Q_UNLIKELY(!ok)) {
71  result.errorMessage = parsingError(c);
72  qCWarning(C_VALIDATOR).noquote().nospace()
73  << debugString(c) << " Failed to parse \"" << v
74  << "\" into an unsigned integer number";
75  } else {
76  const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
77  if (Q_UNLIKELY(!ok)) {
79  c, static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidMin));
80  qCWarning(C_VALIDATOR).noquote()
81  << debugString(c) << "Invalid minimum comparison value";
82  } else {
83  if (val < min) {
84  result.errorMessage = validationError(c, min);
85  qCDebug(C_VALIDATOR).noquote()
86  << debugString(c) << val << "is not greater than" << min;
87  } else {
88  valid = true;
89  }
90  }
91  }
92  } break;
93  case QMetaType::Float:
94  case QMetaType::Double:
95  {
96  const double val = v.toDouble(&ok);
97  if (Q_UNLIKELY(!ok)) {
98  result.errorMessage = parsingError(c);
99  qCWarning(C_VALIDATOR).noquote().nospace()
100  << debugString(c) << " Failed to parse \"" << v
101  << "\" into a floating point number";
102  } else {
103  const double min = d->extractDouble(c, params, d->min, &ok);
104  if (Q_UNLIKELY(!ok)) {
106  c, static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidMin));
107  qCWarning(C_VALIDATOR).noquote()
108  << debugString(c) << "Invalid minimum comparison value";
109  } else {
110  if (val < min) {
111  result.errorMessage = validationError(c, min);
112  qCDebug(C_VALIDATOR).noquote()
113  << debugString(c) << val << "is not greater than" << min;
114  } else {
115  valid = true;
116  }
117  }
118  }
119  } break;
120  case QMetaType::QString:
121  {
122  const auto val = static_cast<qlonglong>(v.length());
123  const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
124  if (Q_UNLIKELY(!ok)) {
126  c, static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidMin));
127  qCWarning(C_VALIDATOR).noquote()
128  << debugString(c) << "Invalid minimum comparison value";
129  } else {
130  if (val < min) {
131  result.errorMessage = validationError(c, min);
132  qCDebug(C_VALIDATOR).noquote()
133  << debugString(c) << "String length" << val << "is not longer than" << min;
134  } else {
135  valid = true;
136  }
137  }
138  } break;
139  default:
140  qCWarning(C_VALIDATOR).noquote()
141  << debugString(c) << "The comparison type" << d->type << "is not supported";
143  c, static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidType));
144  break;
145  }
146 
147  if (valid) {
148  if (d->type != QMetaType::QString) {
149  const QVariant _v = d->valueToNumber(c, v, d->type);
150  if (_v.isValid()) {
151  result.value = _v;
152  } else {
153  result.errorMessage = parsingError(c);
154  }
155  } else {
156  result.value.setValue(v);
157  }
158  }
159  } else {
160  defaultValue(c, &result);
161  }
162 
163  return result;
164 }
165 
167 {
168  Q_D(const ValidatorMin);
169 
170  QString min;
171  switch (d->type) {
172  case QMetaType::Char:
173  case QMetaType::Short:
174  case QMetaType::Int:
175  case QMetaType::Long:
176  case QMetaType::LongLong:
177  case QMetaType::QString:
178  min = c->locale().toString(errorData.toLongLong());
179  break;
180  case QMetaType::UChar:
181  case QMetaType::UShort:
182  case QMetaType::UInt:
183  case QMetaType::ULong:
185  min = c->locale().toString(errorData.toULongLong());
186  break;
187  case QMetaType::Float:
188  case QMetaType::Double:
189  min = c->locale().toString(errorData.toDouble());
190  break;
191  default:
192  return validationDataError(c,
193  static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidType));
194  }
195 
196  const QString _label = label(c);
197 
198  if (_label.isEmpty()) {
199  if (d->type == QMetaType::QString) {
200  //% "The text must be longer than %1 characters."
201  return c->qtTrId("cutelyst-valmin-genvalerr-str").arg(min);
202  } else {
203  //% "The value must be greater than %1."
204  return c->qtTrId("cutelyst-valmin-genvalerr-num").arg(min);
205  }
206  } else {
207  if (d->type == QMetaType::QString) {
208  //% "The text in the “%1“ field must be longer than %2 characters."
209  return c->qtTrId("cutelyst-valmin-genvalerr-str-label").arg(_label, min);
210  } else {
211  //% "The value in the “%1” field must be greater than %2."
212  return c->qtTrId("cutelyst-valmin-genvalerr-num-label").arg(_label, min);
213  }
214  }
215 }
216 
218 {
219  const QString _label = label(c);
220  const auto errorType = static_cast<ValidatorRulePrivate::ErrorType>(errorData.toInt());
221 
222  // translation strings are defined in ValidatorBetween
223 
224  if (_label.isEmpty()) {
225  switch (errorType) {
226  case ValidatorRulePrivate::ErrorType::InvalidType:
227  {
228  Q_D(const ValidatorMin);
229  const QMetaType _type(d->type);
230  return c->qtTrId("cutelyst-validator-genvaldataerr-type")
231  .arg(QString::fromLatin1(_type.name()));
232  }
233  case ValidatorRulePrivate::ErrorType::InvalidMin:
234  return c->qtTrId("cutelyst-validator-genvaldataerr-min");
235  case ValidatorRulePrivate::ErrorType::InvalidMax:
236  // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while)
237  Q_UNREACHABLE();
238  return {};
239  }
240  } else {
241  switch (errorType) {
242  case ValidatorRulePrivate::ErrorType::InvalidType:
243  {
244  Q_D(const ValidatorMin);
245  const QMetaType _type(d->type);
246  return c->qtTrId("cutelyst-validator-genvaldataerr-type-label")
247  .arg(QString::fromLatin1(_type.name()), _label);
248  }
249  case ValidatorRulePrivate::ErrorType::InvalidMin:
250  return c->qtTrId("cutelyst-validator-genvaldataerr-min-label").arg(_label);
251  case ValidatorRulePrivate::ErrorType::InvalidMax:
252  // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while)
253  Q_UNREACHABLE();
254  return {};
255  }
256  }
257 
258 #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
259  // NOLINTNEXTLINE(cppcoreguidelines-avoid-do-while)
260  Q_UNREACHABLE_RETURN({});
261 #else
262  return {};
263 #endif
264 }
265 
267 {
268  Q_UNUSED(errorData)
269  Q_D(const ValidatorMin);
270 
271  // translation strings are defined in ValidatorBetween
272 
273  const QString _label = label(c);
274  if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
275  if (_label.isEmpty()) {
276  return c->qtTrId("cutelyst-validator-genparseerr-float");
277  } else {
278  return c->qtTrId("cutelyst-validator-genparseerr-float-label").arg(_label);
279  }
280  } else {
281  if (_label.isEmpty()) {
282  return c->qtTrId("cutelyst-validator-genparseerr-int");
283  } else {
284  return c->qtTrId("cutelyst-validator-genparseerr-int-label").arg(_label);
285  }
286  }
287 }
The Cutelyst Context.
Definition: context.h:42
QLocale locale() const noexcept
Definition: context.cpp:460
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
Checks if a value is not smaller or shorter than a maximum value.
Definition: validatormin.h:47
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString genericParsingError(Context *c, const QVariant &errorData) const override
ValidatorMin(const QString &field, QMetaType::Type type, const QVariant &min, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString parsingError(Context *c, const QVariant &errorData={}) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
qlonglong toLongLong(QStringView s, bool *ok) const const
QString toString(QDate date, QLocale::FormatType format) const const
const char * name() const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
qsizetype length() const const
double toDouble(bool *ok) const const
qulonglong toULongLong(bool *ok, int base) const const
bool isValid() const const
void setValue(QVariant &&value)
double toDouble(bool *ok) const const
int toInt(bool *ok) const const
qlonglong toLongLong(bool *ok) const const
qulonglong toULongLong(bool *ok) const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49