Skip to content

JSON serializer & custom validation

The generator detects which serializers are available and produces the corresponding converters:

SerializerGenerated converterRegistration
System.Text.JsonPatchFieldJsonConverterFactoryoptions.Converters.Add(new PatchFieldJsonConverterFactory())
Newtonsoft.JsonPatchFieldNewtonsoftConvertersettings.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.

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.

  • .NET 6+ (or .NET Framework with SDK-style projects and System.Text.Json NuGet)
  • required keyword needs C# 11 / .NET 7+ (optional — without it all properties are optional in Create)

MIT