Initial commit
This commit is contained in:
commit
63ee71a19b
123 files changed
+11268
No files matched your search
@@ -0,0 +1,34 @@
|
||||
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.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user