Skip to content

Commit

Permalink
Fixed error when passed explicit Validator instance: System.InvalidOp…
Browse files Browse the repository at this point in the history
…erationException: Cannot validate instances of type 'ModelSubType'. This validator can only validate instances of type 'ModelType'.
  • Loading branch information
Liero committed Feb 21, 2024
1 parent 0ad8f73 commit 39564cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ public virtual void ClearMessages()
public virtual IValidator? ResolveValidator(FieldIdentifier fieldIdentifier = default)
{
if (EditContext == null) throw new InvalidOperationException("EditContext is null");
if (Validator != null) return Validator;

object model = fieldIdentifier.Model ?? EditContext.Model;
Type interfaceValidatorType = typeof(IValidator<>).MakeGenericType(model.GetType());
Type modelType = model.GetType();

if (Validator != null && Validator.CanValidateInstancesOfType(modelType))
{
return Validator;
}

Type interfaceValidatorType = typeof(IValidator<>).MakeGenericType(modelType);
var ctx = new ValidatorFactoryContext(interfaceValidatorType, ServiceProvider, EditContext, model, fieldIdentifier);
return ValidatorFactory.CreateValidator(ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

<EmbedUntrackedSources>true</EmbedUntrackedSources>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.1.2</Version>
<PackageVersion>1.1.2</PackageVersion>
<Version>1.1.3</Version>
<PackageVersion>1.1.3</PackageVersion>
<Authors>Liero; Daniel Turan</Authors>
<Company>vNext Software Consulting</Company>
<PackageId>vNext.BlazorComponents.FluentValidation</PackageId>
Expand Down

0 comments on commit 39564cf

Please sign in to comment.