cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorinteger.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorinteger_p.h"
7
8using namespace Cutelyst;
9
11 QMetaType::Type type,
12 const Cutelyst::ValidatorMessages &messages,
13 const QString &defValKey)
14 : ValidatorRule(*new ValidatorIntegerPrivate(field, type, messages, defValKey))
15{
16}
17
19
21 const ParamsMultiMap &params) const
22{
24
25 const QString v = value(params);
26
27 if (!v.isEmpty()) {
28 Q_D(const ValidatorInteger);
29 QVariant converted;
30
31 switch (d->type) {
32 case QMetaType::Char:
33 case QMetaType::Short:
34 case QMetaType::Int:
35 case QMetaType::Long:
36 case QMetaType::LongLong:
37 case QMetaType::UChar:
38 case QMetaType::UShort:
39 case QMetaType::UInt:
40 case QMetaType::ULong:
41 case QMetaType::ULongLong:
42 converted = d->valueToNumber(c, v, d->type);
43 break;
44 default:
46 qCWarning(C_VALIDATOR).noquote()
47 << debugString(c) << "Conversion type" << d->type << "is not an integer type";
48 break;
49 }
50
51 if (converted.isValid()) {
52 result.value = converted;
53 } else {
54 qCDebug(C_VALIDATOR).noquote().nospace()
55 << debugString(c) << " \"" << v << "\" is not parseable as integer value "
56 << "or exceeds the limits of the selected type " << d->type;
57 result.errorMessage = validationError(c);
58 }
59 } else {
60 defaultValue(c, &result);
61 }
62
63 return result;
64}
65
66QString ValidatorInteger::genericValidationError(Context *c, const QVariant &errorData) const
67{
68 QString error;
69 Q_UNUSED(errorData)
70 Q_D(const ValidatorInteger);
71 const QString _label = label(c);
72 QString min;
73 QString max;
74 switch (d->type) {
75 case QMetaType::Char:
76 min = c->locale().toString(std::numeric_limits<char>::min());
77 max = c->locale().toString(std::numeric_limits<char>::max());
78 break;
79 case QMetaType::Short:
80 min = c->locale().toString(std::numeric_limits<short>::min());
81 max = c->locale().toString(std::numeric_limits<short>::max());
82 break;
83 case QMetaType::Int:
84 min = c->locale().toString(std::numeric_limits<int>::min());
85 max = c->locale().toString(std::numeric_limits<int>::max());
86 break;
87 case QMetaType::Long:
88 min = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::min()));
89 max = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::max()));
90 break;
91 case QMetaType::LongLong:
92 min = c->locale().toString(std::numeric_limits<qlonglong>::min());
93 max = c->locale().toString(std::numeric_limits<qlonglong>::max());
94 break;
95 case QMetaType::UChar:
96 min = c->locale().toString(std::numeric_limits<uchar>::min());
97 max = c->locale().toString(std::numeric_limits<uchar>::max());
98 break;
99 case QMetaType::UShort:
100 min = c->locale().toString(std::numeric_limits<ushort>::min());
101 max = c->locale().toString(std::numeric_limits<ushort>::max());
102 break;
103 case QMetaType::UInt:
104 min = c->locale().toString(std::numeric_limits<uint>::min());
105 max = c->locale().toString(std::numeric_limits<uint>::max());
106 break;
107 case QMetaType::ULong:
108 min = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::min()));
109 max = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::max()));
110 break;
111 case QMetaType::ULongLong:
112 default:
113 min = c->locale().toString(std::numeric_limits<qulonglong>::min());
114 max = c->locale().toString(std::numeric_limits<qulonglong>::max());
115 break;
116 }
117 if (_label.isEmpty()) {
118 //: %1 is the minimum numerical limit for the selected type, %2 is the maximum numeric limit
119 error = c->translate("Cutelyst::ValidatorInteger",
120 "Not a valid integer value between %1 and %2.")
121 .arg(min, max);
122 } else {
123 //: %1 will be replaced by the field name, %2 is the minimum numerical limit for the
124 //: selected type, %3 is the maximum numeric limit
125 error =
126 c->translate("Cutelyst::ValidatorInteger",
127 "The value in the “%1“ field is not a valid integer between %2 and %3.")
128 .arg(_label, min, max);
129 }
130 return error;
131}
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 the value is an integer.
~ValidatorInteger() override
Deconstructs the integer validator.
ValidatorInteger(const QString &field, QMetaType::Type type=QMetaType::ULongLong, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new integer validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
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