valit-stack / Valit

Valit is dead simple validation for .NET Core. No more if-statements all around your code. Write nice and clean fluent validators instead!
MIT License
322 stars 26 forks source link

Validation for reference types seems nonfunctional #164

Closed Wintereise closed 6 years ago

Wintereise commented 6 years ago

Very simple test class:

    public class ValitTestModel
    {
        public bool testAttributeA;
        public ushort testAttributeB;

        public bool validate()
        {
            var validator = ValitRules<ValitTestModel>
                .Create()
                .Ensure(m => m.testAttributeA, _ => _
                    .Required())
                .Ensure(m => m.testAttributeB, _ => _
                    .Required())
                .For(this)
                .Validate();

            return validator.Succeeded;
        }
    }

The error returned is that The type <bool|ushort> must be a reference type to use it as parameter TProperty for ValitRulePropertyExtensions.Required<TObject, TProperty> where both are defined as classes. C# assumes these to be structs, so it doesn't work.

I did notice however that bool and a few other types have type specific extension methods defined, how do we go about using them?

Wintereise commented 6 years ago

It works if the fields are made nullable, which is what we ended up doing.