Cutelyst  3.5.0
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 
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 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Stores custom error messages and the input field label.
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.
~ValidatorUrl() override
Deconstructs the validator.
bool isEmpty() const const
The Cutelyst Context.
Definition: context.h:38
bool empty() const const
bool isEmpty() const const
The field under validation must be a valid URL.
Definition: validatorurl.h:30
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString scheme() const const
QString toLower() const const
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
bool isValid() const const
bool isRelative() const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 ...
bool isLocalFile() const const