cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorsize.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorsize_p.h"
7
8using namespace Cutelyst;
9
11 QMetaType::Type type,
12 const QVariant &size,
13 const Cutelyst::ValidatorMessages &messages,
14 const QString &defValKey)
15 : ValidatorRule(*new ValidatorSizePrivate(field, type, size, messages, defValKey))
16{
17}
18
20
22{
24
25 const QString v = value(params);
26
27 if (!v.isEmpty()) {
28
29 Q_D(const ValidatorSize);
30 bool ok = false;
31 bool valid = false;
32
33 switch (d->type) {
35 case QMetaType::Int:
36 case QMetaType::Long:
38 {
39 const auto 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 size = d->extractLongLong(c, params, d->size, &ok);
46 if (Q_UNLIKELY(!ok)) {
47 result.errorMessage = validationDataError(c, 1);
48 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison size";
49 } else {
50 if (val != size) {
51 result.errorMessage = validationError(c, size);
52 qCDebug(C_VALIDATOR).noquote() << debugString(c) << val << "!=" << size;
53 } else {
54 valid = true;
55 }
56 }
57 }
58 } break;
60 case QMetaType::UInt:
63 {
64 const auto val = v.toULongLong(&ok);
65 if (Q_UNLIKELY(!ok)) {
66 result.errorMessage = parsingError(c);
67 qCWarning(C_VALIDATOR).noquote().nospace()
68 << debugString(c) << "Failed to parse \"" << v
69 << "\" into an unsigned integer number";
70 } else {
71 const qulonglong size = d->extractULongLong(c, params, d->size, &ok);
72 if (Q_UNLIKELY(!ok)) {
73 result.errorMessage = validationDataError(c, 1);
74 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison size";
75 } else {
76 if (val != size) {
77 result.errorMessage = validationError(c, size);
78 qCDebug(C_VALIDATOR).noquote() << debugString(c) << val << "!=" << size;
79 } else {
80 valid = true;
81 }
82 }
83 }
84 } break;
87 {
88 const auto val = v.toDouble(&ok);
89 if (Q_UNLIKELY(!ok)) {
90 result.errorMessage = parsingError(c);
91 qCWarning(C_VALIDATOR).noquote().nospace()
92 << debugString(c) << "Failed to parse \"" << v
93 << "\" into a floating point number";
94 } else {
95 const double size = d->extractDouble(c, params, d->size, &ok);
96 if (Q_UNLIKELY(!ok)) {
97 result.errorMessage = validationDataError(c, 1);
98 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison size";
99 } else {
100 if (val != size) {
101 result.errorMessage = validationError(c, size);
102 qCDebug(C_VALIDATOR).noquote() << debugString(c) << val << "!=" << size;
103 } else {
104 valid = true;
105 }
106 }
107 }
108 } break;
110 {
111 const auto val = static_cast<qlonglong>(v.length());
112 const qlonglong size = d->extractLongLong(c, params, d->size, &ok);
113 if (Q_UNLIKELY(!ok)) {
114 result.errorMessage = validationDataError(c, 1);
115 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison size";
116 } else {
117 if (val != size) {
118 result.errorMessage = validationError(c, size);
119 qCDebug(C_VALIDATOR).noquote()
120 << debugString(c) << "string length" << val << "!=" << size;
121 } else {
122 valid = true;
123 }
124 }
125 } break;
126 default:
127 qCWarning(C_VALIDATOR).noquote()
128 << debugString(c) << "The comparison type" << d->type << "is not supported";
129 result.errorMessage = validationDataError(c, 0);
130 break;
131 }
132
133 if (valid) {
134 if (d->type != QMetaType::QString) {
135 const QVariant _v = d->valueToNumber(c, v, d->type);
136 if (_v.isValid()) {
137 result.value = _v;
138 } else {
139 result.errorMessage = parsingError(c);
140 }
141 } else {
142 result.value.setValue(v);
143 }
144 }
145 } else {
146 defaultValue(c, &result);
147 }
148
149 return result;
150}
151
153{
154 Q_D(const ValidatorSize);
155
156 QString size;
157 switch (d->type) {
158 case QMetaType::Short:
159 case QMetaType::Int:
160 case QMetaType::Long:
163 size = c->locale().toString(errorData.toLongLong());
164 break;
166 case QMetaType::UInt:
167 case QMetaType::ULong:
169 size = c->locale().toString(errorData.toULongLong());
170 break;
171 case QMetaType::Float:
173 size = c->locale().toString(errorData.toDouble());
174 break;
175 default:
176 return validationDataError(c, 0);
177 }
178
179 const QString _label = label(c);
180
181 if (_label.isEmpty()) {
182 if (d->type == QMetaType::QString) {
183 //% "The text must be exactly %1 characters long."
184 return c->qtTrId("cutelyst-valsize-genvalerr-str").arg(size);
185 } else {
186 //% "The value must be %1."
187 return c->qtTrId("cutelyst-valsize-genvalerr-num").arg(size);
188 }
189 } else {
190 if (d->type == QMetaType::QString) {
191 //: %1 will be replaced by the field label, %2 will be replaced by the required string
192 //: size
193 //% "The text in the “%1“ field must be exactly %2 characters long."
194 return c->qtTrId("cutelyst-valsize-genvalerr-str-label").arg(_label, size);
195 } else {
196 //: %1 will be replaced by the field label, %2 will be replaced by the required
197 //: size/value
198 //% "The value in the “%1” field must be %2."
199 return c->qtTrId("cutelyst-valsize-genvalerr-num-label").arg(_label, size);
200 }
201 }
202}
203
205{
206 int field = errorData.toInt();
207 const QString _label = label(c);
208
209 if (field == 0) {
210 Q_D(const ValidatorSize);
211 const QMetaType _type(d->type);
212 if (_label.isEmpty()) {
213 return c->qtTrId("cutelyst-validator-genvaldataerr-type")
214 .arg(QString::fromLatin1(_type.name()));
215 } else {
216 return c->qtTrId("cutelyst-validator-genvaldataerr-type-label")
217 .arg(QString::fromLatin1(_type.name()), _label);
218 }
219 } else {
220 if (_label.isEmpty()) {
221 //% "The comparison value is not valid."
222 return c->qtTrId("cutelyst-valsize-genvaldataerr-size");
223 } else {
224 //: %1 will be replaced by the field label
225 //% "The comparison value for the “%1” field is not valid."
226 return c->qtTrId("cutelyst-valsize-genvaldataerr-size-label").arg(_label);
227 }
228 }
229}
230
232{
233 Q_UNUSED(errorData)
234 Q_D(const ValidatorSize);
235
236 // translation strings are defined in ValidatorBetween
237
238 const QString _label = label(c);
239 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
240 if (_label.isEmpty()) {
241 return c->qtTrId("cutelyst-validator-genparseerr-float");
242 } else {
243 return c->qtTrId("cutelyst-validator-genparseerr-float-label").arg(_label);
244 }
245 } else {
246 if (_label.isEmpty()) {
247 return c->qtTrId("cutelyst-validator-genparseerr-int");
248 } else {
249 return c->qtTrId("cutelyst-validator-genparseerr-int-label").arg(_label);
250 }
251 }
252}
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
Base class for all validator rules.
QString field() const noexcept
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 field under validation must have a size matching the given value.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorSize(const QString &field, QMetaType::Type type, const QVariant &size, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericParsingError(Context *c, const QVariant &errorData) const override
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
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
QMultiMap::size_type size() 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.