//----------------------------------------------------------------------- // // Copyright (c) lanedirt. All rights reserved. // Licensed under the MIT license. See LICENSE.md file in the project root for full license information. // //----------------------------------------------------------------------- namespace AliasVault.Client.Main.Models.FormValidation; using System.ComponentModel.DataAnnotations; using System.Globalization; /// /// Model validation attribute for date strings. /// /// /// Initializes a new instance of the class. /// /// The date format to validate. public class StringDateFormatAttribute(string format) : ValidationAttribute { /// /// Check if the date string is in the correct format. /// /// The field value. /// ValidationContext. /// ValidationResult. protected override ValidationResult IsValid(object? value, ValidationContext validationContext) { if (value is string dateString && DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out _)) { return ValidationResult.Success!; } return new ValidationResult($"The date must be in the format {format}.", [validationContext.MemberName!]); } }