aboutsummaryrefslogtreecommitdiff
path: root/back-end/src/Model/FeedMeta.cs
blob: 7cd788a00578859457e1569975f463f7e577e423 (plain)
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
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: CMNext
* Package: Content.Publishing.Blog.Admin
* File: FeedMeta.cs 
*
* CMNext is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* CMNext is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

using System.Text.Json.Serialization;
using System.Text.RegularExpressions;

using FluentValidation;

using VNLib.Plugins.Extensions.Validation;

namespace Content.Publishing.Blog.Admin.Model
{
    internal class FeedMeta : IBlogFeedContext
    {
        private static readonly Regex FileNameRegex = new(@"^[a-zA-Z0-9_.\-]+$", RegexOptions.Compiled);
        private static readonly Regex HttpUrlRegex = new(@"^https?://", RegexOptions.Compiled | RegexOptions.IgnoreCase);

        [JsonPropertyName("url")]
        public string PublihUrl { get; set; } = string.Empty;

        [JsonPropertyName("path")]
        public string FeedPath { get; set; } = string.Empty;

        [JsonPropertyName("image")]
        public string ImageUrl { get; set; } = string.Empty;

        [JsonPropertyName("description")]
        public string? Description { get; set; }

        [JsonPropertyName("maxItems")]
        public int? MaxItems { get; set; }

        [JsonPropertyName("author")]
        public string? Author { get; set; }

        [JsonPropertyName("contact")]
        public string? WebMaster { get; set; }

        [JsonPropertyName("properties")]
        public ExtendedProperty[]? ExtendedProperties { get; set; }


        public static IValidator<FeedMeta> GetValidator()
        {
            InlineValidator<FeedMeta> validator = new();

            validator.RuleFor(x => x.PublihUrl)
                .NotEmpty()
                .MaximumLength(200)
                .Matches(HttpUrlRegex)
                .WithMessage("Your feed url is not a valid http url");

            validator.RuleFor(x => x.FeedPath)
                .NotEmpty()
                .MaximumLength(200)
                .Must(static p => !p.StartsWith('/') && !p.StartsWith('\\'))
                .WithMessage("The feed file path must not contain a leading slash")
                .Matches(FileNameRegex)
                .WithMessage("The feed file name is not valid");

            validator.RuleFor(x => x.ImageUrl)
                .MaximumLength(200)
                .Must(static x => string.IsNullOrWhiteSpace(x) || HttpUrlRegex.IsMatch(x))
                .WithMessage("The image url is not a valid http url");

            validator.RuleFor(x => x.Description!)
                .NotEmpty()
                .IllegalCharacters()
                .MaximumLength(200);

            //Cap max items at 100
            validator.RuleFor(x => x.MaxItems)
                .InclusiveBetween(1, 100);

            validator.RuleFor(x => x.Author!)
                .SpecialCharacters()
                .MaximumLength(200);

            validator.RuleFor(x => x.WebMaster!)
                .IllegalCharacters()
                .MaximumLength(200);

            //Make sure the keys for each propery are valid
            validator.RuleForEach(x => x.ExtendedProperties)
                      .ChildRules(r => r.RuleFor(k => k.Name!).IllegalCharacters())
                      .When(x => x.ExtendedProperties != null);

            return validator;
        }
    }
}