cutelyst 4.0.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-2023 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
12ValidatorUrl::ValidatorUrl(const QString &field,
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
22
24{
26
27 Q_D(const ValidatorUrl);
28
29 const QString v = value(params);
30
31 if (!v.isEmpty()) {
32
33 bool valid = true;
34
35 QUrl::ParsingMode parsingMode = QUrl::TolerantMode;
36 if (d->constraints.testFlag(StrictParsing)) {
37 parsingMode = QUrl::StrictMode;
38 }
39
40 QUrl url(v, parsingMode);
41 if (!url.isValid() || url.isEmpty()) {
42 valid = false;
43 }
44
45 if (valid &&
46 (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
47 url.isRelative()) {
48 valid = false;
49 }
50
51 if (valid &&
52 (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
53 url.isLocalFile()) {
54 valid = false;
55 }
56
57 if (valid) {
58 const QStringList schemeList =
59 d->constraints.testFlag(WebsiteOnly)
60 ? QStringList({QStringLiteral("http"), QStringLiteral("https")})
61 : d->schemes;
62
63 // if (d->constraints.testFlag(WebsiteOnly)) {
64 // if (!schemeList.contains(QStringLiteral("http"), Qt::CaseInsensitive))
65 // {
66 // schemeList.append(QStringLiteral("http"));
67 // }
68 // if (!schemeList.contains(QStringLiteral("https"),
69 // Qt::CaseInsensitive)) {
70 // schemeList.append(QStringLiteral("https"));
71 // }
72 // }
73
74 if (!schemeList.empty()) {
75
76 // const QStringList sc = schemeList;
77 bool foundScheme = false;
78 for (const QString &s : schemeList) {
79 const QString sl = s.toLower();
80 if (url.scheme() == sl) {
81 foundScheme = true;
82 break;
83 }
84 }
85
86 if (!foundScheme) {
87 valid = false;
88 }
89 }
90 }
91
92 if (!valid) {
93 result.errorMessage = validationError(c);
94 qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Not a valid URL";
95 } else {
96 result.value.setValue(url);
97 }
98 } else {
99 defaultValue(c, &result);
100 }
101
102 return result;
103}
104
105QString ValidatorUrl::genericValidationError(Context *c, const QVariant &errorData) const
106{
107 QString error;
108 Q_UNUSED(errorData)
109 const QString _label = label(c);
110 if (_label.isEmpty()) {
111 error = c->translate("Cutelyst::ValidatorUrl", "Not a valid URL.");
112 } else {
113 //: %1 will be replaced by the field label
114 error = c->translate("Cutelyst::ValidatorUrl",
115 "The value in the “%1” field is not a valid URL.")
116 .arg(_label);
117 }
118 return error;
119}
The Cutelyst Context.
Definition: context.h:38
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.
void defaultValue(Context *c, ValidatorReturnType *result) 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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
The field under validation must be a valid URL.
Definition: validatorurl.h:33
~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