cutelyst  3.9.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-2022 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 
15 Q_DECLARE_LOGGING_CATEGORY(C_VALIDATOR)
16 
17 namespace Cutelyst {
18 
38 class Context;
39 
49 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorReturnType {
67  explicit operator bool() const { return errorMessage.isNull(); }
68 
74  bool isValid() const { return errorMessage.isNull(); }
75 };
76 
141 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
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 
172 class ValidatorRulePrivate;
173 
300 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
301 {
302 public:
310  ValidatorRule(const QString &field,
311  const ValidatorMessages &messages = ValidatorMessages(),
312  const QString &defValKey = QString());
313 
317  virtual ~ValidatorRule();
318 
319 protected:
325  ValidatorRule(ValidatorRulePrivate &dd);
326 
371  virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
372 
377  QString field() const;
378 
384  QString label(Context *c) const;
385 
389  QString value(const ParamsMultiMap &params) const;
390 
397  bool trimBefore() const;
398 
413  QString validationError(Context *c, const QVariant &errorData = QVariant()) const;
414 
445  virtual QString genericValidationError(Context *c,
446  const QVariant &errorData = QVariant()) const;
447 
462  QString parsingError(Context *c, const QVariant &errorData = QVariant()) const;
463 
494  virtual QString genericParsingError(Context *c, const QVariant &errorData = QVariant()) const;
495 
510  QString validationDataError(Context *c, const QVariant &errorData = QVariant()) const;
511 
542  virtual QString genericValidationDataError(Context *c,
543  const QVariant &errorData = QVariant()) const;
544 
551  void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const;
552 
553 private:
554  Q_DECLARE_PRIVATE(ValidatorRule)
555  Q_DISABLE_COPY(ValidatorRule)
556 
557 
562  void setTranslationContext(QLatin1String trContext);
563 
574  void setTrimBefore(bool trimBefore);
575 
576  friend class Validator;
577  friend class ValidatorPrivate;
578 };
579 
580 } // namespace Cutelyst
581 
582 #endif // CUTELYSTVALIDATORRULE_H
ValidatorMessages()
Constructs a default ValidatorMessages object with all custom messages disabled.
Stores custom error messages and the input field label.
bool isNull() const const
The Cutelyst Context.
Definition: context.h:38
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
Validation processor for input data
Definition: validator.h:282
bool isValid() const
Returns true if validation succeeded.
Definition: validatorrule.h:74
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
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.