AutoBogus
AutoBogus copied to clipboard
Convention to generate based on Data Annotations
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?
Hey @jeremyhayes
Interesting concept. I will see what I can cook up.
Nick.
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)
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:
-
System.Text.Json
attributes-
JsonSerializerOptions
to be able to set conventions -
JsonRequiredAttribute
to detect the nullability of a property -
JsonIgnoreAttribute
to ignore a property, including conditionally withJsonIgnoreCondition
-
JsonDerivedTypeAttribute
andJsonPolymorphicAttribute
to support polymorphism
-
-
Newtonsoft.Json
attributes -
System.Xml.Serialization
-
XmlArrayAttribute
for detecting nullability -
XmlArrayItemAttribute
for detecting nullability and allowed types -
XmlAttributeAttribute
to populate a concrete type when the property is based on an abstract type or to specify the type of data (for example[XmlAttribute(DataType = "base64Binary")] public byte[] Image;
)-
XmlElement(Type = typeof(Manager))
-
XmlElement(DataType = "base64Binary")
-
-
XmlElementAttribute
to populate a concrete type when the property is based on an abstract type-
XmlElement(Type = typeof(Manager))
-
XmlElement(DataType = "base64Binary")
-
-
XmlIgnoreAttribute
to not populate a field with data -
XmlIncludeAttribute
to populate a concrete type when the property is based on an abstract type -
XmlChoiceIdentifierAttribute
to populate strings/objects that can be set to a specific set of types/values
-
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.
Very interested in this feature too, and completely agree with @DarkDibblez, it will be a big time saver!