I am trying to generate a schema for validation using only the attributes in System.ComponentModel.DataAnnotations (i.e. required, key, etc.).
When I generate my schema for types I have defined using JSchemaGenerator properties that are not marked with the Required attribute are still listed as required in the schema that is generated.
I have found no way using the attributed in the DataAnnotations namespace to correct this. The only way I have seen to fix this is to use the JsonProperty to set how default/null handling is defined for the property, which I would rather not do since that ties
my entities to JSON.NET and mixes attributes.
The only workaround I have at the moment is the post-process the schema removing properties from the required list that do not have the Required attribute.
var schema = SchemaGenerator.Generate(type);
var removeRequireds = (from required in schema.Required
let requiredProperty = type.GetProperty(required)
let requiredAttribute =
(from p in requiredProperty.GetCustomAttributes(typeof(RequiredAttribute), true)
where requiredAttribute == null
foreach (var required in removeRequireds)
I think that if the schema generator is to support DataAnnotations in a pure way it should recognize that properties not marked with the required attribute are not required, without having to use JSON.NET specific attributes to compensate.