-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateMovieRequestValidator.cs
More file actions
58 lines (46 loc) · 1.89 KB
/
Copy pathCreateMovieRequestValidator.cs
File metadata and controls
58 lines (46 loc) · 1.89 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
using FluentValidation;
using SmartMovieCatalog.Contracts.Movies;
namespace SmartMovieCatalog.Api.Features.Movies.CreateMovie;
public sealed class CreateMovieRequestValidator : AbstractValidator<CreateMovieRequest>
{
public CreateMovieRequestValidator()
{
RuleFor(request => request.Title)
.NotEmpty();
RuleFor(request => request.ReleaseYear)
.NotNull()
.InclusiveBetween(1888, DateTimeOffset.UtcNow.Year + 1);
RuleFor(request => request.CountryCode)
.NotEmpty()
.Must(countryCode => countryCode is not null &&
countryCode.Trim().Length == 2 &&
countryCode.Trim().All(char.IsLetter))
.WithMessage("Country code must contain exactly two letters.");
RuleFor(request => request.OriginalLanguage)
.NotEmpty();
RuleForEach(request => request.Genres)
.Must(genre => !string.IsNullOrWhiteSpace(genre))
.WithMessage("Genre must not be empty.");
RuleFor(request => request.DurationMinutes)
.GreaterThan(0)
.When(request => request.DurationMinutes.HasValue);
RuleFor(request => request.ExternalId)
.GreaterThan(0)
.When(request => request.ExternalId.HasValue);
RuleFor(request => request.Image)
.Must(IsRelativeImagePath)
.WithMessage("Image must be a relative path.")
.When(request => !string.IsNullOrWhiteSpace(request.Image));
}
private static bool IsRelativeImagePath(string? image)
{
if (image is null)
{
return false;
}
string trimmedImage = image.Trim();
return trimmedImage.StartsWith("/", StringComparison.Ordinal) &&
!trimmedImage.StartsWith("//", StringComparison.Ordinal) &&
!trimmedImage.Contains('\\', StringComparison.Ordinal);
}
}