cutelyst  4.4.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/Plugins/Utils/validator_export.h>
9 #include <Cutelyst/paramsmultimap.h>
10 
11 #include <QLoggingCategory>
12 #include <QScopedPointer>
13 #include <QVariant>
14 
15 Q_DECLARE_LOGGING_CATEGORY(C_VALIDATOR)
16 
17 namespace Cutelyst {
18 
38 class Context;
39 
49 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorReturnType {
65  explicit operator bool() const noexcept { return errorMessage.isNull(); }
66 
70  [[nodiscard]] bool isValid() const noexcept { return errorMessage.isNull(); }
71 };
72 
142 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
146  ValidatorMessages() = default;
158  ValidatorMessages(const char *customLabel,
159  const char *customValidationError = nullptr,
160  const char *customParsingError = nullptr,
161  const char *customValidationDataError = nullptr)
162  : label(customLabel)
163  , validationError(customValidationError)
164  , parsingError(customParsingError)
165  , validationDataError(customValidationDataError)
166  {
167  }
168  const char *label = nullptr;
169  const char *validationError = nullptr;
170  const char *parsingError = nullptr;
171  const char *validationDataError = nullptr;
172 };
173 
174 class ValidatorRulePrivate;
175 
303 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
304 {
305 public:
315  ValidatorRule(const QString &field,
316  const ValidatorMessages &messages = {},
317  const QString &defValKey = {},
318  QByteArrayView validatorName = nullptr);
319 
323  virtual ~ValidatorRule();
324 
325 protected:
326  // shared d-pointer
327  // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
328  const std::unique_ptr<ValidatorRulePrivate> d_ptr;
333  ValidatorRule(ValidatorRulePrivate &dd);
334 
380  virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
381 
385  [[nodiscard]] QString field() const noexcept;
386 
391  [[nodiscard]] QString label(Context *c) const;
392 
396  [[nodiscard]] QString value(const ParamsMultiMap &params) const;
397 
404  [[nodiscard]] bool trimBefore() const noexcept;
405 
420  [[nodiscard]] QString validationError(Context *c, const QVariant &errorData = {}) const;
421 
452  virtual QString genericValidationError(Context *c, const QVariant &errorData = {}) const;
453 
468  [[nodiscard]] QString parsingError(Context *c, const QVariant &errorData = {}) const;
469 
500  virtual QString genericParsingError(Context *c, const QVariant &errorData = {}) const;
501 
516  [[nodiscard]] QString validationDataError(Context *c, const QVariant &errorData = {}) const;
517 
547  virtual QString genericValidationDataError(Context *c, const QVariant &errorData = {}) const;
548 
553  void defaultValue(Context *c, ValidatorReturnType *result) const;
554 
561  [[nodiscard]] QString debugString(Context *c) const;
562 
563 private:
564  Q_DECLARE_PRIVATE(ValidatorRule) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
565  Q_DISABLE_COPY(ValidatorRule)
566 
567 
571  void setTranslationContext(const char *trContext) noexcept;
572 
583  void setTrimBefore(bool trimBefore) noexcept;
584 
585  friend class Validator;
586  friend class ValidatorPrivate;
587 };
588 
589 } // namespace Cutelyst
590 
591 #endif // CUTELYSTVALIDATORRULE_H
The Cutelyst Context.
Definition: context.h:42
Base class for all validator rules.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const =0
The Cutelyst namespace holds all public Cutelyst API.
bool isNull() const const
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.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
bool isValid() const noexcept
Definition: validatorrule.h:70