JSON serializer & custom validation
JSON serializer support
Section titled “JSON serializer support”The generator detects which serializers are available and produces the corresponding converters:
| Serializer | Generated converter | Registration |
|---|---|---|
| System.Text.Json | PatchFieldJsonConverterFactory | options.Converters.Add(new PatchFieldJsonConverterFactory()) |
| Newtonsoft.Json | PatchFieldNewtonsoftConverter | settings.Converters.Add(new PatchFieldNewtonsoftConverter()) |
Both are generated if both packages are referenced. No converter is auto-registered — you choose which one to use, or write your own.
Custom validation
Section titled “Custom validation”Simple validator
Section titled “Simple validator”Implement IDtoValidator<T> and point to it from the attribute:
public class MyCreateValidator : IDtoValidator<CreatePlayerRequest>{ public DtoValidationResult Validate(CreatePlayerRequest instance) { var result = new DtoValidationResult(); if (instance.Name.HasValue && instance.Name.Value.Length < 3) result.AddError("name", "must be at least 3 characters."); return result; }}
[CreateDto(Validator = typeof(MyCreateValidator))]public class Player { ... }When a validator is set, Validate() delegates entirely to it — the default generated rules are replaced.
FluentValidation
Section titled “FluentValidation”When FluentValidation is installed, the generator additionally produces:
FluentDtoValidator<T>— base class bridging FluentValidation withIDtoValidator<T>{RequestName}CreateBaseValidator— contains the generated required/null rules{RequestName}UpdateBaseValidator— contains the generated null rules
Inherit to extend:
public class MyCreateValidator : CreatePlayerRequestCreateBaseValidator{ public MyCreateValidator() { RuleFor(x => x.Name) .Must(f => !f.HasValue || f.Value.Length >= 3) .WithMessage("Name must be at least 3 characters."); }}
[CreateDto(Validator = typeof(MyCreateValidator))]public class Player { ... }Or start from scratch:
public class MyCreateValidator : FluentDtoValidator<CreatePlayerRequest>{ public MyCreateValidator() { // your rules only }}Related guides
Section titled “Related guides”- Full CRUD with SQLite — end-to-end project with
[CrudApi], relationships, Query DSL, observability, and PatchField tri-state demo - Modeling Relationships & Query DSL —
[OneToOne]/[OneToMany], dot-notation filtering, every DSL operator with SQL translations - PatchField Tri-State — the null-vs-missing problem and
PatchField<T>with pattern-matching handlers - Declarative Observability —
[Log]+[Trace]for structured logging and OpenTelemetry spans on CRUD stores
Requirements
Section titled “Requirements”- .NET 6+ (or .NET Framework with SDK-style projects and System.Text.Json NuGet)
requiredkeyword needs C# 11 / .NET 7+ (optional — without it all properties are optional in Create)
License
Section titled “License”MIT