luboshl / MiniValidationPlus

Validation library based on System.ComponentModel.DataAnnotations with non-nullable reference types support.
MIT License
0 stars 0 forks source link

Property with getter throwing error - option to swallow exceptions #20

Open luboshl opened 1 month ago

luboshl commented 1 month ago

Some types that are going to be validated can contain properties that throw an exception.

public class MyClass
{
    public string Name => throw new InvalidOperationException();
}

By default the behavior should be similar to validation in MVC.

luboshl commented 1 month ago

@Kraviecc, I think this explains your issue we discussed. Am I right?

luboshl commented 1 month ago

Quick investigation - MVC throws the error from the property with getter that throws an error. So we should add an option to swallow exceptions in property getters.

   at ......................
   at Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter[TDeclaringType,TValue](Func`2 getter, Object target)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry.get_Model()
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitImplementation(ModelMetadata& metadata, String& key, Object model)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, String prefix, Object model, ModelMetadata metadata, Object container)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.EnforceBindRequiredAndValidate(ObjectModelValidator baseObjectValidator, ActionContext actionContext, ParameterDescriptor parameter, ModelMetadata metadata, ModelBindingContext modelBindingContext, ModelBindingResult modelBindingResult, Object container)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value, Object container)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
Kraviecc commented 1 month ago

@Kraviecc, I think this explains your issue we discussed. Am I right?

Thanks, @luboshl. My issue comes from the fact that in our project we used Castle.Components.Validator library (https://github.com/carcer/Castle.Components.Validator/tree/master/src/Castle.Components.Validator) and we didn't want to manually mark lots of properties that weren't supposed to be validated (also these get-only) during migration to the new MiniValidation library.