6 #include "validatordomain_p.h"
16 ValidatorRule(* new ValidatorDomainPrivate(field, checkDNS, messages, defValKey))
31 bool hasRootDot =
false;
32 if (_v.endsWith(u
'.')) {
38 const QString v = QString::fromLatin1(QUrl::toAce(_v)).toLower();
41 const QStringList nonAceParts = _v.split(QLatin1Char(
'.'));
42 if (!nonAceParts.empty()) {
43 const QString tld = nonAceParts.last();
49 for (
const QChar &ch : tld) {
50 const ushort &uc = ch.unicode();
51 if (((uc > 47) && (uc < 58)) || (uc == 45)) {
61 if (v.length() < 254) {
62 const QStringList parts = v.split(QLatin1Char(
'.'), Qt::KeepEmptyParts);
64 if (parts.size() > 1) {
66 if (parts.last().length() > 1) {
67 for (
int i = 0; i < parts.size(); ++i) {
69 const QString part = parts.at(i);
70 if (!part.isEmpty()) {
72 if (part.length() < 64) {
73 bool isTld = (i == (parts.size() -1));
74 bool isPunyCode = part.startsWith(u
"xn--");
75 for (
int j = 0; j < part.size(); ++j) {
76 const ushort &uc = part.at(j).unicode();
77 const bool isDigit = ((uc > 47) && (uc < 58));
78 const bool isDash = (uc == 45);
80 if ((j == 0) && (isDash || isDigit)) {
86 if ((j == (part.size() - 1)) && isDash) {
91 const bool isChar = ((uc > 96) && (uc < 123));
94 if (!(isDigit || isDash || isChar)) {
101 if (!(isDigit || isDash || isChar)) {
156 if (valid && checkDNS) {
157 QDnsLookup alookup(QDnsLookup::A, v);
159 QObject::connect(&alookup, &QDnsLookup::finished, &aloop, &QEventLoop::quit);
160 QTimer::singleShot(3100, &alookup, &QDnsLookup::abort);
164 if (((alookup.error() != QDnsLookup::NoError) && (alookup.error() != QDnsLookup::OperationCancelledError)) || alookup.hostAddressRecords().empty()) {
165 QDnsLookup aaaaLookup(QDnsLookup::AAAA, v);
167 QObject::connect(&aaaaLookup, &QDnsLookup::finished, &aaaaLoop, &QEventLoop::quit);
168 QTimer::singleShot(3100, &aaaaLookup, &QDnsLookup::abort);
172 if (((aaaaLookup.error() != QDnsLookup::NoError) && (aaaaLookup.error() != QDnsLookup::OperationCancelledError)) || aaaaLookup.hostAddressRecords().empty()) {
175 }
else if (aaaaLookup.error() == QDnsLookup::OperationCancelledError) {
179 }
else if (alookup.error() == QDnsLookup::OperationCancelledError) {
189 if (valid && extractedValue) {
191 *extractedValue = v + QLatin1Char(
'.');
204 if (
label.isEmpty()) {
207 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name seems to be valid but could not be found in the domain name system.");
210 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name contains characters that are not allowed.");
213 error = c->
translate(
"Cutelyst::ValidatorDomain",
"At least one of the sections separated by dots exceeds the maximum allowed length of 63 characters. Note that internationalized domain names can be longer internally than they are displayed.");
216 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The full name of the domain must not be longer than 253 characters. Note that internationalized domain names can be longer internally than they are displayed.");
219 error = c->
translate(
"Cutelyst::ValidatorDomain",
"This is not a valid domain name because it has either no parts (is empty) or only has a top level domain.");
222 error = c->
translate(
"Cutelyst::ValidatorDomain",
"At least one of the sections separated by dots is empty. Check whether you have entered two dots consecutively.");
225 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The top level domain (last part) contains characters that are not allowed, like digits and/or dashes.");
228 error = c->
translate(
"Cutelyst::ValidatorDomain",
"Domain name sections are not allowed to start with a dash.");
231 error = c->
translate(
"Cutelyst::ValidatorDomain",
"Domain name sections are not allowed to end with a dash.");
234 error = c->
translate(
"Cutelyst::ValidatorDomain",
"Domain name sections are not allowed to start with a digit.");
237 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name is valid.");
240 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The DNS lookup was aborted because it took too long.");
243 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
249 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field seems to be valid but could not be found in the domain name system.").arg(
label);
252 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field contains characters that are not allowed.").arg(
label);
255 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because at least one of the sections separated by dots exceeds the maximum allowed length of 63 characters. Note that internationalized domain names can be longer internally than they are displayed.").arg(
label);
258 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The full name of the domain in the “%1” field must not be longer than 253 characters. Note that internationalized domain names can be longer internally than they are displayed.").arg(
label);
261 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The “%1” field does not contain a valid domain name because it has either no parts (is empty) or only has a top level domain.").arg(
label);
264 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because at least one of the sections separated by dots is empty. Check whether you have entered two dots consecutively.").arg(
label);
267 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The top level domain (last part) of the domain name in the “%1” field contains characters that are not allowed, like digits and or dashes.").arg(
label);
270 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because domain name sections are not allowed to start with a dash.").arg(
label);
273 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because domain name sections are not allowed to end with a dash.").arg(
label);
276 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1“ field is not valid because domain name sections are not allowed to start with a digit.").arg(
label);
279 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The domain name in the “%1” field is valid.").arg(
label);
282 error = c->
translate(
"Cutelyst::ValidatorDomain",
"The DNS lookup for the domain name in the “%1” field was aborted because it took too long.").arg(
label);
285 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
297 const QString &v =
value(params);
304 result.
value.setValue(exVal);
318 const QString _label =
label(c);
324 #include "moc_validatordomain.cpp"
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Checks if the value of the input field contains FQDN according to RFC 1035.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
static QString diagnoseString(Context *c, Diagnose diagnose, const QString &label=QString())
Returns a human readable description of a Diagnose.
ValidatorDomain(const QString &field, bool checkDNS=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorDomain with the given parameters.
~ValidatorDomain() override
Deconstructs ValidatorDomain.
Diagnose
Possible diagnose information for the checked domain.
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 char *validatorName) 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 validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
static bool validate(const QString &value, bool checkDNS, Diagnose *diagnose=nullptr, QString *extractedValue=nullptr)
Returns true if value is a valid domain name.
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.