-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional-validation.php
More file actions
456 lines (393 loc) · 12.9 KB
/
conditional-validation.php
File metadata and controls
456 lines (393 loc) · 12.9 KB
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
<?php
require __DIR__ . '/../vendor/autoload.php';
use Gravity\DataVerify;
echo "=== Conditional Validation Examples ===\n\n";
// Example 1: Basic conditional - shipping address required only if delivery is shipping
echo "1. Basic conditional validation\n";
$data1 = new stdClass();
$data1->delivery_type = 'shipping';
$data1->shipping_address = '';
$dv1 = new DataVerify($data1);
$dv1
->field('shipping_address')
->when('delivery_type', '=', 'shipping')
->then->required->string;
if (!$dv1->verify()) {
echo " ✗ Validation failed:\n";
foreach ($dv1->getErrors() as $error) {
echo " - {$error['message']}\n";
}
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 2: AND conditions - all must be true
echo "2. AND conditions (all must be true)\n";
$data2 = new stdClass();
$data2->type = 'premium';
$data2->amount = 150;
$data2->discount_code = '';
$dv2 = new DataVerify($data2);
$dv2
->field('discount_code')
->when('type', '=', 'premium')
->and('amount', '>', 100)
->then->required->string;
if (!$dv2->verify()) {
echo " ✗ Discount code required for premium orders over 100€\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 3: OR conditions - at least one must be true
echo "3. OR conditions (at least one must be true)\n";
$data3 = new stdClass();
$data3->country = 'BE';
$data3->vat_number = '';
$dv3 = new DataVerify($data3);
$dv3
->field('vat_number')
->when('country', '=', 'FR')
->or('country', '=', 'BE')
->or('country', '=', 'DE')
->then->required->regex('/^[A-Z]{2}\d{9,11}$/');
if (!$dv3->verify()) {
echo " ✗ VAT number required for FR, BE, or DE\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 4: Complex AND with multiple conditions
echo "4. Complex AND with multiple conditions\n";
$data4 = new stdClass();
$data4->age = 25;
$data4->country = 'FR';
$data4->income = 50000;
$data4->premium_feature = '';
$dv4 = new DataVerify($data4);
$dv4
->field('premium_feature')
->when('age', '>=', 18)
->and('country', 'in', ['FR', 'BE'])
->and('income', '>', 30000)
->then->required;
if (!$dv4->verify()) {
echo " ✗ Premium feature access denied\n";
} else {
echo " ✓ Premium feature validation passed\n";
}
echo "\n";
// Example 5: Numeric comparison - parental consent for minors
echo "5. Age-based conditional validation\n";
$data5 = new stdClass();
$data5->age = 15;
$data5->parental_consent = null;
$dv5 = new DataVerify($data5);
$dv5
->field('parental_consent')
->when('age', '<', 18)
->then->required->boolean;
if (!$dv5->verify()) {
echo " ✗ Parental consent required for users under 18\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 6: Multiple conditional validations on different fields
echo "6. Multiple conditional validations\n";
$data6 = new stdClass();
$data6->account_type = 'business';
$data6->has_vat = true;
$data6->company_name = '';
$data6->vat_number = '';
$dv6 = new DataVerify($data6);
$dv6
->field('company_name')
->when('account_type', '=', 'business')
->then->required->string
->field('vat_number')
->when('has_vat', '=', true)
->then->required->string;
if (!$dv6->verify()) {
echo " ✗ Business account validation failed:\n";
foreach ($dv6->getErrors() as $error) {
echo " - {$error['field']}: {$error['message']}\n";
}
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 7: Mix normal and conditional validations
echo "7. Mix normal and conditional validations\n";
$data7 = new stdClass();
$data7->email = 'invalid-email';
$data7->newsletter = true;
$data7->phone = '';
$dv7 = new DataVerify($data7);
$dv7
->field('email')
->required
->email
->field('phone')
->when('newsletter', '=', true)
->then->required;
if (!$dv7->verify()) {
echo " ✗ Validation errors:\n";
foreach ($dv7->getErrors() as $error) {
echo " - {$error['field']}: {$error['test']} failed\n";
}
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 8: Conditional on nested objects with AND
echo "8. Conditional on nested objects with AND\n";
$data8 = new stdClass();
$data8->user = new stdClass();
$data8->user->type = 'business';
$data8->user->country = 'FR';
$data8->user->vat_number = '';
$dv8 = new DataVerify($data8);
$dv8
->field('user')->required->object
->subfield('vat_number')
->when('user.type', '=', 'business')
->and('user.country', 'in', ['FR', 'DE', 'IT'])
->then->required->string->minLength(9);
if (!$dv8->verify()) {
echo " ✗ VAT required for business users in EU\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 9: Deeply nested paths with conditions
echo "9. Deeply nested paths\n";
$data9 = new stdClass();
$data9->config = new stdClass();
$data9->config->features = new stdClass();
$data9->config->features->advanced = new stdClass();
$data9->config->features->advanced->enabled = true;
$data9->config->features->advanced->api_key = '';
$dv9 = new DataVerify($data9);
$dv9
->field('config')->required->object
->subfield('features')->required->object
->subfield('features', 'advanced')->required->object
->subfield('features', 'advanced', 'api_key')
->when('config.features.advanced.enabled', '=', true)
->then->required->string;
if (!$dv9->verify()) {
echo " ✗ API key required when advanced features enabled\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 10: Success case - condition not met
echo "10. Success when condition is not met\n";
$data10 = new stdClass();
$data10->delivery_type = 'pickup';
$data10->shipping_address = '';
$dv10 = new DataVerify($data10);
$dv10
->field('shipping_address')
->when('delivery_type', '=', 'shipping')
->then->required;
if ($dv10->verify()) {
echo " ✓ Shipping address not required for pickup\n";
} else {
echo " ✗ Validation failed\n";
}
echo "\n";
// Example 11: Using 'not_in' operator with OR
echo "11. Not-in operator with OR conditions\n";
$data11 = new stdClass();
$data11->payment_method = 'crypto';
$data11->kyc_document = '';
$dv11 = new DataVerify($data11);
$dv11
->field('kyc_document')
->when('payment_method', '=', 'crypto')
->or('payment_method', '=', 'wire_transfer')
->then->required->string;
if (!$dv11->verify()) {
echo " ✗ KYC document required for crypto or wire transfers\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 12: Multiple fields with different OR conditions
echo "12. Multiple fields with different OR conditions\n";
$data12 = new stdClass();
$data12->status = 'pending';
$data12->priority = 'high';
$data12->approval = '';
$data12->escalation = '';
$dv12 = new DataVerify($data12);
$dv12
->field('approval')
->when('status', '=', 'pending')
->or('status', '=', 'review')
->then->required
->field('escalation')
->when('priority', '=', 'high')
->or('priority', '=', 'urgent')
->then->required;
if (!$dv12->verify()) {
echo " ✗ Multiple conditional validations failed:\n";
foreach ($dv12->getErrors() as $error) {
echo " - {$error['field']}\n";
}
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
echo "=== End of examples ===\n";
// Example 13: Deferred conditional evaluation - default + conditional rules
echo "13. Mix default and conditional rules (new in v1.1.0)\n";
$data13 = new stdClass();
$data13->user = new stdClass();
$data13->user->id = 123;
$data13->user->password = 'weak';
$dv13 = new DataVerify($data13);
$dv13
->field('user')->required->object
->subfield('password')
->minLength(8) // Always applied
->containsUpper // Always applied
->when('user.id', '!=', null)
->then->required; // Applied if user.id exists
if (!$dv13->verify()) {
echo " ✗ Password must be 8+ chars with uppercase, and required for existing users\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 14: Multiple conditional blocks on same field
echo "14. Multiple conditional blocks on same field\n";
$data14 = new stdClass();
$data14->user = new stdClass();
$data14->user->id = 456;
$data14->user->role = 'admin';
$data14->user->password = 'ShortPass1!';
$dv14 = new DataVerify($data14);
$dv14
->field('user')->required->object
->subfield('password')
->minLength(8) // Default
->when('user.id', '!=', null)
->then->required // Block 1: required for existing users
->when('user.role', '=', 'admin')
->then->minLength(16) // Block 2: 16+ chars for admins
->containsSpecialCharacter; // Block 2: special char for admins
if (!$dv14->verify()) {
echo " ✗ Admin password requires 16+ characters with special character\n";
foreach ($dv14->getErrors() as $error) {
echo " - {$error['field']}: {$error['test']} failed\n";
}
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 15: Conditional blocks across multiple fields
echo "15. Conditional blocks across multiple fields\n";
$data15 = new stdClass();
$data15->subscription = new stdClass();
$data15->subscription->plan = 'enterprise';
$data15->subscription->users = 5;
$data15->subscription->payment_method = 'invoice';
$data15->subscription->billing_email = '';
$data15->subscription->card_number = '';
$dv15 = new DataVerify($data15);
$dv15
->field('subscription')->required->object
->subfield('billing_email')
->email
->when('subscription.plan', '=', 'enterprise')
->then->required
->subfield('card_number')
->when('subscription.payment_method', '=', 'card')
->then->required->regex('/^\d{16}$/')
->subfield('users')
->int
->when('subscription.plan', 'in', ['pro', 'enterprise'])
->then->required->between(1, 1000);
if (!$dv15->verify()) {
echo " ✗ Enterprise subscription validation failed:\n";
foreach ($dv15->getErrors() as $error) {
echo " - {$error['field']}: {$error['message']}\n";
}
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 16: Conditional with complex AND/OR combinations
echo "16. Complex AND/OR combinations\n";
$data16 = new stdClass();
$data16->tier = 'premium';
$data16->status = 'active';
$data16->country = 'FR';
$data16->features = [];
$dv16 = new DataVerify($data16);
$dv16
->field('features')
->array
->when('tier', '=', 'premium')
->and('status', '=', 'active')
->and('country', 'in', ['FR', 'DE', 'BE'])
->then->required;
if (!$dv16->verify()) {
echo " ✗ Premium features required for active premium users in EU\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
// Example 17: API REST use case - POST vs PATCH
echo "17. API REST - POST vs PATCH pattern\n";
// POST /users - creating new user (id = null)
echo " POST /users (new user):\n";
$postData = new stdClass();
$postData->user = new stdClass();
$postData->user->id = null;
$postData->user->email = 'new@example.com';
$postData->user->password = 'OptionalPass123';
$dvPost = new DataVerify($postData);
$dvPost
->field('user')->required->object
->subfield('email')
->email // Format checked if present
->when('user.id', '!=', null)
->then->required // Not required for POST
->subfield('password')
->minLength(12) // Format checked if present
->when('user.id', '!=', null)
->then->required; // Not required for POST
if ($dvPost->verify()) {
echo " ✓ Valid - email and password optional for new users\n";
} else {
echo " ✗ Validation failed\n";
}
// PATCH /users/123 - updating existing user (id exists)
echo " PATCH /users/123 (update user):\n";
$patchData = new stdClass();
$patchData->user = new stdClass();
$patchData->user->id = 123;
$patchData->user->email = '';
$patchData->user->password = '';
$dvPatch = new DataVerify($patchData);
$dvPatch
->field('user')->required->object
->subfield('email')
->email
->when('user.id', '!=', null)
->then->required // Required for PATCH
->subfield('password')
->minLength(12)
->when('user.id', '!=', null)
->then->required; // Required for PATCH
if (!$dvPatch->verify()) {
echo " ✗ Validation failed - email and password required for updates\n";
} else {
echo " ✓ Validation passed\n";
}
echo "\n";
echo "=== End of conditional validation examples ===\n";