Cutelyst  2.5.0
validatorrule.h
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CUTELYSTVALIDATORRULE_H
19 #define CUTELYSTVALIDATORRULE_H
20 
21 #include <Cutelyst/cutelyst_global.h>
22 #include <Cutelyst/paramsmultimap.h>
23 
24 #include <QScopedPointer>
25 #include <QVariant>
26 #include <QLoggingCategory>
27 
28 Q_DECLARE_LOGGING_CATEGORY(C_VALIDATOR)
29 
30 namespace Cutelyst {
31 
51 class Context;
52 
62 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorReturnType {
63  QString errorMessage;
64  QVariant value;
65  QVariant extra;
72  explicit operator bool() const {
73  return errorMessage.isNull();
74  }
75 
81  bool isValid() const {
82  return errorMessage.isNull();
83  }
84 };
85 
144 struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
157  ValidatorMessages(const char *customLabel, const char *customValidationError = nullptr, const char *customParsingError = nullptr, const char *customValidationDataError = nullptr) :
158  label(customLabel),
159  validationError(customValidationError),
160  parsingError(customParsingError),
161  validationDataError(customValidationDataError)
162  {}
163  const char *label = nullptr;
164  const char *validationError = nullptr;
165  const char *parsingError = nullptr;
166  const char *validationDataError = nullptr;
167 };
168 
169 class ValidatorRulePrivate;
170 
292 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
293 {
294 public:
301  ValidatorRule(const QString &field, const ValidatorMessages &messages = ValidatorMessages(), const QString &defValKey = QString());
302 
306  ~ValidatorRule();
307 
308 protected:
309  const QScopedPointer<ValidatorRulePrivate> d_ptr;
314  ValidatorRule(ValidatorRulePrivate &dd);
315 
358  virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
359 
364  QString field() const;
365 
371  QString label(Context *c) const;
372 
376  QString value(const ParamsMultiMap &params) const;
377 
384  bool trimBefore() const;
385 
399  QString validationError(Context *c, const QVariant &errorData = QVariant()) const;
400 
429  virtual QString genericValidationError(Context *c, const QVariant &errorData = QVariant()) const;
430 
444  QString parsingError(Context *c, const QVariant &errorData = QVariant()) const;
445 
474  virtual QString genericParsingError(Context *c, const QVariant &errorData = QVariant()) const;
475 
489  QString validationDataError(Context *c, const QVariant &errorData = QVariant()) const;
490 
519  virtual QString genericValidationDataError(Context *c, const QVariant &errorData = QVariant()) const;
520 
527  void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const;
528 
529 private:
530  Q_DECLARE_PRIVATE(ValidatorRule)
531  Q_DISABLE_COPY(ValidatorRule)
532 
533 
538  void setTranslationContext(QLatin1String trContext);
539 
549  void setTrimBefore(bool trimBefore);
550 
551  friend class Validator;
552  friend class ValidatorPrivate;
553 };
554 
555 }
556 
557 #endif //CUTELYSTVALIDATORRULE_H
QMap< QString, QString > ParamsMultiMap
ValidatorMessages()
Constructs a default ValidatorMessages object with all custom messages disabled.
Stores custom error messages and the input field label.
The Cutelyst Context.
Definition: context.h:50
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:239
bool isValid() const
Returns true if validation succeeded.
Definition: validatorrule.h:81
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
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.