SteveDunn / Vogen

A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Apache License 2.0
877 stars 45 forks source link

respect data annotations #53

Open dzmitry-lahoda opened 2 years ago

dzmitry-lahoda commented 2 years ago
[ValueObject(typeof(int))]
[System.ComponentModel.DataAnnotations.Range(2,100, Error = "Generate validator from this. Hm. Even compile time validator for well know annotations"]
public partial struct CustomerId {
}
Herdo commented 2 years ago

From the existing attributes, these might be relevant (ordered descending by usefulness, IMO):

dzmitry-lahoda commented 2 years ago

at least one example can be handy to provide patches for others:) Range is good target.

SteveDunn commented 2 weeks ago

Thanks for the suggestion @dzmitry-lahoda . Perhaps this would be better as a field, which would allow fluent chaining to combine validators. Vogen could then recognise and parse those fields and generate the appropriate validation rules from them. I particularly like your idea of using well known validators at compile time! 👍

dzmitry-lahoda commented 2 weeks ago

Hello, than you for continue to improve C#, so I moved to Rust.

What I found so, Rust attribute proc macro is very close to C# source generators.

I feel that you may find very solid designs in Rust crates for many things C# capable of.

In Rust, unfortunately, there is no generic standard validation attributes. Each crate clap/serde/prost do their own validations. So serde seems mostly shared (schemars uses serde). May be some of these design can be good to look at.

May be not exactly relevant, but C# can also be extended with idiomatic partial application and type state builders like this thing does https://github.com/elastio/bon .

So I believe if anything in C# existing validation attributes prevents some forms of being statically typed or some other limitation, it should be avoid and some specialization used (for example JSON validation composable with Vogen is interesting) or builder like validation (via type state). Because there is some well known success doing so in similar language.

SteveDunn commented 2 weeks ago

Thanks, I'll take a look around some of these!