I am using JSON.NET to generate JSON Schema from c# object class. But I was unable to add any other json schema attributes e.g. maxLength, pattern(regex to validate email), etc
Below is my working code, I can only generate json schema with required attribute. It would be great if anyone can post some code example about how to add those extra attribute for json schema.
Thanks,
my code example
public class Customer
{
[JsonProperty(Required = Required.Always)]
public int CustomerID { get; set; }
[JsonProperty(Required = Required.Always)]
public string FirstName { get; set; }
[JsonProperty(Required = Required.Always)]
public string LastName { get; set; }
[JsonProperty(Required = Required.Always)]
public string Email { get; set; }
[JsonProperty(Required = Required.AllowNull)]
public string Phone { get; set; }
}
to
{
"title" : "Customer",
"type" : "object",
"properties" : {
"CustomerID" : {
"required" : true,
"type" : "integer"
},
"FirstName" : {
"required" : true,
"type" : "string"
},
"LastName" : {
"required" : true,
"type" : "string"
},
"Email" : {
"required" : true,
"type" : "string"
},
"Phone" : {
"required" : true,
"type" : [
"string",
"null"
]
}
}
}