-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (114 loc) · 4.2 KB
/
Program.cs
File metadata and controls
140 lines (114 loc) · 4.2 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
using System.ComponentModel.DataAnnotations;
namespace RequiredIf
{
class Program
{
static void Main(string[] args)
{
// Scenario 0: Condition met, Surname is null, Name is null (validation should fail)
Person test0 = new Person();
ValidateModel(test0);
// Scenario 1: Condition met, Surname is empty, Name is null (validation should fail)
Person test1 = new Person { Surname = "" };
ValidateModel(test1);
// Scenario 2: Condition met, Surname is empty, Name is set (validation should pass)
Person test2 = new Person
{
Surname = "",
Name = "John"
};
ValidateModel(test2);
// Scenario 3: Condition met, Surname is not empty, Name is null (validation should pass)
Person test3 = new Person
{
Surname = "Doe"
};
ValidateModel(test3);
// Scenario 4: Condition met, Surname is not empty, Name is set (validation should pass)
Person test4 = new Person
{
Surname = "Doe",
Name = "John"
};
ValidateModel(test4);
// Scenario 5: Condition not met, Surname is empty, Name is null (validation should pass)
TestWithoutCondition test5 = new TestWithoutCondition { Surname = "" };
ValidateModel(test5);
// Scenario 6: Condition not met, Surname is empty, Name is set (validation should pass)
Student teststudent1 = new Student
{
Surname = "",
Name = "John"
};
ValidateModel(teststudent1);
// Scenario 7: Condition not met, Surname is not empty, Name is null (validation should pass)
TestWithoutCondition test7 = new TestWithoutCondition
{
Surname = "Doe"
};
ValidateModel(test7);
// Scenario 8: Condition not met, Surname is not empty, Name is set (validation should pass)
NestedHierarchyTest test8 = new NestedHierarchyTest
{
Surname = "Doe",
Name = "John"
};
ValidateModel(test8);
// Scenario 8: Condition not met, Surname is not empty, Name is set (validation should pass)
Student teststudent2 = new Student
{
Surname = "Doe",
Name = "John",
StudentId = "HD723IKK"
};
ValidateModel(teststudent2);
Person test = new Person
{
Surname = " ", // Inválido
Name = null // Inválido porque Surname está vacío
};
Console.WriteLine("\nSINGLE VALIDATION TEST: \n.");
// Validar el modelo
test.ValidateSelf();
// Verificar errores
if (test.HasErrors)
{
foreach (var error in test.GetErrors(null))
{
Console.WriteLine($"Error: {error}");
}
}
else
{
Console.WriteLine("All validations passed.");
}
Console.WriteLine("Validation completed.");
}
private static void ValidateModel(object model)
{
Console.WriteLine($"Validating model: {model.GetType().Name}");
var context = new ValidationContext(model);
var results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(model, context, results, true);
if (isValid)
{
Console.WriteLine("Model is valid.");
}
else
{
Console.WriteLine("Model validation failed.");
foreach (var validationResult in results)
{
Console.WriteLine($" - {validationResult.ErrorMessage}");
}
}
Console.WriteLine();
}
}
public class TestWithoutCondition : Person
{
}
public class NestedHierarchyTest : Student
{
}
}