Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

Commented Issue: Validating dynamic attributes [7127]

$
0
0
hi,
awesome library, but could you tell me please how to validate the following
```
public class Product
{
public string Id { get; set; }
public List<Attribute> Attributes { get; set; }
}

public class Attribute
{
public string Name { get; set; }
public string Value { get; set; }
}
var person = new Person();
person.Attributes.Add("price","10");

```
how can i validate that price attribute is no more than 5 for example.

thanks in advanced.

Comments: You could define a custom rule against the Attributes property using a Must validator: ``` public class ProductValidator : AbstractValidator<Product> { public ProductValidator() { RuleFor(x => x.Attributes).Must(HaveValidPrice); } bool HaveValidPrice(List<Attribute> attributes) { var priceAttribute = attributes.Single(x => x.Name == "price"); return int.Parse(priceAttribute.Value) <= 5; } } ```

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>