Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to suport FluentValidation 9.x.x. #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
<PackageReference Include="AzureFromTheTrenches.Commanding" Version="9.0.1" />
<PackageReference Include="AzureFromTheTrenches.Commanding.Abstractions" Version="9.0.2" />
<PackageReference Include="FSharp.Core" Version="4.7.0" />
<PackageReference Include="FluentValidation" Version="8.5.0" />
<PackageReference Include="FluentValidation" Version="9.1.3" />
<PackageReference Include="Handlebars.Net" Version="1.10.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.5" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.0" />
<PackageReference Include="FluentValidation" Version="9.1.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public ValidationResult Validate(object command)
return new ValidationResult();
}

var validationResult = validator.Validate(command);
var context = new ValidationContext<object>(command);
var validationResult = validator.Validate(context);

return new ValidationResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,31 @@ private void AddRulesFromIncludedValidators(OpenApiSchema schema, IOpenApiSchema
{
var childAdapters = (validator as IEnumerable<IValidationRule>)
.NotNull()
.OfType<IncludeRule>()
.OfType<PropertyRule>()
.Where(includeRule => includeRule.Condition == null && includeRule.AsyncCondition == null)
.SelectMany(includeRule => includeRule.Validators)
.OfType<ChildValidatorAdaptor>();
.OfType<IChildValidatorAdaptor>();

foreach (var adapter in childAdapters)
{
var propertyValidatorContext = new PropertyValidatorContext(new ValidationContext(null), null, string.Empty);
var includeValidator = adapter.GetValidator(propertyValidatorContext);
var propertyValidatorContext = new PropertyValidatorContext(new ValidationContext<object>(null), null, string.Empty);
var includeValidator = GetValidatorFromChildValidatorAdapter(adapter, propertyValidatorContext);
ApplyRulesToSchema(schema, schemaFilterContext, includeValidator);
AddRulesFromIncludedValidators(schema, schemaFilterContext, includeValidator);
}
}

private IValidator GetValidatorFromChildValidatorAdapter(IChildValidatorAdaptor childValidatorAdapter, PropertyValidatorContext propertyValidatorContext)
{
var childValidatorAdapterType = childValidatorAdapter.GetType();
var getValidatorMethod = childValidatorAdapterType.GetMethod(nameof(ChildValidatorAdaptor<object, object>.GetValidator));
if (getValidatorMethod != null)
{
var validator = (IValidator)getValidatorMethod.Invoke(childValidatorAdapter, new[] { propertyValidatorContext });
return validator;
}

return null;
}
}
}