6 #include "validatorip_p.h" 7 #include <QHostAddress> 8 #include <QRegularExpression> 14 ValidatorRule(*new ValidatorIpPrivate(field, constraints, messages, defValKey))
36 qCDebug(C_VALIDATOR,
"ValidatorIp: Validation failed for field %s at %s::%s: not a valid IP address within the constraints.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
58 static const std::vector<std::pair<QHostAddress,int>> ipv4Private({
78 static const std::vector<std::pair<QHostAddress,int>> ipv4Reserved({
126 static const std::vector<std::pair<QHostAddress,int>> ipv6Private({
135 static const std::vector<std::pair<QHostAddress,int>> ipv6Reserved({
171 if (a.setAddress(
value)) {
175 if (a.protocol() == QAbstractSocket::IPv4Protocol) {
177 if (constraints.testFlag(
IPv6Only)) {
183 for (
const std::pair<QHostAddress,int> &subnet : ipv4Private) {
184 if (a.isInSubnet(subnet.first, subnet.second)) {
193 for (
const std::pair<QHostAddress,int> &subnet : ipv4Reserved) {
194 if (a.isInSubnet(subnet.first, subnet.second)) {
202 if (a.isInSubnet(
QHostAddress(QStringLiteral(
"224.0.0.0")), 4)) {
209 if (constraints.testFlag(
IPv4Only)) {
215 for (
const std::pair<QHostAddress,int> &subnet : ipv6Private) {
216 if (a.isInSubnet(subnet.first, subnet.second)) {
225 for (
const std::pair<QHostAddress,int> &subnet : ipv6Reserved) {
226 if (a.isInSubnet(subnet.first, subnet.second)) {
234 if (a.isInSubnet(
QHostAddress(QStringLiteral(
"ff00::")), 8)) {
255 error = c->
translate(
"Cutelyst::ValidatorIp",
"IP address is invalid or not acceptable.");
258 error = c->
translate(
"Cutelyst::ValidatorIp",
"The IP address in the “%1” field is invalid or not acceptable.").
arg(_label);
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
~ValidatorIp() override
Deconstructs the ip validator.
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
ValidatorIp(const QString &field, Constraints constraints=NoConstraint, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ip validator.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
void setValue(const T &value)
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
static bool validate(const QString &value, Constraints constraints=NoConstraint)
Returns true if value is a valid IP address within the constraints.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Checks if the field value is a valid IP address.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.