Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.66k stars 95 forks source link

[Question] Dependent - Attribute Based Validations? #143

Closed Simonl9l closed 2 months ago

Simonl9l commented 2 months ago

Per the Readme section on this, is there a way to create a custom validation that can validate based on the value of another option/argument?

Is there an example, or could the documentation be updated to demonstrate this?

There is potential and example here that validates across properties of a class, but given that this Framework does not use class for the Options/Arguments but method parameters, I'm not sure how this can work.

neuecc commented 2 months ago

If you implement ValidationAttribute, it will work without any issues. This is not specific to this framework, but a general approach, so please research it on your own.

Simonl9l commented 2 months ago

Whilst outside of the scope on Asp.Net Core once can use the ValidationAttribute as soon as you want to do things like access other dependent arguments, and services from the DI container to complete validations, all bets seem to be off.

Also the ValidationContext that is needed (it seems) to support this is littered with [RequiresUnreferencedCode] attributes that make it non AOT-able.

I guess I'll just do validation inside the command, for now, but do note that both .Net Core and C# have threads on providing code generation support for parameter validations: https://github.com/dotnet/aspnetcore/issues/46349 https://github.com/dotnet/csharplang/discussions/6373