cutelyst 5.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-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;
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
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 url(v, d->constraints.testFlag(StrictParsing) ? QUrl::StrictMode : QUrl::TolerantMode);
36 if (!url.isValid() || url.isEmpty()) {
37 valid = false;
38 }
39
40 if (valid &&
41 (d->constraints.testFlag(NoRelative) || d->constraints.testFlag(WebsiteOnly)) &&
42 url.isRelative()) {
43 valid = false;
44 }
45
46 if (valid &&
47 (d->constraints.testFlag(NoLocalFile) || d->constraints.testFlag(WebsiteOnly)) &&
48 url.isLocalFile()) {
49 valid = false;
50 }
51
52 if (valid) {
53 const QStringList schemeList =
54 d->constraints.testFlag(WebsiteOnly)
55 ? QStringList({QStringLiteral("http"), QStringLiteral("https")})
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
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
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
The field under validation must be a valid URL.
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={})
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
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.