6#include "validatorsize_p.h"
14 const QString &defValKey)
15 :
ValidatorRule(*new ValidatorSizePrivate(field, type, size, messages, defValKey))
25 const QString v =
value(params);
34 case QMetaType::Short:
37 case QMetaType::LongLong:
39 const auto val = c->
locale().toLongLong(v, &ok);
40 if (Q_UNLIKELY(!ok)) {
42 qCWarning(C_VALIDATOR).noquote().nospace()
43 <<
debugString(c) <<
"Failed to parse \"" << v <<
"\" into an integer number";
45 const qlonglong size = d->extractLongLong(c, params, d->size, &ok);
46 if (Q_UNLIKELY(!ok)) {
48 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison size";
53 QVariantMap{{QStringLiteral(
"val"), val},
54 {QStringLiteral(
"size"), size}});
55 qCDebug(C_VALIDATOR).noquote() <<
debugString(c) << val <<
"!=" << size;
62 case QMetaType::UShort:
64 case QMetaType::ULong:
65 case QMetaType::ULongLong:
67 const auto val = v.toULongLong(&ok);
68 if (Q_UNLIKELY(!ok)) {
70 qCWarning(C_VALIDATOR).noquote().nospace()
72 <<
"\" into an unsigned integer number";
74 const qulonglong size = d->extractULongLong(c, params, d->size, &ok);
75 if (Q_UNLIKELY(!ok)) {
77 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison size";
82 QVariantMap{{QStringLiteral(
"val"), val},
83 {QStringLiteral(
"size"), size}});
84 qCDebug(C_VALIDATOR).noquote() <<
debugString(c) << val <<
"!=" << size;
91 case QMetaType::Float:
92 case QMetaType::Double:
94 const auto val = v.toDouble(&ok);
95 if (Q_UNLIKELY(!ok)) {
97 qCWarning(C_VALIDATOR).noquote().nospace()
99 <<
"\" into a floating point number";
101 const double size = d->extractDouble(c, params, d->size, &ok);
102 if (Q_UNLIKELY(!ok)) {
104 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison size";
109 QVariantMap{{QStringLiteral(
"val"), val},
110 {QStringLiteral(
"size"), size}});
111 qCDebug(C_VALIDATOR).noquote() <<
debugString(c) << val <<
"!=" << size;
118 case QMetaType::QString:
120 const auto val =
static_cast<qlonglong
>(v.length());
121 const qlonglong size = d->extractLongLong(c, params, d->size, &ok);
122 if (Q_UNLIKELY(!ok)) {
124 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison size";
129 QVariantMap{{QStringLiteral(
"val"), val}, {QStringLiteral(
"size"), size}});
130 qCDebug(C_VALIDATOR).noquote()
131 <<
debugString(c) <<
"string length" << val <<
"!=" << size;
138 qCWarning(C_VALIDATOR).noquote()
139 <<
debugString(c) <<
"The comparison type" << d->type <<
"is not supported";
145 if (d->type != QMetaType::QString) {
146 const QVariant _v = d->valueToNumber(c, v, d->type);
153 result.
value.setValue(v);
169 const QVariantMap map = errorData.toMap();
172 case QMetaType::Short:
174 case QMetaType::Long:
175 case QMetaType::LongLong:
176 case QMetaType::QString:
177 size = c->
locale().toString(map.value(u
"size"_qs).toLongLong());
179 case QMetaType::UShort:
180 case QMetaType::UInt:
181 case QMetaType::ULong:
182 case QMetaType::ULongLong:
183 size = c->
locale().toString(map.value(u
"size"_qs).toULongLong());
185 case QMetaType::Float:
186 case QMetaType::Double:
187 size = c->
locale().toString(map.value(u
"size"_qs).toDouble());
194 const QString _label =
label(c);
196 if (_label.isEmpty()) {
197 if (d->type == QMetaType::QString) {
199 error = c->
translate(
"Cutelyst::ValidatorSize",
200 "The text must be exactly %1 characters long.")
204 error = c->
translate(
"Cutelyst::ValidatorSize",
"The value must be %1.").arg(size);
207 if (d->type == QMetaType::QString) {
210 error = c->
translate(
"Cutelyst::ValidatorSize",
211 "The text in the “%1“ field must be exactly %2 characters long.")
217 c->
translate(
"Cutelyst::ValidatorSize",
"The value in the “%1” field must be %2.")
229 int field = errorData.toInt();
230 const QString _label =
label(c);
234 if (_label.isEmpty()) {
235 error = c->
translate(
"Cutelyst::ValidatorSize",
236 "The comparison type with ID %1 is not supported.")
237 .arg(
static_cast<int>(d->type));
241 "The comparison type with ID %1 for the “%2” field is not supported.")
242 .arg(QString::number(
static_cast<int>(d->type)), _label);
244 }
else if (
field == 1) {
245 if (_label.isEmpty()) {
246 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison value is not valid.");
249 error = c->
translate(
"Cutelyst::ValidatorSize",
250 "The comparison value for the “%1” field is not valid.")
264 const QString _label =
label(c);
265 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
266 if (_label.isEmpty()) {
267 error = c->
translate(
"Cutelyst::ValidatorSize",
268 "Failed to parse the input value into a floating point number.");
271 error = c->
translate(
"Cutelyst::ValidatorSize",
272 "Failed to parse the input value for the “%1” field into a "
273 "floating point number.")
277 if (_label.isEmpty()) {
278 error = c->
translate(
"Cutelyst::ValidatorSize",
279 "Failed to parse the input value into an integer number.");
284 "Cutelyst::ValidatorSize",
285 "Failed to parse the input value for the “%1” field into an integer number.")
QLocale locale() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
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 ¶ms) 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 field under validation must have a size matching the given value.
~ValidatorSize() override
Deconstructs the size validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorSize(const QString &field, QMetaType::Type type, const QVariant &size, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new size validator.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.