cutelyst  5.0.1
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-2025 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/Plugins/Utils/validator_export.h>
9 #include <Cutelyst/context.h>
10 #include <Cutelyst/paramsmultimap.h>
11 #include <functional>
12 
13 #include <QLoggingCategory>
14 #include <QPointer>
15 #include <QScopedPointer>
16 #include <QVariant>
17 
18 Q_DECLARE_LOGGING_CATEGORY(C_VALIDATOR)
19 
20 namespace Cutelyst {
21 
41 class Context;
42 
52 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorReturnType {
68  explicit operator bool() const noexcept { return errorMessage.isNull(); }
69 
73  [[nodiscard]] bool isValid() const noexcept { return errorMessage.isNull(); }
74 };
75 
82 using ValidatorRtFn = std::function<void(ValidatorReturnType &&result)>;
83 
153 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
157  ValidatorMessages() = default;
169  explicit ValidatorMessages(const char *customLabel,
170  const char *customValidationError = nullptr,
171  const char *customParsingError = nullptr,
172  const char *customValidationDataError = nullptr)
173  : label(customLabel)
174  , validationError(customValidationError)
175  , parsingError(customParsingError)
176  , validationDataError(customValidationDataError)
177  {
178  }
179  const char *label = nullptr;
180  const char *validationError = nullptr;
181  const char *parsingError = nullptr;
182  const char *validationDataError = nullptr;
183 };
184 
185 class ValidatorRulePrivate;
186 
353 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
354 {
355 public:
365  explicit ValidatorRule(const QString &field,
366  const ValidatorMessages &messages = {},
367  const QString &defValKey = {},
368  QByteArrayView validatorName = nullptr);
369 
373  virtual ~ValidatorRule();
374 
375 protected:
376  // shared d-pointer
377  // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
378  const std::unique_ptr<ValidatorRulePrivate> d_ptr;
383  explicit ValidatorRule(ValidatorRulePrivate &dd);
384 
430  virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
431 
476  virtual void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const;
477 
481  [[nodiscard]] QString field() const noexcept;
482 
487  [[nodiscard]] QString label(const Context *c) const;
488 
492  [[nodiscard]] QString value(const ParamsMultiMap &params) const;
493 
500  [[nodiscard]] bool trimBefore() const noexcept;
501 
516  [[nodiscard]] QString validationError(Context *c, const QVariant &errorData = {}) const;
517 
548  virtual QString genericValidationError(Context *c, const QVariant &errorData = {}) const;
549 
564  [[nodiscard]] QString parsingError(Context *c, const QVariant &errorData = {}) const;
565 
596  virtual QString genericParsingError(Context *c, const QVariant &errorData = {}) const;
597 
612  [[nodiscard]] QString validationDataError(Context *c, const QVariant &errorData = {}) const;
613 
643  virtual QString genericValidationDataError(Context *c, const QVariant &errorData = {}) const;
644 
649  void defaultValue(Context *c, ValidatorReturnType *result) const;
650 
658  void defaultValue(Context *c, ValidatorRtFn cb) const;
659 
666  [[nodiscard]] QString debugString(const Context *c) const;
667 
668 private:
669  Q_DECLARE_PRIVATE(ValidatorRule) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
670  Q_DISABLE_COPY(ValidatorRule)
671 
672 
676  void setTranslationContext(const char *trContext) noexcept;
677 
688  void setTrimBefore(bool trimBefore) noexcept;
689 
690  friend class Validator;
691  friend class ValidatorPrivate;
692  friend class AsyncValidator;
693 };
694 
695 } // namespace Cutelyst
696 
697 #endif // CUTELYSTVALIDATORRULE_H
Stores custom error messages and the input field label.
bool isNull() const const
The Cutelyst Context.
Definition: context.h:42
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
Validation processor for input data.
Definition: validator.h:308
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
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.
bool isValid() const noexcept
Definition: validatorrule.h:73