6#include "validatorip_p.h"
10#include <QHostAddress>
11#include <QRegularExpression>
16 Constraints constraints,
42 "ValidatorIp: Validation failed for field %s at %s::%s: not a valid IP address "
43 "within the constraints.",
45 qPrintable(c->controllerName()),
46 qPrintable(c->actionName()));
63 "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$")))) {
70 static const std::vector<std::pair<QHostAddress, int>> ipv4Private(
89 static const std::vector<std::pair<QHostAddress, int>> ipv4Reserved(
132 {
QHostAddress(QStringLiteral(
"255.255.255.255")), 32}});
135 static const std::vector<std::pair<QHostAddress, int>> ipv6Private(
143 static const std::vector<std::pair<QHostAddress, int>> ipv6Reserved(
178 if (a.setAddress(
value)) {
184 if (constraints.testFlag(
IPv6Only)) {
191 for (
const std::pair<QHostAddress, int> &subnet : ipv4Private) {
192 if (a.isInSubnet(subnet.first, subnet.second)) {
202 for (
const std::pair<QHostAddress, int> &subnet : ipv4Reserved) {
203 if (a.isInSubnet(subnet.first, subnet.second)) {
212 if (a.isInSubnet(
QHostAddress(QStringLiteral(
"224.0.0.0")), 4)) {
219 if (constraints.testFlag(
IPv4Only)) {
226 for (
const std::pair<QHostAddress, int> &subnet : ipv6Private) {
227 if (a.isInSubnet(subnet.first, subnet.second)) {
237 for (
const std::pair<QHostAddress, int> &subnet : ipv6Reserved) {
238 if (a.isInSubnet(subnet.first, subnet.second)) {
247 if (a.isInSubnet(
QHostAddress(QStringLiteral(
"ff00::")), 8)) {
268 error = c->
translate(
"Cutelyst::ValidatorIp",
"IP address is invalid or not acceptable.");
271 error = c->
translate(
"Cutelyst::ValidatorIp",
272 "The IP address in the “%1” field is invalid or not acceptable.")
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorIp(const QString &field, Constraints constraints=NoConstraint, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ip validator.
~ValidatorIp() override
Deconstructs the ip validator.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
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 ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
static bool validate(const QString &value, Constraints constraints=NoConstraint)
Returns true if value is a valid IP address within the constraints.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
void setValue(const T &value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.