Click or drag to resize
Json.NET Schema

Using Data Annotation attributes

 

This sample generates a new JSchema from a .NET type with Data Annotations attributes.

Sample
Types
public class Building
{
    [Required]
    [MaxLength(100)]
    public string Name { get; set; }

    [Required]
    [Phone]
    public string PhoneNumber { get; set; }

    [Required]
    [EnumDataType(typeof(BuildingZone))]
    public string Zone { get; set; }
}

public enum BuildingZone
{
    Residential,
    Commercial,
    Industrial
}
Usage
JSchemaGenerator generator = new JSchemaGenerator();

JSchema schema = generator.Generate(typeof(Building));
// {
//   "type": "object",
//   "properties": {
//     "Name": {
//       "type": "string",
//       "maxLength": 100
//     },
//     "PhoneNumber": {
//       "type": "string",
//       "format": "phone"
//     },
//     "Zone": {
//       "type": "string",
//       "enum": [
//         "Residential",
//         "Commercial",
//         "Industrial"
//       ]
//     }
//   },
//   "required": [
//     "Name",
//     "PhoneNumber",
//     "Zone"
//   ]
// }