Home JSON Schema Generation with System.ComponentModel.DataAnnotations
JSON Schema Generation with System.ComponentModel.DataAnnotations
Cancel

JSON Schema Generation with System.ComponentModel.DataAnnotations

The System.ComponentModel.DataAnnotations namespace defines numerous attributes that are commonly used within ASP.Net and other areas to validate data models.

The primary downside to using this approach is that the JSON has to be deserialized into the model before validation can occur.

An alternative is to generate a JSON Schema from the model. The JsonSchema.Net.Generation.DataAnnotations library is an extension on JsonSchema.Net.Generation to support the System.ComponentModel.DataAnnotations attributes.

It’s important to understand that the DataAnnotations attributes are not as flexible as the attributes defined in the main library. For advanced schema features, using the main library attributes is imperative.

Usage

In order to generate constraints from the DataAnnotations attributes, you’ll need to first register the handlers.

1
DataAnnotationsSupport.AddDataAnnotations();

Supported attributes

The following attributes are supported:

AttributeDescription of support
[AllowedValues()]Produces an enum keyword with the indicated values. (.Net 8 only)
[DeniedValues()]Produces a not: { enum: []} keyword construct with the indicated values. (.Net 8 only)
[Base64String]Produces a format: base64 keyword. Note that this format is defined by OpenAPI but not JSON Schema.
[EmailAddress]Produces a format: email keyword.
[EnumDataType()]Produces an enum keyword with the values of the C# enum type supplied in the attribute.
[Length()]Produces minLength and/or maxLength keywords.
[MaxLength()]Produces a maxLength keyword.
[MinLength()]Produces a minLength keyword.
[Range()]Produces minimum and/or maximum keywords.
[RegularExpression()]Produces a pattern keyword.
[StringLength()]Produces minLength and/or maxLength keywords.
[UrlAttribute]Produces a format: uri keyword.
Contents