6 #include "validatordomain_p.h" 10 #include <QHostAddress> 11 #include <QStringList> 21 :
ValidatorRule(*new ValidatorDomainPrivate(field, options, messages, defValKey))
41 if (!nonAceParts.
empty()) {
48 for (
const QChar &ch : tld) {
49 const char16_t uc = ch.unicode();
50 if (((uc >= ValidatorRulePrivate::ascii_0) &&
51 (uc <= ValidatorRulePrivate::ascii_9)) ||
52 (uc == ValidatorRulePrivate::ascii_dash)) {
61 if (ace.
length() <= ValidatorDomainPrivate::maxDnsNameWithLastDot) {
64 if (parts.
size() > 1) {
66 if (parts.
last().length() > 1) {
67 for (
int i = 0; i < parts.
size(); ++i) {
81 if (part.
length() > ValidatorDomainPrivate::maxDnsLabelLength) {
86 const bool isTld = (i == (parts.
size() - 1));
87 const bool isPunyCode = part.
startsWith(u
"xn--");
88 const qsizetype partEnd = part.
size() - 1;
90 for (
int j = 0; j < part.
size(); ++j) {
93 ((uc >= ValidatorRulePrivate::ascii_0) &&
94 (uc <= ValidatorRulePrivate::ascii_9));
96 (uc == ValidatorRulePrivate::ascii_dash);
99 if (j == 0 && (isDash || isDigit)) {
104 if (j == partEnd && isDash) {
109 ((uc >= ValidatorRulePrivate::ascii_a) &&
110 (uc <= ValidatorRulePrivate::ascii_z));
114 if (!(isDigit || isDash || isChar)) {
120 if (!(isDigit || isDash || isChar)) {
157 if (diag ==
Valid && extractedValue) {
159 *extractedValue = ace + u
'.';
161 *extractedValue = ace;
165 return diag ==
Valid;
171 std::function<
void(
Diagnose diagnose,
const QString &extractedValue)> cb)
182 if (!options.testAnyFlag(
CheckDNS)) {
192 if (dns->hostAddressRecords().empty()) {
196 cb(
Valid, extracted);
203 if (dns2->hostAddressRecords().empty()) {
206 cb(
Valid, extracted);
235 if (dns2->hostAddressRecords().empty()) {
238 cb(
Valid, extracted);
259 return c->
qtTrId(
"cutelyst-valdomain-diag-missingdns");
262 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidchars");
268 return c->
qtTrId(
"cutelyst-valdomain-diag-labeltoolong");
273 return c->
qtTrId(
"cutelyst-valdomain-diag-toolong");
277 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidlabelcount");
281 return c->
qtTrId(
"cutelyst-valdomain-diag-emptylabel");
285 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidtld");
288 return c->
qtTrId(
"cutelyst-valdomain-diag-dashstart");
291 return c->
qtTrId(
"cutelyst-valdomain-diag-dashend");
294 return c->
qtTrId(
"cutelyst-valdomain-diag-digitstart");
297 return c->
qtTrId(
"cutelyst-valdomain-diag-valid");
300 return c->
qtTrId(
"cutelyst-valdomain-diag-dnstimeout");
303 return c->
qtTrId(
"cutelyst-valdomain-diag-dnserror");
305 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
313 return c->
qtTrId(
"cutelyst-valdomain-diag-missingdns-label").
arg(
label);
316 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidchars-label").
arg(
label);
323 return c->
qtTrId(
"cutelyst-valdomain-diag-labeltoolong-label").
arg(
label);
328 return c->
qtTrId(
"cutelyst-valdomain-diag-toolong-label").
arg(
label);
332 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidlabelcount-label").
arg(
label);
337 return c->
qtTrId(
"cutelyst-valdomain-diag-emptylabel-label").
arg(
label);
341 return c->
qtTrId(
"cutelyst-valdomain-diag-invalidtld-label").
arg(
label);
345 return c->
qtTrId(
"cutelyst-valdomain-diag-dashstart-label").
arg(
label);
349 return c->
qtTrId(
"cutelyst-valdomain-diag-dashend-label").
arg(
label);
353 return c->
qtTrId(
"cutelyst-valdomain-diag-digitstart-label").
arg(
label);
356 return c->
qtTrId(
"cutelyst-valdomain-diag-valid-label").
arg(
label);
360 return c->
qtTrId(
"cutelyst-valdomain-diag-dnstimeout-label").
arg(
label);
364 return c->
qtTrId(
"cutelyst-valdomain-diag-dnserror-label");
366 Q_ASSERT_X(
false,
"domain validation diagnose",
"invalid diagnose");
378 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"Can not find valid DNS entry for" << v;
381 qCDebug(C_VALIDATOR).noquote()
382 << valInfo <<
"The domain name contains characters that are not allowed";
385 qCDebug(C_VALIDATOR).noquote()
386 << valInfo <<
"At least on of the domain name labels exceeds the maximum" <<
"size of" 387 << ValidatorDomainPrivate::maxDnsLabelLength <<
"characters";
390 qCDebug(C_VALIDATOR).noquote()
391 << valInfo <<
"The domain name exceeds the maximum size of" 392 << ValidatorDomainPrivate::maxDnsNameWithLastDot <<
"characters";
395 qCDebug(C_VALIDATOR).noquote()
396 << valInfo <<
"Invalid label count. Either no labels or only TLD";
399 qCDebug(C_VALIDATOR).noquote()
400 << valInfo <<
"At least one of the domain name labels is empty";
403 qCDebug(C_VALIDATOR).noquote()
404 << valInfo <<
"The TLD label contains characters that are not allowed";
407 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"At least one label starts with a dash";
410 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"At least one label ends with a dash";
413 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"At least one label starts with a digit";
416 qCDebug(C_VALIDATOR).noquote() << valInfo <<
"The DNS lookup exceeds the timeout of" 417 #if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) 418 << ValidatorDomainPrivate::dnsLookupTimeout;
420 << ValidatorDomainPrivate::dnsLookupTimeout.
count()
424 qCDebug(C_VALIDATOR).noquote()
425 << valInfo <<
"The DNS lookup failed because of errors in the" 444 if (C_VALIDATOR().isDebugEnabled()) {
462 v, d->options, [cb,
this, c, v](
Diagnose diagnose,
const QString &extractedValue) {
463 if (diagnose == Valid) {
464 cb({.errorMessage = {}, .value = extractedValue});
466 if (C_VALIDATOR().isDebugEnabled()) {
467 writeDebugString(debugString(c), diagnose, v);
482 #include "moc_validatordomain.cpp"
Checks if the value of the input field contains a FQDN according to RFC 1035.
ValidatorDomain(const QString &field, Options options=NoOption, const ValidatorMessages &messages={}, const QString &defValKey={})
Constructs a new ValidatorDomain object with the given parameters.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Stores custom error messages and the input field label.
const_reference at(qsizetype i) const const
qsizetype size() const const
static QString diagnoseString(const Context *c, Diagnose diagnose, const QString &label={})
qsizetype size() const const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString chopped(qsizetype len) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toAce(const QString &domain, AceProcessingOptions options)
bool isEmpty() const const
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
Diagnose
Possible diagnose information for the checked domain.
QString toLower() const const
qsizetype count() const const
QString value(const ParamsMultiMap ¶ms) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
QString fromLatin1(QByteArrayView str)
QString validationError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
const QChar at(qsizetype position) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
qsizetype length() const const
~ValidatorDomain() override
Contains the result of a single input parameter validation.
static void validateCb(const QString &value, Options options, std::function< void(Diagnose diagnose, const QString &extractedValue)> cb)
Checks if value is a vaid fully qualified domain name and writes the result to the callback cb...
QString arg(Args &&... args) const const
void setValue(QVariant &&value)
static bool validate(const QString &value, Diagnose *diagnose=nullptr, QString *extractedValue=nullptr)
Returns true if value is a valid fully qualified domain name.