cutelyst 3.9.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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorurl_p.h"
7
8#include <QUrl>
9
10using namespace Cutelyst;
11
13 Constraints constraints,
14 const QStringList &schemes,
15 const Cutelyst::ValidatorMessages &messages,
16 const QString &defValKey)
17 : ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, messages, defValKey))
18{
19}
20
24
26{
28
29 Q_D(const ValidatorUrl);
30
31 const QString v = value(params);
32
33 if (!v.isEmpty()) {
34
35 bool valid = true;
36
38 if (d->constraints.testFlag(StrictParsing)) {
39 parsingMode = QUrl::StrictMode;
40 }
41
42 QUrl url(v, parsingMode);
43 if (!url.isValid() || url.isEmpty()) {
44 valid = false;
45 }
46
47 if (valid &&
48 (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
49 url.isRelative()) {
50 valid = false;
51 }
52
53 if (valid &&
54 (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
55 url.isLocalFile()) {
56 valid = false;
57 }
58
59 if (valid) {
60 const QStringList schemeList =
61 d->constraints.testFlag(WebsiteOnly)
62 ? QStringList({QStringLiteral("http"), QStringLiteral("https")})
63 : d->schemes;
64
65 // if (d->constraints.testFlag(WebsiteOnly)) {
66 // if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive))
67 // {
68 // schemeList.append(QStringLiteral("http"));
69 // }
70 // if (!schemeList.contains(QStringLiteral("https"),
71 // Qt::CaseInsensitive)) {
72 // schemeList.append(QStringLiteral("https"));
73 // }
74 // }
75
76 if (!schemeList.empty()) {
77
78 // const QStringList sc = schemeList;
79 bool foundScheme = false;
80 for (const QString &s : schemeList) {
81 const QString sl = s.toLower();
82 if (url.scheme() == sl) {
83 foundScheme = true;
84 break;
85 }
86 }
87
88 if (!foundScheme) {
89 valid = false;
90 }
91 }
92 }
93
94 if (!valid) {
95 result.errorMessage = validationError(c);
96 qCDebug(C_VALIDATOR,
97 "ValidatorUrl: Validation failed for field %s at %s::%s: not a valid URL",
98 qPrintable(field()),
99 qPrintable(c->controllerName()),
100 qPrintable(c->actionName()));
101 } else {
102 result.value.setValue(url);
103 }
104 } else {
105 defaultValue(c, &result, "ValidatorUrl");
106 }
107
108 return result;
109}
110
112{
113 QString error;
114 Q_UNUSED(errorData)
115 const QString _label = label(c);
116 if (_label.isEmpty()) {
117 error = c->translate("Cutelyst::ValidatorUrl", "Not a valid URL.");
118 } else {
119 //: %1 will be replaced by the field label
120 error = c->translate("Cutelyst::ValidatorUrl",
121 "The value in the “%1” field is not a valid URL.")
122 .arg(_label);
123 }
124 return error;
125}
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:490
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 ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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.
~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
bool empty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
QString toLower() const const
bool isEmpty() const const
bool isLocalFile() const const
bool isRelative() const const
bool isValid() const const
QString scheme() const const
void setValue(const T &value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.