Skip to content

Commit 3df31b8

Browse files
committed
ScheduledDate should be in the future
1 parent 3b270d8 commit 3df31b8

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public bool ShouldUpdateDate
8888
}
8989
}
9090

91+
[FutureDateValidation]
9192
public DateTime? ScheduledPublishDate
9293
{
9394
get => scheduledPublishDate;

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/FallbackUrlValidationAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
55

66
[AttributeUsage(AttributeTargets.Property)]
7-
public class FallbackUrlValidationAttribute : ValidationAttribute
7+
public sealed class FallbackUrlValidationAttribute : ValidationAttribute
88
{
99
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
1010
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
5+
6+
[AttributeUsage(AttributeTargets.Property)]
7+
public sealed class FutureDateValidationAttribute : ValidationAttribute
8+
{
9+
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
10+
{
11+
if (value is null)
12+
{
13+
return ValidationResult.Success;
14+
}
15+
16+
return (DateTime)value <= DateTime.UtcNow
17+
? new ValidationResult("The scheduled publish date must be in the future.")
18+
: ValidationResult.Success;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
5+
6+
namespace LinkDotNet.Blog.UnitTests.Web.Features.Admin.BlogPostEditor.Components;
7+
8+
public class FutureDateValidationAttributeTests
9+
{
10+
[Fact]
11+
public void GivenScheduledDateIsInThePast_WhenValidating_ThenError()
12+
{
13+
var model = new CreateNewModel
14+
{
15+
Title = "Title",
16+
ShortDescription = "Desc",
17+
Content = "Content",
18+
IsPublished = false,
19+
ScheduledPublishDate = DateTime.Now.AddDays(-1),
20+
PreviewImageUrl = "https://steven-giesel.com",
21+
};
22+
var validationContext = new ValidationContext(model);
23+
var results = new List<ValidationResult>();
24+
25+
var result = Validator.TryValidateObject(model, validationContext, results, true);
26+
27+
result.Should().BeFalse();
28+
results.Count.Should().Be(1);
29+
}
30+
}

0 commit comments

Comments
 (0)