cutelyst  3.7.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-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 <QScopedPointer>
12 #include <QVariant>
13 #include <QLoggingCategory>
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 {
50  QString errorMessage;
51  QVariant value;
52  QVariant extra;
59  explicit operator bool() const {
60  return errorMessage.isNull();
61  }
62 
68  bool isValid() const {
69  return errorMessage.isNull();
70  }
71 };
72 
131 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
144  ValidatorMessages(const char *customLabel, const char *customValidationError = nullptr, const char *customParsingError = nullptr, const char *customValidationDataError = nullptr) :
145  label(customLabel),
146  validationError(customValidationError),
147  parsingError(customParsingError),
148  validationDataError(customValidationDataError)
149  {}
150  const char *label = nullptr;
151  const char *validationError = nullptr;
152  const char *parsingError = nullptr;
153  const char *validationDataError = nullptr;
154 };
155 
156 class ValidatorRulePrivate;
157 
279 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
280 {
281 public:
288  ValidatorRule(const QString &field, const ValidatorMessages &messages = ValidatorMessages(), const QString &defValKey = QString());
289 
293  virtual ~ValidatorRule();
294 
295 protected:
296  const QScopedPointer<ValidatorRulePrivate> d_ptr;
301  ValidatorRule(ValidatorRulePrivate &dd);
302 
345  virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
346 
351  QString field() const;
352 
358  QString label(Context *c) const;
359 
363  QString value(const ParamsMultiMap &params) const;
364 
371  bool trimBefore() const;
372 
386  QString validationError(Context *c, const QVariant &errorData = QVariant()) const;
387 
416  virtual QString genericValidationError(Context *c, const QVariant &errorData = QVariant()) const;
417 
431  QString parsingError(Context *c, const QVariant &errorData = QVariant()) const;
432 
461  virtual QString genericParsingError(Context *c, const QVariant &errorData = QVariant()) const;
462 
476  QString validationDataError(Context *c, const QVariant &errorData = QVariant()) const;
477 
506  virtual QString genericValidationDataError(Context *c, const QVariant &errorData = QVariant()) const;
507 
514  void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const;
515 
516 private:
517  Q_DECLARE_PRIVATE(ValidatorRule)
518  Q_DISABLE_COPY(ValidatorRule)
519 
520 
525  void setTranslationContext(QLatin1String trContext);
526 
536  void setTrimBefore(bool trimBefore);
537 
538  friend class Validator;
539  friend class ValidatorPrivate;
540 };
541 
542 }
543 
544 #endif //CUTELYSTVALIDATORRULE_H
The Cutelyst Context.
Definition: context.h:39
Base class for all validator rules.
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:227
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()
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
Returns true if validation succeeded.
Definition: validatorrule.h:68