Cutelyst  1.11.0
validatorurl.cpp
1 /*
2  * Copyright (C) 2017 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 QString &label, const QString &customError) :
25  ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, label, customError))
26 {
27 }
28 
29 ValidatorUrl::ValidatorUrl(ValidatorUrlPrivate &dd) :
30  ValidatorRule(dd)
31 {
32 }
33 
35 {
36 }
37 
38 QString ValidatorUrl::validate() const
39 {
40  QString result;
41 
42  Q_D(const ValidatorUrl);
43 
44  const QString v = value();
45 
46  if (!v.isEmpty()) {
47 
48  bool valid = true;
49 
50  QUrl::ParsingMode parsingMode = QUrl::TolerantMode;
51  if (d->constraints.testFlag(StrictParsing)) {
52  parsingMode = QUrl::StrictMode;
53  }
54 
55  QUrl url(value(), parsingMode);
56  if (!url.isValid() || url.isEmpty()) {
57  valid = false;
58  }
59 
60  if (valid && (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) && url.isRelative()) {
61  valid = false;
62  }
63 
64  if (valid && (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) && url.isLocalFile()) {
65  valid = false;
66  }
67 
68  if (valid) {
69  QStringList schemeList = d->schemes;
70 
71  if (d->constraints.testFlag(WebsiteOnly)) {
72  if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive)) {
73  schemeList.append(QStringLiteral("http"));
74  }
75  if (!schemeList.contains(QStringLiteral("https"), Qt::CaseInsensitive)) {
76  schemeList.append(QStringLiteral("https"));
77  }
78  }
79 
80  if (!schemeList.isEmpty()) {
81 
82  const QStringList sc = schemeList;
83  bool foundScheme = false;
84  for (const QString &s : sc) {
85  const QString sl = s.toLower();
86  if (url.scheme() == sl) {
87  foundScheme = true;
88  break;
89  }
90  }
91 
92  if (!foundScheme) {
93  valid = false;
94  }
95  }
96  }
97 
98  if (!valid) {
99  result = validationError();
100  }
101  }
102 
103  return result;
104 }
105 
107 {
108  QString error;
109  if (label().isEmpty()) {
110  error = QStringLiteral("Not a valid URL.");
111  } else {
112  error = QStringLiteral("The value in the “%1” field is not a valid URL.").arg(label());
113  }
114  return error;
115 }
116 
117 void ValidatorUrl::setConstraints(Constraints constraints)
118 {
119  Q_D(ValidatorUrl);
120  d->constraints = constraints;
121 }
122 
123 void ValidatorUrl::setSchemes(const QStringList &schemes)
124 {
125  Q_D(ValidatorUrl);
126  d->schemes = schemes;
127 }
QString genericValidationError() const override
Returns a generic error message.
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes=QStringList(), const QString &label=QString(), const QString &customError=QString())
Constructs a new url validator.
QString value() const
Returns the field value.
void setSchemes(const QStringList &schemes)
Sets an optional list of valid schemes.
The field under validation must be a valid URL.
Definition: validatorurl.h:40
QString validationError() const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validators.
void setConstraints(Constraints constraints)
Sets optional validation contraints.
QString label() const
Returns the human readable field label used for generic error messages.
~ValidatorUrl()
Deconstructs the validator.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...