forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testvalidator.cpp
4024 lines (3416 loc) · 174 KB
/
testvalidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef VALIDATORTEST_H
#define VALIDATORTEST_H
#include "coverageobject.h"
#include "headers.h"
#include <Cutelyst/Plugins/Utils/Validator/Validator>
#include <Cutelyst/Plugins/Utils/Validator/Validators>
#include <Cutelyst/Plugins/Utils/Validator/validatorresult.h>
#include <Cutelyst/application.h>
#include <Cutelyst/controller.h>
#include <Cutelyst/headers.h>
#include <Cutelyst/upload.h>
#include <limits>
#include <QCryptographicHash>
#include <QHostInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocale>
#include <QObject>
#include <QRegularExpression>
#include <QStringList>
#include <QTest>
#include <QTimeZone>
#include <QUrlQuery>
#include <QUuid>
using namespace Cutelyst;
class TestValidator : public CoverageObject
{
Q_OBJECT
public:
explicit TestValidator(QObject *parent = nullptr)
: CoverageObject(parent)
{
}
~TestValidator() {}
private Q_SLOTS:
void initTestCase();
void testValidator_data();
void testValidator() { doTest(); }
void testValidatorAccepted_data();
void testValidatorAccepted() { doTest(); };
void testValidatorAfter_data();
void testValidatorAfter() { doTest(); };
void testValidatorAlpha_data();
void testValidatorAlpha() { doTest(); };
void testValidatorAlphaDash_data();
void testValidatorAlphaDash() { doTest(); };
void testValidatorAlphaNum_data();
void testValidatorAlphaNum() { doTest(); };
void testValidatorBefore_data();
void testValidatorBefore() { doTest(); };
void testValidatorBetween_data();
void testValidatorBetween() { doTest(); };
void testValidatorBoolean_data();
void testValidatorBoolean() { doTest(); };
void testValidatorCharNotAllowed_data();
void testValidatorCharNotAllowed() { doTest(); }
void testValidatorConfirmed_data();
void testValidatorConfirmed() { doTest(); };
void testValidatorDate_data();
void testValidatorDate() { doTest(); };
void testValidatorDateTime_data();
void testValidatorDateTime() { doTest(); };
void testValidatorDifferent_data();
void testValidatorDifferent() { doTest(); };
void testValidatorDigits_data();
void testValidatorDigits() { doTest(); };
void testValidatorDigitsBetween_data();
void testValidatorDigitsBetween() { doTest(); };
void testValidatorDomain_data();
void testValidatorDomain() { doTest(); };
void testValidatorEmail_data();
void testValidatorEmail() { doTest(); };
void testValidatorFileSize_data();
void testValidatorFileSize() { doTest(); };
void testValidatorFilled_data();
void testValidatorFilled() { doTest(); };
void testValidatorIn_data();
void testValidatorIn() { doTest(); };
void testValidatorInteger_data();
void testValidatorInteger() { doTest(); };
void testValidatorIp_data();
void testValidatorIp() { doTest(); };
void testValidatorJson_data();
void testValidatorJson() { doTest(); };
void testValidatorMax_data();
void testValidatorMax() { doTest(); };
void testValidatorMin_data();
void testValidatorMin() { doTest(); };
void testValidatorNotIn_data();
void testValidatorNotIn() { doTest(); };
void testValidatorNumeric_data();
void testValidatorNumeric() { doTest(); };
void testValidatorPresent_data();
void testValidatorPresent() { doTest(); };
#ifdef CUTELYST_VALIDATOR_WITH_PWQUALITY
void testValidatorPwQuality_data();
void testValidatorPwQuality() { doTest(); };
#endif
void testValidatorRegex_data();
void testValidatorRegex() { doTest(); };
void testValidatorRequired_data();
void testValidatorRequired() { doTest(); };
void testValidatorRequiredIf_data();
void testValidatorRequiredIf() { doTest(); };
void testValidatorRequiredIfStash_data();
void testValidatorRequiredIfStash() { doTest(); };
void testValidatorRequiredUnless_data();
void testValidatorRequiredUnless() { doTest(); };
void testValidatorRequiredUnlessStash_data();
void testValidatorRequiredUnlessStash() { doTest(); };
void testValidatorRequiredWith_data();
void testValidatorRequiredWith() { doTest(); };
void testValidatorRequiredWithAll_data();
void testValidatorRequiredWithAll() { doTest(); };
void testValidatorRequiredWithout_data();
void testValidatorRequiredWithout() { doTest(); };
void testValidatorRequiredWithoutAll_data();
void testValidatorRequiredWithoutAll() { doTest(); };
void testValidatorSame_data();
void testValidatorSame() { doTest(); };
void testValidatorSize_data();
void testValidatorSize() { doTest(); };
void testValidatorTime_data();
void testValidatorTime() { doTest(); };
void testValidatorUrl_data();
void testValidatorUrl() { doTest(); };
void cleanupTestCase();
private:
TestEngine *m_engine;
TestEngine *getEngine();
void doTest();
const QByteArray valid{"valid"};
const QByteArray invalid{"invalid"};
const QByteArray parsingError{"parsingerror"};
const QByteArray validationDataError{"validationdataerror"};
const QList<Qt::DateFormat> dateFormats{Qt::ISODate, Qt::RFC2822Date, Qt::TextDate};
};
class ValidatorTest : public Controller
{
Q_OBJECT
public:
explicit ValidatorTest(QObject *parent)
: Controller(parent)
{
}
// ***** Endpoint for checking Validator::BodyParamsOnly
C_ATTR(bodyParamsOnly, :Local :AutoArgs)
void bodyParamsOnly(Context *c)
{
Validator v({new ValidatorRequired(u"req_field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::BodyParamsOnly));
}
// ***** Endpoint for checking Validator::QueryParamsOnly
C_ATTR(queryParamsOnly, :Local :AutoArgs)
void queryParamsOnly(Context *c)
{
Validator v({new ValidatorRequired(u"req_field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::QueryParamsOnly));
}
// ***** Endpoint for ValidatorAccepted ******
C_ATTR(accepted, :Local :AutoArgs)
void accepted(Context *c)
{
Validator v({new ValidatorAccepted(u"accepted_field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with QDate ******
C_ATTR(afterDate, :Local :AutoArgs)
void afterDate(Context *c)
{
Validator v({new ValidatorAfter(
u"after_field"_qs, QDate::currentDate(), QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with QTime ******
C_ATTR(afterTime, :Local :AutoArgs)
void afterTime(Context *c)
{
Validator v({new ValidatorAfter(
u"after_field"_qs, QTime(12, 0), QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with QDateTime ******
C_ATTR(afterDateTime, :Local :AutoArgs)
void afterDateTime(Context *c)
{
Validator v({new ValidatorAfter(u"after_field"_qs,
QDateTime::currentDateTime(),
QString(),
nullptr,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with custom format ******
C_ATTR(afterFormat, :Local :AutoArgs)
void afterFormat(Context *c)
{
Validator v({new ValidatorAfter(u"after_field"_qs,
QDateTime::currentDateTime(),
QString(),
"yyyy d MM HH:mm",
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with invalid validation data ******
C_ATTR(afterInvalidValidationData, :Local :AutoArgs)
void afterInvalidValidationData(Context *c)
{
Validator v({new ValidatorAfter(
u"after_field"_qs, QDate(), QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with invalid validation data 2 ******
C_ATTR(afterInvalidValidationData2, :Local :AutoArgs)
void afterInvalidValidationData2(Context *c)
{
Validator v({new ValidatorAfter(
u"after_field"_qs, u"schiet"_qs, QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with time zone *****
C_ATTR(afterValidWithTimeZone, :Local :AutoArgs)
void afterValidWithTimeZone(Context *c)
{
Validator v({new ValidatorAfter(u"after_field"_qs,
QDateTime(QDate(2018, 1, 15),
QTime(12, 0),
QTimeZone(QByteArrayLiteral("Indian/Christmas"))),
u"Europe/Berlin"_qs,
nullptr,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAfter with time zone in iput field *****
C_ATTR(afterValidWithTimeZoneField, :Local :AutoArgs)
void afterValidWithTimeZoneField(Context *c)
{
Validator v({new ValidatorAfter(u"after_field"_qs,
QDateTime(QDate(2018, 1, 15),
QTime(12, 0),
QTimeZone(QByteArrayLiteral("Indian/Christmas"))),
u"tz_field"_qs,
nullptr,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAlpha ******
C_ATTR(alpha, :Local :AutoArgs)
void alpha(Context *c)
{
Validator v({new ValidatorAlpha(u"alpha_field"_qs, false, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// **** Endpoint for ValidatorAlpha only ASCII *****
C_ATTR(alphaAscii, :Local :AutoArgs)
void alphaAscii(Context *c)
{
Validator v({new ValidatorAlpha(u"alpha_field"_qs, true, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAlphaDash ******
C_ATTR(alphaDash, :Local :AutoArgs)
void alphaDash(Context *c)
{
Validator v({new ValidatorAlphaDash(u"alphadash_field"_qs, false, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAlphaDash only ASCII ******
C_ATTR(alphaDashAscii, :Local :AutoArgs)
void alphaDashAscii(Context *c)
{
Validator v({new ValidatorAlphaDash(u"alphadash_field"_qs, true, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAlphaNum ******
C_ATTR(alphaNum, :Local :AutoArgs)
void alphaNum(Context *c)
{
Validator v({new ValidatorAlphaNum(u"alphanum_field"_qs, false, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorAlphaNum only ASCII ******
C_ATTR(alphaNumAscii, :Local :AutoArgs)
void alphaNumAscii(Context *c)
{
Validator v({new ValidatorAlphaNum(u"alphanum_field"_qs, true, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with QDate ******
C_ATTR(beforeDate, :Local :AutoArgs)
void beforeDate(Context *c)
{
Validator v({new ValidatorBefore(
u"before_field"_qs, QDate::currentDate(), QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with QTime ******
C_ATTR(beforeTime, :Local :AutoArgs)
void beforeTime(Context *c)
{
Validator v({new ValidatorBefore(
u"before_field"_qs, QTime(12, 0), QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with QDateTime ******
C_ATTR(beforeDateTime, :Local :AutoArgs)
void beforeDateTime(Context *c)
{
Validator v({new ValidatorBefore(u"before_field"_qs,
QDateTime::currentDateTime(),
QString(),
nullptr,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with custom format ******
C_ATTR(beforeFormat, :Local :AutoArgs)
void beforeFormat(Context *c)
{
Validator v({new ValidatorBefore(u"before_field"_qs,
QDateTime::currentDateTime(),
QString(),
"yyyy d MM HH:mm",
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with invalid validation data ******
C_ATTR(beforeInvalidValidationData, :Local :AutoArgs)
void beforeInvalidValidationData(Context *c)
{
Validator v({new ValidatorBefore(
u"before_field"_qs, QDate(), QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with invalid validation data 2 ******
C_ATTR(beforeInvalidValidationData2, :Local :AutoArgs)
void beforeInvalidValidationData2(Context *c)
{
Validator v({new ValidatorBefore(
u"before_field"_qs, u"schiet"_qs, QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with time zone *****
C_ATTR(beforeValidWithTimeZone, :Local :AutoArgs)
void beforeValidWithTimeZone(Context *c)
{
Validator v({new ValidatorBefore(u"after_field"_qs,
QDateTime(QDate(2018, 1, 15),
QTime(12, 0),
QTimeZone(QByteArrayLiteral("America/Tijuana"))),
u"Europe/Berlin"_qs,
nullptr,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBefore with time zone in iput field *****
C_ATTR(beforeValidWithTimeZoneField, :Local :AutoArgs)
void beforeValidWithTimeZoneField(Context *c)
{
Validator v({new ValidatorBefore(u"after_field"_qs,
QDateTime(QDate(2018, 1, 15),
QTime(12, 0),
QTimeZone(QByteArrayLiteral("America/Tijuana"))),
u"tz_field"_qs,
nullptr,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBetween with int ******
C_ATTR(betweenInt, :Local :AutoArgs)
void betweenInt(Context *c)
{
Validator v({new ValidatorBetween(
u"between_field"_qs, QMetaType::Int, -10, 10, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBetween with uint ******
C_ATTR(betweenUint, :Local :AutoArgs)
void betweenUint(Context *c)
{
Validator v({new ValidatorBetween(
u"between_field"_qs, QMetaType::UInt, 10, 20, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBetween with float ******
C_ATTR(betweenFloat, :Local :AutoArgs)
void betweenFloat(Context *c)
{
Validator v({new ValidatorBetween(
u"between_field"_qs, QMetaType::Float, -10.0, 10.0, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBetween with string ******
C_ATTR(betweenString, :Local :AutoArgs)
void betweenString(Context *c)
{
Validator v({new ValidatorBetween(
u"between_field"_qs, QMetaType::QString, 5, 10, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorBoolean ******
C_ATTR(boolean, :Local :AutoArgs)
void boolean(Context *c)
{
Validator v({new ValidatorBoolean(u"boolean_field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorCharNotAllowed ******
C_ATTR(charNotAllowed, :Local :AutoArgs)
void charNotAllowed(Context *c)
{
Validator v({new ValidatorCharNotAllowed(
u"char_not_allowed_field"_qs, u"#%*."_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorConfirmed ******
C_ATTR(confirmed, :Local :AutoArgs)
void confirmed(Context *c)
{
Validator v({new ValidatorConfirmed(u"pass"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDate with standard formats ******
C_ATTR(date, :Local :AutoArgs)
void date(Context *c)
{
Validator v({new ValidatorDate(u"field"_qs, nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDate with custom format ******
C_ATTR(dateFormat, :Local :AutoArgs)
void dateFormat(Context *c)
{
Validator v({new ValidatorDate(u"field"_qs, "yyyy d MM", m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDateTime with standard formats ******
C_ATTR(dateTime, :Local :AutoArgs)
void dateTime(Context *c)
{
Validator v({new ValidatorDateTime(u"field"_qs, QString(), nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDateTime with custom format ******
C_ATTR(dateTimeFormat, :Local :AutoArgs)
void dateTimeFormat(Context *c)
{
Validator v({new ValidatorDateTime(
u"field"_qs, QString(), "yyyy d MM mm:HH", m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDifferent ******
C_ATTR(different, :Local :AutoArgs)
void different(Context *c)
{
Validator v(
{new ValidatorDifferent(u"field"_qs, u"other"_qs, nullptr, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDigits without exact length ******
C_ATTR(digits, :Local :AutoArgs)
void digits(Context *c)
{
Validator v({new ValidatorDigits(u"field"_qs, -1, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDigits with exact length ******
C_ATTR(digitsLength, :Local :AutoArgs)
void digitsLength(Context *c)
{
Validator v({new ValidatorDigits(u"field"_qs, 10, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDigitsBetween ******
C_ATTR(digitsBetween, :Local :AutoArgs)
void digitsBetween(Context *c)
{
Validator v({new ValidatorDigitsBetween(u"field"_qs, 5, 10, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDomain without DNS check *****
C_ATTR(domain, :Local :AutoArgs)
void domain(Context *c)
{
Validator v({new ValidatorDomain(u"field"_qs, false, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorDomain with DNS check *****
C_ATTR(domainDns, :Local :AutoArgs)
void domainDns(Context *c)
{
Validator v({new ValidatorDomain(u"field"_qs, true, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorEmail valid ****
C_ATTR(emailValid, :Local :AutoArgs)
void emailValid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::Valid, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail valid emails completely valid *****
C_ATTR(emailDnsWarnValid, :Local :AutoArgs)
void emailDnsWarnValid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::Valid, ValidatorEmail::CheckDNS, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail RFC5321 conformant emails valid *****
C_ATTR(emailRfc5321Valid, :Local :AutoArgs)
void emailRfc5321Valid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::RFC5321, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail RFC5321 conformant emails invalid *****
C_ATTR(emailRfc5321Invalid, :Local :AutoArgs)
void emailRfc5321Invalid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::DNSWarn, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail CFWS conformant emails valid *****
C_ATTR(emailCfwsValid, :Local :AutoArgs)
void emailCfwsValid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::CFWS, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail CFWS conformant emails invalid *****
C_ATTR(emailCfwsInvalid, :Local :AutoArgs)
void emailCfwsInvalid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::RFC5321, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail Deprecated emails valid *****
C_ATTR(emailDeprecatedValid, :Local :AutoArgs)
void emailDeprecatedValid(Context *c)
{
Validator v({new ValidatorEmail(u"field"_qs,
ValidatorEmail::Deprecated,
ValidatorEmail::NoOption,
m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail CFWS conformant emails invalid *****
C_ATTR(emailDeprecatedInvalid, :Local :AutoArgs)
void emailDeprecatedInvalid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::CFWS, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail RFC5322 emails valid *****
C_ATTR(emailRfc5322Valid, :Local :AutoArgs)
void emailRfc5322Valid(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::RFC5322, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail RFC5322 conformant emails invalid *****
C_ATTR(emailRfc5322Invalid, :Local :AutoArgs)
void emailRfc5322Invalid(Context *c)
{
Validator v({new ValidatorEmail(u"field"_qs,
ValidatorEmail::Deprecated,
ValidatorEmail::NoOption,
m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail with errors *****
C_ATTR(emailErrors, :Local :AutoArgs)
void emailErrors(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::RFC5322, ValidatorEmail::NoOption, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail with allowed IDNs
C_ATTR(emailIdnAllowed, :Local :AutoArgs)
void emailIdnAllowed(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::Valid, ValidatorEmail::AllowIDN, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail with allowed UTF8 local part
C_ATTR(emailUtf8Local, :Local :AutoArgs)
void emailUtf8Local(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::Valid, ValidatorEmail::UTF8Local, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorEmail with allowed UTF8 local part and IDN
C_ATTR(emailUtf8, :Local :AutoArgs)
void emailUtf8(Context *c)
{
Validator v({new ValidatorEmail(
u"field"_qs, ValidatorEmail::Valid, ValidatorEmail::AllowUTF8, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming | Validator::BodyParamsOnly));
}
// ***** Endpoint for ValidatorFileSize ******
C_ATTR(fileSize, :Local :AutoArgs)
void fileSize(Context *c)
{
ValidatorFileSize::Option option = ValidatorFileSize::NoOption;
const QString opt = c->req()->bodyParameter(u"option"_qs);
if (opt == QLatin1String("OnlyBinary")) {
option = ValidatorFileSize::OnlyBinary;
} else if (opt == QLatin1String("OnlyDecimal")) {
option = ValidatorFileSize::OnlyDecimal;
} else if (opt == QLatin1String("ForceBinary")) {
option = ValidatorFileSize::ForceBinary;
} else if (opt == QLatin1String("ForceDecimal")) {
option = ValidatorFileSize::ForceDecimal;
}
const double min = c->req()->bodyParameter(u"min"_qs, u"-1.0"_qs).toDouble();
const double max = c->req()->bodyParameter(u"max"_qs, u"-1.0"_qs).toDouble();
c->setLocale(QLocale(c->req()->bodyParameter(u"locale"_qs, u"C"_qs)));
Validator v({new ValidatorFileSize(u"field"_qs, option, min, max, m_validatorMessages)});
checkResponse(c, v.validate(c, Validator::NoTrimming));
}
// ***** Endpoint for ValidatorFileSize with return value check *****
C_ATTR(fileSizeValue, :Local :AutoArgs)
void fileSizeValue(Context *c)
{
c->setLocale(QLocale::c());
Validator v({new ValidatorFileSize(u"field"_qs)});
const ValidatorResult r = v.validate(c);
if (r) {
QString sizeString;
const QVariant rv = r.value(u"field"_qs);
if (rv.typeId() == QMetaType::Double) {
sizeString = QString::number(rv.toDouble(), 'f', 2);
} else {
sizeString = QString::number(rv.toULongLong());
}
c->response()->setBody(sizeString.toUtf8());
} else {
c->response()->setBody(r.errorStrings().constFirst());
}
}
// ***** Endpoint for ValidatorFilled ******
C_ATTR(filled, :Local :AutoArgs)
void filled(Context *c)
{
Validator v({new ValidatorFilled(u"field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorIn ******
C_ATTR(in, :Local :AutoArgs)
void in(Context *c)
{
Validator v({new ValidatorIn(u"field"_qs,
QStringList({u"eins"_qs, u"zwei"_qs, u"drei"_qs}),
Qt::CaseSensitive,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorInteger ******
C_ATTR(integer, :Local :AutoArgs)
void integer(Context *c)
{
Validator v({new ValidatorInteger(u"field"_qs, QMetaType::Int, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorIp ******
C_ATTR(ip, :Local :AutoArgs)
void ip(Context *c)
{
ValidatorIp::Constraints constraints = ValidatorIp::NoConstraint;
if (!c->request()->bodyParameter(u"constraints"_qs).isEmpty()) {
QStringList cons = c->request()->bodyParameter(u"constraints"_qs).split(u","_qs);
if (cons.contains(u"IPv4Only"_qs)) {
constraints |= ValidatorIp::IPv4Only;
}
if (cons.contains(u"IPv6Only"_qs)) {
constraints |= ValidatorIp::IPv6Only;
}
if (cons.contains(u"NoPrivateRange"_qs)) {
constraints |= ValidatorIp::NoPrivateRange;
}
if (cons.contains(u"NoReservedRange"_qs)) {
constraints |= ValidatorIp::NoReservedRange;
}
if (cons.contains(u"NoMultiCast"_qs)) {
constraints |= ValidatorIp::NoMultiCast;
}
}
Validator v({new ValidatorIp(u"field"_qs, constraints, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorJson ******
C_ATTR(json, :Local :AutoArgs)
void json(Context *c)
{
Validator v({new ValidatorJson(
u"field"_qs, ValidatorJson::ExpectedType::All, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorJson with object expected ******
C_ATTR(jsonObject, :Local :AutoArgs)
void jsonObject(Context *c)
{
Validator v({new ValidatorJson(
u"field"_qs, ValidatorJson::ExpectedType::Object, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorJson with array expected ******
C_ATTR(jsonArray, :Local :AutoArgs)
void jsonArray(Context *c)
{
Validator v({new ValidatorJson(
u"field"_qs, ValidatorJson::ExpectedType::Array, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorMax ******
C_ATTR(max, :Local :AutoArgs)
void max(Context *c)
{
QMetaType::Type type = QMetaType::UnknownType;
if (!c->request()->bodyParameter(u"type"_qs).isEmpty()) {
const QString t = c->request()->bodyParameter(u"type"_qs);
if (t == QLatin1String("sint")) {
type = QMetaType::Int;
} else if (t == QLatin1String("uint")) {
type = QMetaType::UInt;
} else if (t == QLatin1String("float")) {
type = QMetaType::Float;
} else if (t == QLatin1String("string")) {
type = QMetaType::QString;
}
}
Validator v({new ValidatorMax(u"field"_qs, type, 10, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorMin ******
C_ATTR(min, :Local :AutoArgs)
void min(Context *c)
{
c->setStash(u"compval"_qs, 10);
QMetaType::Type type = QMetaType::UnknownType;
if (!c->request()->bodyParameter(u"type"_qs).isEmpty()) {
const QString t = c->request()->bodyParameter(u"type"_qs);
if (t == QLatin1String("sint")) {
type = QMetaType::Int;
} else if (t == QLatin1String("uint")) {
type = QMetaType::UInt;
} else if (t == QLatin1String("float")) {
type = QMetaType::Float;
} else if (t == QLatin1String("string")) {
type = QMetaType::QString;
}
}
Validator v({new ValidatorMin(u"field"_qs, type, u"compval"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorNotIn ******
C_ATTR(notIn, :Local :AutoArgs)
void notIn(Context *c)
{
Validator v(
{new ValidatorNotIn(u"field"_qs,
QStringList({u"eins"_qs, u"zwei"_qs, u"drei"_qs, u"vier"_qs}),
Qt::CaseSensitive,
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorNumeric ******
C_ATTR(numeric, :Local :AutoArgs)
void numeric(Context *c)
{
Validator v({new ValidatorNumeric(u"field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorPresent ******
C_ATTR(present, :Local :AutoArgs)
void present(Context *c)
{
Validator v({new ValidatorPresent(u"field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorPwQuality *****
#ifdef CUTELYST_VALIDATOR_WITH_PWQUALITY
C_ATTR(pwQuality, :Local :AutoArgs)
void pwQuality(Context *c)
{
static const QVariantMap options({{u"difok"_qs, 1},
{u"minlen"_qs, 8},
{u"dcredit"_qs, 0},
{u"ucredit"_qs, 0},
{u"ocredit"_qs, 0},
{u"lcredit"_qs, 0},
{u"minclass"_qs, 0},
{u"maxrepeat"_qs, 0},
{u"maxclassrepeat"_qs, 0},
{u"maxsequence"_qs, 0},
{u"gecoscheck"_qs, 0},
{u"dictcheck"_qs, 1},
{u"usercheck"_qs, 0}});
static Validator v({new ValidatorPwQuality(
u"field"_qs, 50, options, QString(), QString(), m_validatorMessages)});
checkResponse(c, v.validate(c));
}
#endif
// ***** Endpoint for ValidatorRegularExpression ******
C_ATTR(regex, :Local :AutoArgs)
void regex(Context *c)
{
Validator v({new ValidatorRegularExpression(
u"field"_qs,
QRegularExpression(u"^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$"_qs),
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorRequired ******
C_ATTR(required, :Local :AutoArgs)
void required(Context *c)
{
Validator v({new ValidatorRequired(u"field"_qs, m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorRequiredIf ******
C_ATTR(requiredIf, :Local :AutoArgs)
void requiredIf(Context *c)
{
Validator v({new ValidatorRequiredIf(u"field"_qs,
u"field2"_qs,
QStringList({u"eins"_qs, u"zwei"_qs}),
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorRequiredIfStash with stash match *****
C_ATTR(requiredIfStashMatch, :Local :AutoArgs)
void requiredIfStashMatch(Context *c)
{
c->setStash(u"stashkey"_qs, u"eins"_qs);
Validator v({new ValidatorRequiredIfStash(u"field"_qs,
u"stashkey"_qs,
QVariantList({u"eins"_qs, u"zwei"_qs}),
m_validatorMessages)});
checkResponse(c, v.validate(c));
}
// ***** Endpoint for ValidatorRequiredIfStash with stash match in other stash key *****
C_ATTR(requiredIfStashMatchStashKey, :Local :AutoArgs)
void requiredIfStashMatchStashKey(Context *c)
{
c->setStash(u"stashkey"_qs, u"eins"_qs);
c->setStash(u"otherStashKey"_qs, QStringList({u"eins"_qs, u"zwei"_qs}));
Validator v({new ValidatorRequiredIfStash(
u"field"_qs, u"stashkey"_qs, u"otherStashKey"_qs, m_validatorMessages)});