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

New Post: Pre Validating Property in class

$
0
0
No, this isn't something that's doable with FluentValidation.

FluentValidation is designed to validate pre-populated object instances, typically view-models that directly represent the input that the user has entered in your UI. You'd then validate the object as a whole, which gives the advantage of being able to do cross-property validation for example.

The approach you're trying to achieve looks like the type of validation that you'd more typically apply to a domain entity, which shouldn't even be allowed to exist in an invalid state (hence the guard checks within the property setters). These are two different approaches to validation which you'd typically apply to different type of objects...FluentValidation isn't designed to work with the latter, so if this is the approach you want then you'd probably be better off using a different library for validation.

Jeremy

New Post: Pre Validating Property in class

$
0
0
Thank you for a detailed explanation Jeremy.

You're right, this is a domain entity validation in classes that also implement IDataErrorInfo and IValidatableObject interfaces to support Entity Framework and WinForms validation out of the box. I was able to hook up both interface implementations to work nicely with Fluent Validate, "only" problem was validating before property is actually changed.

I'm currently doing all the validation manually inside classes without any framework and without possibility to inject different validators.
If someone has some suggestion of some other validation frameworks please let me know.

Thank you for a excellent library Jeremy.
Keep up the good work.

Commented Issue: NuGet FluentValidation.Mvc does not have a stong name [7128]

$
0
0
I am unable to sign my project because FluentValidation.Mvc doesn't have a strong name. You should sign the libraries you publish on NuGet like everyone else otherwise they can't be used in projects with signed assemblies.
Comments: Only a trusted person signs and publishes to NuGet. The SNK does not belong in version control at all. Signing a build and publishing it to NuGet should be a completely independent step. I've never encountered an unsigned package in NuGet before.

Created Issue: OverridePropertyName is ignored with SetCollectionValidator [7129]

$
0
0
ValidationResult.PropertyName is generally affected by .OverridePropertyName, except where there is a collection validator. In these cases, ValidationResult.PropertyName still matches the (indexed) collection property name.

For example:

```
RuleFor(ow => ow.FineComponentCollection)
.Must((ow, coll) => coll.Any(fc => fc.Amount > 0))
.When(ow => ow.FineComponentCollection.Count > 0)
.OverridePropertyName("Test") // this works
.WithMessage("At least one non-zero monetary amount must be specified.");

RuleFor(ow => ow.FineComponentCollection)
.SetCollectionValidator(OutcomeFineComponentValidator.GetValidator(isNewEntity))
.OverridePropertyName("Test"); // this doesn't work
```
In the latter case, my ValidationResult.PropertyName still ends up as something like "FineComponentCollection[2].Amount" instead of "Test[2].Amount"

Edited Issue: OverridePropertyName is ignored in ValidationResult.PropertyName path when used with SetValidator and SetCollectionValidator [7129]

$
0
0
ValidationResult.PropertyName is generally affected by .OverridePropertyName, except when used with a navigation property with a sub-validator set. In these cases, the first portion of the ValidationResult.PropertyName path still matches the navigation property name, indexed in the case of a collection.

For example:
```
RuleFor(ow => ow.FineComponentCollection)
.Must((ow, coll) => coll.Any(fc => fc.Amount > 0))
.When(ow => ow.FineComponentCollection.Count > 0)
.OverridePropertyName("Test") // this works
.WithMessage("At least one non-zero monetary amount must be specified.");

RuleFor(ow => ow.FineComponentCollection)
.SetCollectionValidator(OutcomeFineComponentValidator.GetValidator(isNewEntity))
.OverridePropertyName("Test"); // this doesn't work
```
In the latter case, my ValidationResult.PropertyName still ends up as something like "FineComponentCollection[2].Amount" instead of "Test[2].Amount".

Source code checked in, #14352c9906e3

$
0
0
Fix issue where OverridePropertyName didn't work with SetValidator/SetCollectionValidator

Commented Issue: OverridePropertyName is ignored in ValidationResult.PropertyName path when used with SetValidator and SetCollectionValidator [7129]

$
0
0
ValidationResult.PropertyName is generally affected by .OverridePropertyName, except when used with a navigation property with a sub-validator set. In these cases, the first portion of the ValidationResult.PropertyName path still matches the navigation property name, indexed in the case of a collection.

For example:
```
RuleFor(ow => ow.FineComponentCollection)
.Must((ow, coll) => coll.Any(fc => fc.Amount > 0))
.When(ow => ow.FineComponentCollection.Count > 0)
.OverridePropertyName("Test") // this works
.WithMessage("At least one non-zero monetary amount must be specified.");

RuleFor(ow => ow.FineComponentCollection)
.SetCollectionValidator(OutcomeFineComponentValidator.GetValidator(isNewEntity))
.OverridePropertyName("Test"); // this doesn't work
```
In the latter case, my ValidationResult.PropertyName still ends up as something like "FineComponentCollection[2].Amount" instead of "Test[2].Amount".
Comments: Thanks for reporting this, I've fixed it in the latest commit.

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: awesome man, thank you very much

Created Issue: EqualValidator and NotEqualValidator fail if property/value don't implement IComparable [7130]

$
0
0
New
Hi, in the latest (unstable) check in, usage of the new Internal.Comparer fails in these validators if they've been used for object type properties that don't implement IComparable. Suggest the Compare method of each is changed to fall back to default equality comparison when this occurs:

```
protected bool Compare(object comparisonValue, object propertyValue)
{
if (comparer != null)
{
return comparer.Equals(comparisonValue, propertyValue);
}

if (comparisonValue is IComparable && propertyValue is IComparable)
{
return Internal.Comparer.GetEqualsResult((IComparable)comparisonValue, (IComparable)propertyValue);
}

return comparisonValue == propertyValue;
}
```

Or you could use Object.ReferenceEquals and/or use reflection(?) to find the generic EqualityComparer<T>.Default for the property being examined.

Source code checked in, #6179600fe393

$
0
0
Bug fix wwhen comparing non IComparables in EqualNotEqual validator

Commented Issue: EqualValidator and NotEqualValidator fail if property/value don't implement IComparable [7130]

$
0
0
New
Hi, in the latest (unstable) check in, usage of the new Internal.Comparer fails in these validators if they've been used for object type properties that don't implement IComparable. Suggest the Compare method of each is changed to fall back to default equality comparison when this occurs:

```
protected bool Compare(object comparisonValue, object propertyValue)
{
if (comparer != null)
{
return comparer.Equals(comparisonValue, propertyValue);
}

if (comparisonValue is IComparable && propertyValue is IComparable)
{
return Internal.Comparer.GetEqualsResult((IComparable)comparisonValue, (IComparable)propertyValue);
}

return comparisonValue == propertyValue;
}
```

Or you could use Object.ReferenceEquals and/or use reflection(?) to find the generic EqualityComparer<T>.Default for the property being examined.
Comments: Implemented in latest commit

Created Issue: Problem with a Must overload. [7131]

$
0
0
I'm having trouble using the following overload:
```
Must<T, TProperty>(Func<TProperty, bool> predicate)
```

The presence of the generic T in the method definition prevents the concise usage such as:
```
RuleFor(x => x.Title).Must(s => s.Length == 0 || string.IsNullOrWhiteSpace(s));
```
Instead I must type:
```
RuleFor(x => x.Title).Must((string s) => s.Length == 0 || string.IsNullOrWhiteSpace(s));
```

Edited Issue: Problem with a Must overload. [7131]

$
0
0
I'm having trouble using the following overload:
```
Must<T, TProperty>(Func<TProperty, bool> predicate)
```

The presence of the generic T in the method definition prevents concise usage such as:
```
RuleFor(x => x.Title).Must(s => s.Length == 0 || string.IsNullOrWhiteSpace(s));
```
Instead I must type:
```
RuleFor(x => x.Title).Must((string s) => s.Length == 0 || string.IsNullOrWhiteSpace(s));
```

Edited Issue: Problem with a Must overload. [7131]

$
0
0
I'm having trouble using the following overload:
```
Must<T, TProperty>(Func<TProperty, bool> predicate)
```

The presence of the generic T in the method definition prevents concise usage such as:
```
RuleFor(x => x.Title).Must(s => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
Instead I must type:
```
RuleFor(x => x.Title).Must((string s) => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```

Edited Issue: Problem with a Must overload. [7131]

$
0
0
I'm having trouble using the following overload:
```
Must<T, TProperty>(Func<TProperty, bool> predicate)
```

The presence of the generic T in the method definition prevents concise usage such as:
```
RuleFor(x => x.Title).Must(s => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
Instead I must type:
```
RuleFor(x => x.Title).Must((string s) => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
The class being validated T is not used anywhere in the predicate and can be removed from the method signature.

Edited Issue: Problem with a Must overload. [7131]

$
0
0
I'm having trouble using the following overload:
```
Must<T, TProperty>(Func<TProperty, bool> predicate)
```

The presence of the generic T in the method definition prevents concise usage such as:
```
RuleFor(x => x.Title).Must(s => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
Instead I must type:
```
RuleFor(x => x.Title).Must((string s) => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
The class being validated (T) is not used anywhere in the predicate and can be removed from the method signature.

Commented Issue: Problem with a Must overload. [7131]

$
0
0
I'm having trouble using the following overload:
```
Must<T, TProperty>(Func<TProperty, bool> predicate)
```

The presence of the generic T in the method definition prevents concise usage such as:
```
RuleFor(x => x.Title).Must(s => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
Instead I must type:
```
RuleFor(x => x.Title).Must((string s) => !string.IsNullOrWhiteSpace(s) || s.Length == 0);
```
The class being validated (T) is not used anywhere in the predicate and can be removed from the method signature.
Comments: I'm not able to reproduce this problem. The type inference takes care of this so there is no need to explicitly specify the type. The following compiles just fine using FluentValidation 3.4.6: ``` public class Demo { public string Title { get; set; } } public class DemoValidator : AbstractValidator<Demo> { public DemoValidator() { RuleFor(x => x.Title).Must(s => !string.IsNullOrWhiteSpace(s) || s.Length == 0); } } ``` If you're running the latest version, please put together a small sample that reproduces the problem. Thanks.

Created Issue: Polish message "inclusivebetween_error" [7132]

$
0
0
In Messages.pl.resx is:
<data name="inclusivebetween_error" xml:space="preserve">
<value> '{PropertyName}' musi się zawierać pomiędzy {From} i {To}. Wprowadzono {Value}.</value>
</data>
It should be:
<data name="inclusivebetween_error" xml:space="preserve">
<value>'{PropertyName}' musi się zawierać pomiędzy {From} i {To}. Wprowadzono {Value}.</value>
</data>
without ' ' (space).

Closed Issue: Polish message "inclusivebetween_error" [7132]

$
0
0
In Messages.pl.resx is:
<data name="inclusivebetween_error" xml:space="preserve">
<value> '{PropertyName}' musi się zawierać pomiędzy {From} i {To}. Wprowadzono {Value}.</value>
</data>
It should be:
<data name="inclusivebetween_error" xml:space="preserve">
<value>'{PropertyName}' musi się zawierać pomiędzy {From} i {To}. Wprowadzono {Value}.</value>
</data>
without ' ' (space).
Comments: Fixed in latest commit

Source code checked in, #8762849c42e2

$
0
0
Fix spacing in Polish translation
Viewing all 1917 articles
Browse latest View live


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