cutelyst  3.7.0
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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorurl_p.h"
7 #include <QUrl>
8 
9 using namespace Cutelyst;
10 
11 ValidatorUrl::ValidatorUrl(const QString &field, Constraints constraints, const QStringList &schemes, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
12  ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, messages, defValKey))
13 {
14 }
15 
17 {
18 }
19 
21 {
22  ValidatorReturnType result;
23 
24  Q_D(const ValidatorUrl);
25 
26  const QString v = value(params);
27 
28  if (!v.isEmpty()) {
29 
30  bool valid = true;
31 
32  QUrl::ParsingMode parsingMode = QUrl::TolerantMode;
33  if (d->constraints.testFlag(StrictParsing)) {
34  parsingMode = QUrl::StrictMode;
35  }
36 
37  QUrl url(v, parsingMode);
38  if (!url.isValid() || url.isEmpty()) {
39  valid = false;
40  }
41 
42  if (valid && (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) && url.isRelative()) {
43  valid = false;
44  }
45 
46  if (valid && (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) && url.isLocalFile()) {
47  valid = false;
48  }
49 
50  if (valid) {
51  const QStringList schemeList = d->constraints.testFlag(WebsiteOnly) ? QStringList({QStringLiteral("http"), QStringLiteral("https")}) : d->schemes;
52 
53 // if (d->constraints.testFlag(WebsiteOnly)) {
54 // if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive)) {
55 // schemeList.append(QStringLiteral("http"));
56 // }
57 // if (!schemeList.contains(QStringLiteral("https"), Qt::CaseInsensitive)) {
58 // schemeList.append(QStringLiteral("https"));
59 // }
60 // }
61 
62  if (!schemeList.empty()) {
63 
64 // const QStringList sc = schemeList;
65  bool foundScheme = false;
66  for (const QString &s : schemeList) {
67  const QString sl = s.toLower();
68  if (url.scheme() == sl) {
69  foundScheme = true;
70  break;
71  }
72  }
73 
74  if (!foundScheme) {
75  valid = false;
76  }
77  }
78  }
79 
80  if (!valid) {
81  result.errorMessage = validationError(c);
82  qCDebug(C_VALIDATOR, "ValidatorUrl: Validation failed for field %s at %s::%s: not a valid URL", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
83  } else {
84  result.value.setValue(url);
85  }
86  } else {
87  defaultValue(c, &result, "ValidatorUrl");
88  }
89 
90  return result;
91 }
92 
93 QString ValidatorUrl::genericValidationError(Context *c, const QVariant &errorData) const
94 {
95  QString error;
96  Q_UNUSED(errorData)
97  const QString _label = label(c);
98  if (_label.isEmpty()) {
99  error = c->translate("Cutelyst::ValidatorUrl", "Not a valid URL.");
100  } else {
101  //: %1 will be replaced by the field label
102  error = c->translate("Cutelyst::ValidatorUrl", "The value in the “%1” field is not a valid URL.").arg(_label);
103  }
104  return error;
105 }
The Cutelyst Context.
Definition: context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Base class for all validator rules.
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 ...
QString value(const ParamsMultiMap &params) 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.
The field under validation must be a valid URL.
Definition: validatorurl.h:31
~ValidatorUrl() override
Deconstructs the validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes=QStringList(), const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new url validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49