Skip to content

Commit ec5221c

Browse files
authored
Merge pull request #37 from phmatray/codex/find-and-fix-a-critical-bug
Fix required validator to handle boolean values correctly
2 parents 3269118 + 4775c0a commit ec5221c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

FormCraft.UnitTests/Validators/RequiredValidatorTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ public class RequiredValidatorTests
55
private readonly RequiredValidator<TestModel, string> _stringValidator;
66
private readonly RequiredValidator<TestModel, int> _intValidator;
77
private readonly RequiredValidator<TestModel, int?> _nullableIntValidator;
8+
private readonly RequiredValidator<TestModel, bool> _boolValidator;
9+
private readonly RequiredValidator<TestModel, bool?> _nullableBoolValidator;
810
private readonly IServiceProvider _services;
911

1012
public RequiredValidatorTests()
1113
{
1214
_stringValidator = new RequiredValidator<TestModel, string>("Field is required");
1315
_intValidator = new RequiredValidator<TestModel, int>("Field is required");
1416
_nullableIntValidator = new RequiredValidator<TestModel, int?>("Field is required");
17+
_boolValidator = new RequiredValidator<TestModel, bool>("Field is required");
18+
_nullableBoolValidator = new RequiredValidator<TestModel, bool?>("Field is required");
1519
_services = A.Fake<IServiceProvider>();
1620
}
1721

@@ -99,6 +103,76 @@ public async Task ValidateAsync_Int_Should_Pass_When_NonZero()
99103
result.ErrorMessage.ShouldBeNull();
100104
}
101105

106+
[Fact]
107+
public async Task ValidateAsync_Bool_Should_Fail_When_False()
108+
{
109+
// Arrange
110+
var model = new TestModel();
111+
112+
// Act
113+
var result = await _boolValidator.ValidateAsync(model, false, _services);
114+
115+
// Assert
116+
result.IsValid.ShouldBeFalse();
117+
result.ErrorMessage.ShouldBe("Field is required");
118+
}
119+
120+
[Fact]
121+
public async Task ValidateAsync_Bool_Should_Pass_When_True()
122+
{
123+
// Arrange
124+
var model = new TestModel();
125+
126+
// Act
127+
var result = await _boolValidator.ValidateAsync(model, true, _services);
128+
129+
// Assert
130+
result.IsValid.ShouldBeTrue();
131+
result.ErrorMessage.ShouldBeNull();
132+
}
133+
134+
[Fact]
135+
public async Task ValidateAsync_NullableBool_Should_Fail_When_Null()
136+
{
137+
// Arrange
138+
var model = new TestModel();
139+
140+
// Act
141+
var result = await _nullableBoolValidator.ValidateAsync(model, null, _services);
142+
143+
// Assert
144+
result.IsValid.ShouldBeFalse();
145+
result.ErrorMessage.ShouldBe("Field is required");
146+
}
147+
148+
[Fact]
149+
public async Task ValidateAsync_NullableBool_Should_Fail_When_False()
150+
{
151+
// Arrange
152+
var model = new TestModel();
153+
154+
// Act
155+
var result = await _nullableBoolValidator.ValidateAsync(model, false, _services);
156+
157+
// Assert
158+
result.IsValid.ShouldBeFalse();
159+
result.ErrorMessage.ShouldBe("Field is required");
160+
}
161+
162+
[Fact]
163+
public async Task ValidateAsync_NullableBool_Should_Pass_When_True()
164+
{
165+
// Arrange
166+
var model = new TestModel();
167+
168+
// Act
169+
var result = await _nullableBoolValidator.ValidateAsync(model, true, _services);
170+
171+
// Assert
172+
result.IsValid.ShouldBeTrue();
173+
result.ErrorMessage.ShouldBeNull();
174+
}
175+
102176
[Fact]
103177
public async Task ValidateAsync_NullableInt_Should_Fail_When_Null()
104178
{

FormCraft/Forms/Validators/RequiredValidator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public Task<ValidationResult> ValidateAsync(TModel model, TValue value, IService
4444
{
4545
null => false,
4646
string str => !string.IsNullOrWhiteSpace(str),
47+
bool b => b,
4748
System.Collections.IEnumerable enumerable => enumerable.Cast<object>().Any(),
4849
_ => true
4950
};

0 commit comments

Comments
 (0)