cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoremail.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef CUTELYSTVALIDATOREMAIL_H
6 #define CUTELYSTVALIDATOREMAIL_H
7 
8 #include "validatorrule.h"
9 
10 namespace Cutelyst {
11 
12 class ValidatorEmailPrivate;
13 
46 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorEmail : public ValidatorRule
47 {
48  Q_GADGET
49 public:
53  enum Diagnose : int {
54  // Address is valid
55  ValidAddress =
56  0,
59  // Address is valid but there are warnings about the DNS
60  DnsWarnNoMxRecord =
61  4,
66  // Address is valid but a DNS check was not successful
67  DnsMxDisabled = 5,
69  DnsNoRecordFound =
70  6,
71  DnsErrorTimeout = 7,
72  DnsError =
73  8,
74  // Address is valid for SMTP but has unusual Elements
75  RFC5321TLD = 9,
76  RFC5321TLDNumeric =
77  10,
78  RFC5321QuotedString = 11,
79  RFC5321AddressLiteral = 12,
80  RFC5321IPv6Deprecated =
81  13,
84  // Address is valid within the message but cannot be used unmodified for the envelope
85  CFWSComment = 17,
86  CFWSFWS = 18,
87  // Address contains deprecated elements but may still be valid in restricted contexts
88  DeprecatedLocalpart = 33,
89  DeprecatedFWS = 34,
90  DeprecatedQText = 35,
91  DeprecatedQP = 36,
92  DeprecatedComment = 37,
93  DeprecatedCText = 38,
94  DeprecatedCFWSNearAt =
95  49,
96  // The address in only valid according to the broad definition of RFC 5322. It is otherwise
97  // invalid
98  RFC5322Domain =
99  65,
101  RFC5322TooLong = 66,
102  RFC5322LocalTooLong = 67,
103  RFC5322DomainTooLong = 68,
104  RFC5322LabelTooLong = 69,
105  RFC5322DomainLiteral =
106  70,
108  RFC5322DomLitOBSDText = 71,
111  RFC5322IPv6GroupCount =
112  72,
113  RFC5322IPv62x2xColon = 73,
114  RFC5322IPv6BadChar = 74,
115  RFC5322IPv6MaxGroups = 75,
116  RFC5322IPv6ColonStart = 76,
117  RFC5322IPv6ColonEnd = 77,
118  // Address is invalid for any purpose
119  ErrorExpectingDText =
120  129,
121  ErrorNoLocalPart = 130,
122  ErrorNoDomain = 131,
123  ErrorConsecutiveDots = 132,
124  ErrorATextAfterCFWS =
125  133,
126  ErrorATextAfterQS = 134,
127  ErrorATextAfterDomLit =
128  135,
129  ErrorExpectingQpair =
130  136,
131  ErrorExpectingAText = 137,
132  ErrorExpectingQText = 138,
133  ErrorExpectingCText = 139,
134  ErrorBackslashEnd = 140,
135  ErrorDotStart = 141,
136  ErrorDotEnd = 142,
137  ErrorDomainHyphenStart = 143,
138  ErrorDomainHyphenEnd = 144,
139  ErrorUnclosedQuotedStr = 145,
140  ErrorUnclosedComment = 146,
141  ErrorUnclosedDomLiteral = 147,
142  ErrorFWSCRLFx2 = 148,
143  ErrorFWSCRLFEnd = 149,
144  ErrorCRnoLF =
145  150,
146  ErrorFatal = 254
147  };
148  Q_ENUM(Diagnose)
149 
150 
153  enum Category : int {
154  Valid = 1,
155  DNSWarn =
156  DnsMxDisabled,
158  DNSFailed =
159  RFC5321TLD,
161  RFC5321 = CFWSComment,
164  CFWS = DeprecatedLocalpart,
166  Deprecated = RFC5322Domain,
168  RFC5322 = ErrorExpectingDText,
171  Error = 255
173  };
174  Q_ENUM(Category)
175 
176  enum Option : quint8 {
177  NoOption = 0,
178  CheckDNS = 1,
180  UTF8Local = 2,
181  AllowIDN = 4,
182  AllowUTF8 = UTF8Local | AllowIDN
184  };
185  Q_DECLARE_FLAGS(Options, Option)
186 
187 
196  explicit ValidatorEmail(const QString &field,
197  Category threshold = RFC5321,
198  Options options = NoOption,
199  const ValidatorMessages &messages = {},
200  const QString &defValKey = {});
201 
205  ~ValidatorEmail() override;
206 
214  static QString diagnoseString(const Context *c, Diagnose diagnose, const QString &label = {});
215 
223  static QString categoryString(const Context *c, Category category, const QString &label = {});
224 
230  static Category category(Diagnose diagnose);
231 
240  static QString categoryString(const Context *c, Diagnose diagnose, const QString &label = {});
241 
257  static bool validate(const QString &email,
258  Category threshold = RFC5321,
259  Options options = NoOption,
260  QList<Diagnose> *diagnoses = nullptr);
261 
276  static void validateCb(
277  const QString &email,
278  Category threshold,
279  Options options,
280  std::function<
281  void(bool isValid, const QString &cleanedEmail, const QList<Diagnose> &diagnoses)> cb);
282 
283 protected:
295  ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override;
296 
308  void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override;
309 
313  QString genericValidationError(Context *c,
314  const QVariant &errorData = QVariant()) const override;
315 
316 private:
317  Q_DECLARE_PRIVATE(ValidatorEmail) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
318  Q_DISABLE_COPY(ValidatorEmail)
319 };
320 
321 } // namespace Cutelyst
322 
323 Q_DECLARE_OPERATORS_FOR_FLAGS(Cutelyst::ValidatorEmail::Options)
324 
325 #endif // CUTELYSTVALIDATOREMAIL_H
Stores custom error messages and the input field label.
Checks if the value is a valid email address according to specific RFCs.
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
QMultiMap< QString, QString > ParamsMultiMap
Category
Validation category, used as threshold to define valid addresses.
Diagnose
Single diagnose values that show why an address is not valid.