英文:
How can I make a custom validation attribute in C# which validates if an `IEnumerable` contains distinct values?
问题
以下是您要翻译的代码部分:
public class Payload
{
[DistinctValues]
public IEnumerable<string> Names { get; set; }
}
public class DistinctValuesAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
//validation logic here
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//validation logic here
}
}
英文:
I want to be able to use the attribute in a ASP.NET Core Web API
to validate properties in payload objects. Here's an example of how I would want it to work.
public class Payload
{
[DistinctValues]
public IEnumerable<string> Names { get; set; }
}
From what I've seen, the custom attribute should inherit ValidationAttribute
and override the IsValid
method. Here's what I've got so far. I don't know how to create the validation logic.
public class DistinctValuesAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
//validation logic here
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//validation logic here
}
}
答案1
得分: 0
In this bool IsValid(object value)
method, to find whether the list contains any duplicate value, you can compare the count of distinct values against the total count of values.
values.Distinct().Count() == values.Count();
In this ValidationResult IsValid(object value, ValidationContext validationContext)
method,
When the count of values is not equal (fail validation), you should return an instance of ValidationResult
.
For success validation, you should return ValidationResult.Success
.
using System.Linq;
public class DistinctValuesAttribute : ValidationAttribute
{
public DistinctValuesAttribute()
{
ErrorMessage = $"{this.TypeId} contains duplicate value";
}
public DistinctValuesAttribute(string errorMessage)
{
ErrorMessage = errorMessage;
}
public override bool IsValid(object value)
{
IEnumerable<string> values = value as IEnumerable<string>;
if (values != null && values.Any())
{
return values.Distinct().Count() == values.Count();
}
return true;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!IsValid(value))
{
return new ValidationResult(this.ErrorMessage, new List<string> { this.TypeId.ToString() });
}
return ValidationResult.Success;
}
}
英文:
In this bool IsValid(object value)
method, to find whether the list contains any duplicate value, you can compare the count of distinct values against the total count of values.
values.Distinct().Count() == values.Count();
In this ValidationResult IsValid(object value, ValidationContext validationContext)
method,
When the count of values is not equal (fail validation), you should return an instance of ValidationResult
.
For success validation, you should return ValidationResult.Success
.
using System.Linq;
public class DistinctValuesAttribute : ValidationAttribute
{
public DistinctValuesAttribute()
{
ErrorMessage = $"{this.TypeId} contains duplicate value";
}
public DistinctValuesAttribute(string errorMessage)
{
ErrorMessage = errorMessage;
}
public override bool IsValid(object value)
{
IEnumerable<string> values = value as IEnumerable<string>;
if (values != null && values.Any())
{
return values.Distinct().Count() == values.Count();
}
return true;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!IsValid(value))
{
return new ValidationResult(this.ErrorMessage, new List<string> { this.TypeId.ToString() });
}
return ValidationResult.Success;
}
}
答案2
得分: 0
使用上一个答案的帮助,我成功找到了我所需的解决方案。
public class DistinctValuesAttribute : ValidationAttribute
{
public DistinctValuesAttribute()
{
ErrorMessage = "集合应包含不同的值";
}
public DistinctValuesAttribute(string errorMessage)
{
ErrorMessage = errorMessage;
}
public override bool IsValid(object value)
{
var values = value as IEnumerable<object>;
if (values != null && values.Any())
{
return values.Distinct().Count() == values.Count();
}
return true;
}
}
英文:
With the help of the previous answer, I managed to find a solution for what I needed.
public class DistinctValuesAttribute : ValidationAttribute
{
public DistinctValuesAttribute()
{
ErrorMessage = $"The collection should contain distinct values";
}
public DistinctValuesAttribute(string errorMessage)
{
ErrorMessage = errorMessage;
}
public override bool IsValid(object value)
{
var values = value as IEnumerable<object>;
if (values != null && values.Any())
{
return values.Distinct().Count() == values.Count();
}
return true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论