cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrule.h
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef CUTELYSTVALIDATORRULE_H
6#define CUTELYSTVALIDATORRULE_H
7
8#include <Cutelyst/cutelyst_global.h>
9#include <Cutelyst/paramsmultimap.h>
10
11#include <QLoggingCategory>
12#include <QScopedPointer>
13#include <QVariant>
14
15Q_DECLARE_LOGGING_CATEGORY(C_VALIDATOR)
16
17namespace Cutelyst {
18
38class Context;
39
49struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorReturnType {
50 QString errorMessage;
54 QVariant value;
58 QVariant extra;
67 explicit operator bool() const noexcept { return errorMessage.isNull(); }
68
74 [[nodiscard]] bool isValid() const noexcept { return errorMessage.isNull(); }
75};
76
141struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
145 ValidatorMessages() = default;
156 ValidatorMessages(const char *customLabel,
157 const char *customValidationError = nullptr,
158 const char *customParsingError = nullptr,
159 const char *customValidationDataError = nullptr)
160 : label(customLabel)
161 , validationError(customValidationError)
162 , parsingError(customParsingError)
163 , validationDataError(customValidationDataError)
164 {
165 }
166 const char *label = nullptr;
167 const char *validationError = nullptr;
168 const char *parsingError = nullptr;
169 const char *validationDataError = nullptr;
170};
171
172class ValidatorRulePrivate;
173
300class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
301{
302public:
311 ValidatorRule(const QString &field,
312 const ValidatorMessages &messages = ValidatorMessages(),
313 const QString &defValKey = QString(),
314 QByteArrayView validatorName = nullptr);
315
319 virtual ~ValidatorRule();
320
321protected:
322 // shared d-pointer
323 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
324 const std::unique_ptr<ValidatorRulePrivate> d_ptr;
329 ValidatorRule(ValidatorRulePrivate &dd);
330
375 virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
376
381 [[nodiscard]] QString field() const noexcept;
382
388 [[nodiscard]] QString label(Context *c) const;
389
393 [[nodiscard]] QString value(const ParamsMultiMap &params) const;
394
401 [[nodiscard]] bool trimBefore() const noexcept;
402
417 [[nodiscard]] QString validationError(Context *c, const QVariant &errorData = QVariant()) const;
418
449 virtual QString genericValidationError(Context *c,
450 const QVariant &errorData = QVariant()) const;
451
466 [[nodiscard]] QString parsingError(Context *c, const QVariant &errorData = QVariant()) const;
467
498 virtual QString genericParsingError(Context *c, const QVariant &errorData = QVariant()) const;
499
514 [[nodiscard]] QString validationDataError(Context *c,
515 const QVariant &errorData = QVariant()) const;
516
547 virtual QString genericValidationDataError(Context *c,
548 const QVariant &errorData = QVariant()) const;
549
556 void defaultValue(Context *c, ValidatorReturnType *result) const;
557
564 [[nodiscard]] QString debugString(Context *c) const;
565
566private:
567 Q_DECLARE_PRIVATE(ValidatorRule) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
568 Q_DISABLE_COPY(ValidatorRule)
569
575 void setTranslationContext(QLatin1String trContext) noexcept;
576
587 void setTrimBefore(bool trimBefore) noexcept;
588
589 friend class Validator;
590 friend class ValidatorPrivate;
591};
592
593} // namespace Cutelyst
594
595#endif // CUTELYSTVALIDATORRULE_H
The Cutelyst Context.
Definition: context.h:38
Base class for all validator rules.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const =0
Starts the validation and returns the result.
Validation processor for input data.
Definition: validator.h:283
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.
ValidatorMessages(const char *customLabel, const char *customValidationError=nullptr, const char *customParsingError=nullptr, const char *customValidationDataError=nullptr)
Constructs a new ValidatorMessages object with the given parameters.
ValidatorMessages()=default
Constructs a default ValidatorMessages object with all custom messages disabled.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
bool isValid() const noexcept
Returns true if validation succeeded.
Definition: validatorrule.h:74