using System.ComponentModel.DataAnnotations; namespace OnlyPrompt.Backend.ApiModels.Validators { public class NoWhitespaceAttribute : ValidationAttribute { public override bool IsValid(object? value) { if (value is string strValue) { if (strValue.Any(c => char.IsWhiteSpace(c))) return false; return true; } return true; // If it's not a string, we consider it valid. Use [NoWhitespace] only on string properties. } protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { if(value is string strValue) { if(strValue.Any(c => char.IsWhiteSpace(c))) return new ValidationResult($"{validationContext.DisplayName} should not contain any whitespace characters."); return ValidationResult.Success; } return ValidationResult.Success; // If it's not a string, we consider it valid. Use [NoWhitespace] only on string properties. } } }