Skip to content

Commit 0f8f119

Browse files
committed
Added min and max length validators
1 parent 3b03d95 commit 0f8f119

4 files changed

Lines changed: 183 additions & 3 deletions

File tree

src/MADE.Data.Validation/Strings/Resources.Designer.cs

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MADE.Data.Validation/Strings/Resources.resx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<root>
33
<!--
44
Microsoft ResX Schema
@@ -132,16 +132,22 @@
132132
<data name="IpAddressValidator_FeedbackMessage" xml:space="preserve">
133133
<value>The value must be a valid IP address, e.g. 192.168.0.1.</value>
134134
</data>
135+
<data name="MaxLengthValidator_FeedbackMessage" xml:space="preserve">
136+
<value>The length must be less than {0}.</value>
137+
</data>
135138
<data name="MaxValueValidator_FeedbackMessage" xml:space="preserve">
136139
<value>The value must be less than {0}.</value>
137140
</data>
141+
<data name="MinLengthValidator_FeedbackMessage" xml:space="preserve">
142+
<value>The length must be greater than {0}.</value>
143+
</data>
138144
<data name="MinValueValidator_FeedbackMessage" xml:space="preserve">
139145
<value>The value must be greater than {0}.</value>
140146
</data>
141147
<data name="RegexValidator_FeedbackMessage" xml:space="preserve">
142148
<value>The value does not match the valid mask.</value>
143149
</data>
144150
<data name="RequiredValidator_FeedbackMessage" xml:space="preserve">
145-
<value>Please fill out this field.</value>
151+
<value>A value is required.</value>
146152
</data>
147153
</root>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
namespace MADE.Data.Validation.Validators
2+
{
3+
using System;
4+
using System.Collections;
5+
using MADE.Data.Validation;
6+
using MADE.Data.Validation.Extensions;
7+
using MADE.Data.Validation.Strings;
8+
9+
/// <summary>
10+
/// Defines a data validator for ensuring a value is less than a maximum length.
11+
/// </summary>
12+
public class MaxLengthValidator : IValidator
13+
{
14+
private string feedbackMessage;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="MaxLengthValidator"/> class.
18+
/// </summary>
19+
public MaxLengthValidator()
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="MaxLengthValidator"/> class with a maximum value.
25+
/// </summary>
26+
/// <param name="max">The maximum value.</param>
27+
public MaxLengthValidator(IComparable max)
28+
{
29+
this.Max = max;
30+
}
31+
32+
/// <summary>
33+
/// Gets or sets the key associated with the validator.
34+
/// </summary>
35+
public string Key { get; set; } = nameof(MaxLengthValidator);
36+
37+
/// <summary>
38+
/// Gets or sets a value indicating whether the data provided is in an invalid state.
39+
/// </summary>
40+
public bool IsInvalid { get; set; }
41+
42+
/// <summary>
43+
/// Gets or sets a value indicating whether the data is dirty.
44+
/// </summary>
45+
public bool IsDirty { get; set; }
46+
47+
/// <summary>
48+
/// Gets or sets the feedback message to display when <see cref="IValidator.IsInvalid"/> is true.
49+
/// </summary>
50+
public string FeedbackMessage
51+
{
52+
get => this.feedbackMessage.IsNullOrWhiteSpace() ? string.Format(Resources.MaxLengthValidator_FeedbackMessage, this.Max) : this.feedbackMessage;
53+
set => this.feedbackMessage = value;
54+
}
55+
56+
/// <summary>
57+
/// Gets or sets the maximum value.
58+
/// </summary>
59+
public IComparable Max { get; set; }
60+
61+
/// <summary>
62+
/// Executes data validation on the provided <paramref name="value"/>.
63+
/// </summary>
64+
/// <param name="value">The value to be validated.</param>
65+
public void Validate(object value)
66+
{
67+
bool isInvalid = value switch
68+
{
69+
string str => str.Length.IsGreaterThan(this.Max),
70+
ICollection col => col.Count.IsGreaterThan(this.Max),
71+
_ => true
72+
};
73+
74+
this.IsInvalid = isInvalid;
75+
this.IsDirty = true;
76+
}
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
namespace MADE.Data.Validation.Validators
2+
{
3+
using System;
4+
using System.Collections;
5+
using MADE.Data.Validation;
6+
using MADE.Data.Validation.Extensions;
7+
using MADE.Data.Validation.Strings;
8+
9+
/// <summary>
10+
/// Defines a data validator for ensuring a value is greater than a minimum length.
11+
/// </summary>
12+
public class MinLengthValidator : IValidator
13+
{
14+
private string feedbackMessage;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="MinLengthValidator"/> class.
18+
/// </summary>
19+
public MinLengthValidator()
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="MinLengthValidator"/> class with a minimum value.
25+
/// </summary>
26+
/// <param name="min">The maximum value.</param>
27+
public MinLengthValidator(IComparable min)
28+
{
29+
this.Min = min;
30+
}
31+
32+
/// <summary>
33+
/// Gets or sets the key associated with the validator.
34+
/// </summary>
35+
public string Key { get; set; } = nameof(MinLengthValidator);
36+
37+
/// <summary>
38+
/// Gets or sets a value indicating whether the data provided is in an invalid state.
39+
/// </summary>
40+
public bool IsInvalid { get; set; }
41+
42+
/// <summary>
43+
/// Gets or sets a value indicating whether the data is dirty.
44+
/// </summary>
45+
public bool IsDirty { get; set; }
46+
47+
/// <summary>
48+
/// Gets or sets the feedback message to display when <see cref="IValidator.IsInvalid"/> is true.
49+
/// </summary>
50+
public string FeedbackMessage
51+
{
52+
get => this.feedbackMessage.IsNullOrWhiteSpace() ? string.Format(Resources.MinLengthValidator_FeedbackMessage, this.Min) : this.feedbackMessage;
53+
set => this.feedbackMessage = value;
54+
}
55+
56+
/// <summary>
57+
/// Gets or sets the minimum value.
58+
/// </summary>
59+
public IComparable Min { get; set; }
60+
61+
/// <summary>
62+
/// Executes data validation on the provided <paramref name="value"/>.
63+
/// </summary>
64+
/// <param name="value">The value to be validated.</param>
65+
public void Validate(object value)
66+
{
67+
bool isInvalid = value switch
68+
{
69+
string str => str.Length.IsLessThan(this.Min),
70+
ICollection col => col.Count.IsLessThan(this.Min),
71+
_ => true
72+
};
73+
74+
this.IsInvalid = isInvalid;
75+
this.IsDirty = true;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)