Skip to content

Commit 4a9bcd3

Browse files
authored
Merge pull request #339 from hassanhabib/users/cjdutoit/coderub-validation-engine
CODE RUB: Update to validation engine to allow injection of message
2 parents b5d4ef7 + e557c3f commit 4a9bcd3

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

2. Services/2.1 Foundations/2.1 Foundations.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,17 @@ Our dynamic rule now can offer more input parameters and more helpful informatio
387387
Now that we have the advanced exceptions and the dynamic validation rules. It's time to put it all together in terms of accepting infinite number of validation rules, examining their condition results and finally break the circuit when all the continuous validations are done. Here's how to do that:
388388

389389
```csharp
390-
private void Validate(params (dynamic Rule, string Parameter)[] validations)
390+
private void Validate(
391+
string message,
392+
params (dynamic Rule, string Parameter)[] validations)
391393
{
392-
var invalidStudentException = new InvalidStudentException(
393-
message: "Student is invalid. Please fix the errors and try again.");
394+
var invalidStudentException = new InvalidStudentException(message);
394395

395396
foreach((dynamic rule, string parameter) in validations)
396397
{
397398
if(rule.Condition)
398399
{
399-
invalidStudentException.UpsertDataList(parameter, rule.Message);
400+
invalidStudentException.UpsertDataList(key: parameter, value: rule.Message);
400401
}
401402
}
402403

@@ -412,6 +413,7 @@ private static void ValidateStudentOnAdd(Student student)
412413
......
413414

414415
Validate(
416+
message: "Student is invalid. Please fix the errors and try again.",
415417
(Rule: IsInvalid(student.Id), Parameter: nameof(Student.Id)),
416418
(Rule: IsInvalid(student.Name), Parameter: nameof(Student.Name)),
417419
(Rule: IsInvalid(student.Grade), Parameter: nameof(Student.Grade))
@@ -456,6 +458,7 @@ private async ValueTask ValidateStudentOnAddAsync(Student student)
456458
......
457459

458460
Validate(
461+
message: "Student is invalid. Please fix the errors and try again.",
459462
(Rule: IsInvalid(student.Id), Parameter: nameof(Student.Id)),
460463
(Rule: IsInvalid(student.Name), Parameter: nameof(Student.Name)),
461464
(Rule: IsInvalid(student.Address), Parameter: nameof(Student.Address))
@@ -464,6 +467,7 @@ private async ValueTask ValidateStudentOnAddAsync(Student student)
464467
......
465468

466469
Validate(
470+
message: "Student address is invalid. Please fix the errors and try again.",
467471
(Rule: IsInvalid(student.Address.Street), Parameter: nameof(StudentAddress.Street)),
468472
(Rule: IsInvalid(student.Address.City), Parameter: nameof(StudentAddress.City)),
469473
(Rule: IsInvalid(student.Address.ZipCode), Parameter: nameof(StudentAddress.ZipCode))
@@ -631,7 +635,9 @@ private static void ValidateStudentOnAdd(Student student)
631635
{
632636
......
633637

634-
Validate((Rule: IsInvalid(student.Id), Parameter: nameof(Student.Id)));
638+
Validate(
639+
message: "Student is invalid. Please fix the errors and try again.",
640+
(Rule: IsInvalid(student.Id), Parameter: nameof(Student.Id)));
635641
}
636642

637643
private static dynamic IsInvalid(Guid id) => new
@@ -640,16 +646,15 @@ private static dynamic IsInvalid(Guid id) => new
640646
Message = "Id is invalid"
641647
};
642648

643-
private void Validate(params (dynamic Rule, string Parameter)[] validations)
649+
private void Validate(string message, params (dynamic Rule, string Parameter)[] validations)
644650
{
645-
var invalidStudentException = new InvalidStudentException(
646-
message: "Student is invalid. Please fix the errors and try again.");
651+
var invalidStudentException = new InvalidStudentException(message);
647652

648653
foreach((dynamic rule, string parameter) in validations)
649654
{
650655
if(rule.Condition)
651656
{
652-
invalidStudentException.UpsertDataList(parameter, rule.Message);
657+
invalidStudentException.UpsertDataList(key: parameter, value: rule.Message);
653658
}
654659
}
655660

@@ -902,6 +907,7 @@ Just like we did in the structural validations section, we are going to validate
902907
ValidateStudentIsNotNull(student);
903908

904909
Validate(
910+
message: "Student is invalid. Please fix the errors and try again.",
905911
(Rule: IsInvalid(student.Id), Parameter: nameof(Student.Id)),
906912
(Rule: IsInvalid(student.Name), Parameter: nameof(Student.Name)),
907913
(Rule: IsInvalid(student.CreatedBy), Parameter: nameof(Student.CreatedBy)),
@@ -1012,11 +1018,9 @@ private async ValueTask<(bool IsNotRecent, DateTimeOffset StartDate, DateTimeOff
10121018
```
10131019
#### Finally our `Validate` method would look like this:
10141020
```csharp
1015-
private static void Validate(params (dyanamic Rule, string Parameter)[] validations)
1021+
private static void Validate(string message, params (dynamic Rule, string Parameter)[] validations)
10161022
{
1017-
var invalidStudentException =
1018-
new InvalidStudentException(
1019-
message: "Student is invalid. Please fix the errors and try again.");
1023+
var invalidStudentException = new InvalidStudentException(message);
10201024

10211025
foreach((dynamic rule, string parameter) in validations)
10221026
{

0 commit comments

Comments
 (0)