AutoBogus icon indicating copy to clipboard operation
AutoBogus copied to clipboard

Convention to generate based on Data Annotations

Open jeremyhayes opened this issue 4 years ago • 5 comments

Could we leverage the Conventions support to inspect properties annotated with subclasses of ValidationAttribute?

  • https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validationattribute

This would provide a low-config way to generate instances which satisfy declarative model validations. A common pattern I see and use is to generate a random (valid) instance, then update one or more properties to exercise a particular validation.

Not all of these would fit well into a generic convention, but certainly some of the more agnostic would be fairly straight-forward:

  • [Required]
  • [Range]
  • [StringLength]
  • [MinLength]
  • [MaxLength]
  • [DataType] (and/or it's specific implementations)
  • others?

jeremyhayes avatar Sep 04 '20 16:09 jeremyhayes

Hey @jeremyhayes

Interesting concept. I will see what I can cook up.

Nick.

nickdodd79 avatar Sep 05 '20 00:09 nickdodd79

See also supported items on AutoFixture:

https://github.com/AutoFixture/AutoFixture/tree/master/Src/AutoFixture/DataAnnotations

This is important for creating valid objects (and extra important when writing to a database)

304NotModified avatar Dec 03 '21 22:12 304NotModified

I am also interested in the feature. Some other attributes that I would like to see supported on top of the ones already listed are attributes related to serialization. I feel like this would be super useful for generating fake data for web api's in particular. It might be a lot for the base library, but maybe:

  • AutoBogus.Serialization
    • AutoBogus.Serialization.Json.System
    • AutoBogus.Serialization.Json.Newtonsoft
    • AutoBogus.Serialization.Xml.System

Here is a non-exhaustive list of some example use cases:

ChristopherHaws avatar Feb 04 '23 02:02 ChristopherHaws

Very interested in this feature. We do a lot of testing using database contexts and have a rather large (but properly annotated) amount of entities. Currently this means that even though we've already annotated everything we'd still need to add rules duplicating these to the fakers and keep them updated if the data model changes for whatever reason.

DarkDibblez avatar Apr 18 '23 10:04 DarkDibblez

Very interested in this feature too, and completely agree with @DarkDibblez, it will be a big time saver!

gitown avatar Jan 18 '24 19:01 gitown