Cutelyst  2.13.0
validatorurl.cpp
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatorurl_p.h"
20 #include <QUrl>
21 
22 using namespace Cutelyst;
23 
24 ValidatorUrl::ValidatorUrl(const QString &field, Constraints constraints, const QStringList &schemes, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
25  ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, messages, defValKey))
26 {
27 }
28 
30 {
31 }
32 
34 {
35  ValidatorReturnType result;
36 
37  Q_D(const ValidatorUrl);
38 
39  const QString v = value(params);
40 
41  if (!v.isEmpty()) {
42 
43  bool valid = true;
44 
45  QUrl::ParsingMode parsingMode = QUrl::TolerantMode;
46  if (d->constraints.testFlag(StrictParsing)) {
47  parsingMode = QUrl::StrictMode;
48  }
49 
50  QUrl url(v, parsingMode);
51  if (!url.isValid() || url.isEmpty()) {
52  valid = false;
53  }
54 
55  if (valid && (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) && url.isRelative()) {
56  valid = false;
57  }
58 
59  if (valid && (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) && url.isLocalFile()) {
60  valid = false;
61  }
62 
63  if (valid) {
64  const QStringList schemeList = d->constraints.testFlag(WebsiteOnly) ? QStringList({QStringLiteral("http"), QStringLiteral("https")}) : d->schemes;
65 
66 // if (d->constraints.testFlag(WebsiteOnly)) {
67 // if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive)) {
68 // schemeList.append(QStringLiteral("http"));
69 // }
70 // if (!schemeList.contains(QStringLiteral("https"), Qt::CaseInsensitive)) {
71 // schemeList.append(QStringLiteral("https"));
72 // }
73 // }
74 
75  if (!schemeList.empty()) {
76 
77 // const QStringList sc = schemeList;
78  bool foundScheme = false;
79  for (const QString &s : schemeList) {
80  const QString sl = s.toLower();
81  if (url.scheme() == sl) {
82  foundScheme = true;
83  break;
84  }
85  }
86 
87  if (!foundScheme) {
88  valid = false;
89  }
90  }
91  }
92 
93  if (!valid) {
94  result.errorMessage = validationError(c);
95  qCDebug(C_VALIDATOR, "ValidatorUrl: Validation failed for field %s at %s::%s: not a valid URL", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
96  } else {
97  result.value.setValue<QUrl>(url);
98  }
99  } else {
100  defaultValue(c, &result, "ValidatorUrl");
101  }
102 
103  return result;
104 }
105 
106 QString ValidatorUrl::genericValidationError(Context *c, const QVariant &errorData) const
107 {
108  QString error;
109  Q_UNUSED(errorData)
110  const QString _label = label(c);
111  if (_label.isEmpty()) {
112  error = c->translate("Cutelyst::ValidatorUrl", "Not a valid URL.");
113  } else {
114  //: %1 will be replaced by the field label
115  error = c->translate("Cutelyst::ValidatorUrl", "The value in the “%1” field is not a valid URL.").arg(_label);
116  }
117  return error;
118 }
Cutelyst::ValidatorUrl::ValidatorUrl
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes=QStringList(), const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new url validator.
Definition: validatorurl.cpp:24
Cutelyst::ParamsMultiMap
QMap< QString, QString > ParamsMultiMap
Definition: paramsmultimap.h:36
Cutelyst::ValidatorUrl::WebsiteOnly
@ WebsiteOnly
Definition: validatorurl.h:54
Cutelyst::ValidatorUrl::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Definition: validatorurl.cpp:106
Cutelyst::ValidatorMessages
Stores custom error messages and the input field label.
Definition: validatorrule.h:144
Cutelyst::ValidatorRule::value
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
Definition: validatorrule.cpp:41
Cutelyst::ValidatorRule::label
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Definition: validatorrule.cpp:58
Cutelyst::ValidatorRule::validationError
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Definition: validatorrule.cpp:72
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::ValidatorRule
Base class for all validator rules.
Definition: validatorrule.h:292
Cutelyst::ValidatorUrl::validate
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Definition: validatorurl.cpp:33
Cutelyst::ValidatorUrl::NoLocalFile
@ NoLocalFile
Definition: validatorurl.h:53
Cutelyst::ValidatorRule::field
QString field() const
Returns the name of the field to validate.
Definition: validatorrule.cpp:39
Cutelyst::Context::translate
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:473
Cutelyst::ValidatorReturnType
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
Cutelyst::ValidatorUrl::NoRelative
@ NoRelative
Definition: validatorurl.h:52
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::ValidatorReturnType::value
QVariant value
Definition: validatorrule.h:64
Cutelyst::ValidatorUrl::StrictParsing
@ StrictParsing
Definition: validatorurl.h:51
Cutelyst::ValidatorUrl
The field under validation must be a valid URL.
Definition: validatorurl.h:43
Cutelyst::ValidatorReturnType::errorMessage
QString errorMessage
Definition: validatorrule.h:63
Cutelyst::ValidatorUrl::~ValidatorUrl
~ValidatorUrl() override
Deconstructs the validator.
Definition: validatorurl.cpp:29
Cutelyst::ValidatorRule::defaultValue
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 ...
Definition: validatorrule.cpp:162