cutelyst 5.0.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-2025 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;
11using namespace Qt::StringLiterals;
12
14 Constraints constraints,
15 const QStringList &schemes,
16 const Cutelyst::ValidatorMessages &messages,
17 const QString &defValKey)
18 : ValidatorRule(*new ValidatorUrlPrivate(field, constraints, schemes, messages, defValKey))
19{
20}
21
23
25{
27
28 Q_D(const ValidatorUrl);
29
30 const QString v = value(params);
31
32 if (!v.isEmpty()) {
33
34 bool valid = true;
35
36 QUrl url(v, d->constraints.testFlag(StrictParsing) ? QUrl::StrictMode : QUrl::TolerantMode);
37 if (!url.isValid() || url.isEmpty()) {
38 valid = false;
39 }
40
41 if (valid &&
42 (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
43 url.isRelative()) {
44 valid = false;
45 }
46
47 if (valid &&
48 (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
49 url.isLocalFile()) {
50 valid = false;
51 }
52
53 if (valid) {
54 const QStringList schemeList = d->constraints.testFlag(WebsiteOnly)
55 ? QStringList({u"http"_s, u"https"_s})
56 : d->schemes;
57
58 if (!schemeList.empty()) {
59
60 bool foundScheme = false;
61 for (const QString &s : schemeList) {
62 const QString sl = s.toLower();
63 if (url.scheme() == sl) {
64 foundScheme = true;
65 break;
66 }
67 }
68
69 valid = foundScheme;
70 }
71 }
72
73 if (!valid) {
74 result.errorMessage = validationError(c);
75 qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Not a valid URL";
76 } else {
77 result.value.setValue(url);
78 }
79 } else {
80 defaultValue(c, &result);
81 }
82
83 return result;
84}
85
87{
88 cb(validate(c, params));
89}
90
92{
93 Q_UNUSED(errorData)
94 const QString _label = label(c);
95 if (_label.isEmpty()) {
96 //% "Not a valid URL."
97 return c->qtTrId("cutelyst-valurl-genvalerr");
98 } else {
99 //: %1 will be replaced by the field label
100 //% "The text in the “%1” field is not a valid URL."
101 return c->qtTrId("cutelyst-valurl-genvalerr-label").arg(_label);
102 }
103}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
QString field() const noexcept
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
ValidatorRule(const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr)
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
ValidatorUrl(const QString &field, Constraints constraints=NoConstraint, const QStringList &schemes={}, const ValidatorMessages &messages={}, const QString &defValKey={})
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.
bool empty() const const
QString arg(Args &&... args) 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(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.