cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorurl.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorurl_p.h"
7 
8 #include <QUrl>
9 
10 using namespace Cutelyst;
11 using namespace Qt::StringLiterals;
12 
14  Constraints constraints,
15  const QStringList &schemes,
16  const Cutelyst::ValidatorMessages &messages,
17  const QString &defValKey)
18  : ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, messages, defValKey))
19 {
20 }
21 
22 ValidatorUrl::~ValidatorUrl() = default;
23 
25 {
26  ValidatorReturnType result;
27 
28  Q_D(const ValidatorUrl);
29 
30  const QString v = value(params);
31 
32  if (!v.isEmpty()) {
33 
34  bool valid = true;
35 
36  QUrl url(v, d->constraints.testFlag(StrictParsing) ? QUrl::StrictMode : QUrl::TolerantMode);
37  if (!url.isValid() || url.isEmpty()) {
38  valid = false;
39  }
40 
41  if (valid &&
42  (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
43  url.isRelative()) {
44  valid = false;
45  }
46 
47  if (valid &&
48  (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
49  url.isLocalFile()) {
50  valid = false;
51  }
52 
53  if (valid) {
54  const QStringList schemeList = d->constraints.testFlag(WebsiteOnly)
55  ? QStringList({u"http"_s, u"https"_s})
56  : d->schemes;
57 
58  if (!schemeList.empty()) {
59 
60  bool foundScheme = false;
61  for (const QString &s : schemeList) {
62  const QString sl = s.toLower();
63  if (url.scheme() == sl) {
64  foundScheme = true;
65  break;
66  }
67  }
68 
69  valid = foundScheme;
70  }
71  }
72 
73  if (!valid) {
74  result.errorMessage = validationError(c);
75  qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Not a valid URL";
76  } else {
77  result.value.setValue(url);
78  }
79  } else {
80  defaultValue(c, &result);
81  }
82 
83  return result;
84 }
85 
87 {
88  cb(validate(c, params));
89 }
90 
92 {
93  Q_UNUSED(errorData)
94  const QString _label = label(c);
95  if (_label.isEmpty()) {
96  //% "Not a valid URL."
97  return c->qtTrId("cutelyst-valurl-genvalerr");
98  } else {
99  //: %1 will be replaced by the field label
100  //% "The text in the “%1” field is not a valid URL."
101  return c->qtTrId("cutelyst-valurl-genvalerr-label").arg(_label);
102  }
103 }
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Stores custom error messages and the input field label.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes={}, const ValidatorMessages &messages={}, const QString &defValKey={})
The Cutelyst Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
bool empty() const const
bool isEmpty() const const
The field under validation must be a valid URL.
Definition: validatorurl.h:33
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
QString toLower() const const
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString validationError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString arg(Args &&... args) const const
void setValue(QVariant &&value)