-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathValidationOptions.cs
More file actions
128 lines (114 loc) · 4.06 KB
/
Copy pathValidationOptions.cs
File metadata and controls
128 lines (114 loc) · 4.06 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
namespace NpgsqlRest;
/// <summary>
/// Options for parameter validation rules that can be referenced in comment annotations.
/// </summary>
public class ValidationOptions
{
/// <summary>
/// Named validation rules that can be referenced in comment annotations using "validate _param using rule_name" syntax.
/// </summary>
public Dictionary<string, ValidationRule> Rules { get; set; } = new()
{
["not_null"] = new ValidationRule
{
Type = ValidationType.NotNull,
Message = "Parameter '{0}' cannot be null"
},
["not_empty"] = new ValidationRule
{
Type = ValidationType.NotEmpty,
Message = "Parameter '{0}' cannot be empty"
},
["required"] = new ValidationRule
{
Type = ValidationType.Required,
Message = "Parameter '{0}' is required"
},
["email"] = new ValidationRule
{
Type = ValidationType.Regex,
Pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$",
Message = "Parameter '{0}' must be a valid email address"
}
};
}
/// <summary>
/// Defines a validation rule that can be applied to endpoint parameters.
/// </summary>
public class ValidationRule
{
/// <summary>
/// The type of validation to perform.
/// </summary>
public ValidationType Type { get; set; }
/// <summary>
/// Regular expression pattern for Regex validation type.
/// </summary>
public string? Pattern { get; set; }
/// <summary>
/// Minimum length for MinLength validation type.
/// </summary>
public int? MinLength { get; set; }
/// <summary>
/// Maximum length for MaxLength validation type.
/// </summary>
public int? MaxLength { get; set; }
/// <summary>
/// Error message returned when validation fails.
/// Supports the following placeholders:
/// <list type="bullet">
/// <item><description>{0} - Original PostgreSQL parameter name (e.g., "_email")</description></item>
/// <item><description>{1} - Converted parameter name (e.g., "email")</description></item>
/// <item><description>{2} - Validation rule name (e.g., "not_empty")</description></item>
/// </list>
/// Example: "Parameter '{1}' failed validation rule '{2}'" produces "Parameter 'email' failed validation rule 'not_empty'"
/// </summary>
public string Message { get; set; } = "Validation failed for parameter '{0}'";
/// <summary>
/// HTTP status code to return when validation fails. Default is 400 (Bad Request).
/// </summary>
public int StatusCode { get; set; } = 400;
public override string ToString()
{
return Type switch
{
ValidationType.NotNull => $"NotNull (StatusCode: {StatusCode})",
ValidationType.NotEmpty => $"NotEmpty (StatusCode: {StatusCode})",
ValidationType.Required => $"Required (StatusCode: {StatusCode})",
ValidationType.Regex => $"Regex: {Pattern} (StatusCode: {StatusCode})",
ValidationType.MinLength => $"MinLength: {MinLength} (StatusCode: {StatusCode})",
ValidationType.MaxLength => $"MaxLength: {MaxLength} (StatusCode: {StatusCode})",
_ => $"Unknown (StatusCode: {StatusCode})"
};
}
}
/// <summary>
/// Types of validation that can be performed on parameters.
/// </summary>
public enum ValidationType
{
/// <summary>
/// Parameter value cannot be null (DBNull.Value).
/// </summary>
NotNull,
/// <summary>
/// Parameter value cannot be an empty string.
/// </summary>
NotEmpty,
/// <summary>
/// Parameter value cannot be null or empty string. Combines NotNull and NotEmpty.
/// </summary>
Required,
/// <summary>
/// Parameter value must match the specified regular expression pattern.
/// </summary>
Regex,
/// <summary>
/// Parameter value must have at least MinLength characters.
/// </summary>
MinLength,
/// <summary>
/// Parameter value must have at most MaxLength characters.
/// </summary>
MaxLength
}