I'm trying to setup JSON validation using NJsonSchema. I want that C# class properties are translated to lower camel case when validated.
I managed to achieve that goal by passing a custom JsonSchemaGeneratorSettings
object to JsonSchema.FromType()
.
var schema = JsonSchema.FromType<Cat>(new JsonSchemaGeneratorSettings {
DefaultPropertyNameHandling = PropertyNameHandling.Default
});
var schemaData = schema.ToJson();
var errors = schema.Validate(req.ReadAsString() ?? "");
foreach (var error in errors)
Console.WriteLine(error.Path + ": " + error.Kind);
However, this shows the following warning:
CS0618: 'JsonSchemaGeneratorSettings.DefaultPropertyNameHandling' is obsolete: 'Use SerializerSettings directly instead. In NSwag.AspNetCore the property is set automatically.'
I tried to modify the code as follows, but this code defaults to creating upper camel case properties again:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
MissingMemberHandling = MissingMemberHandling.Error,
ContractResolver = new DefaultContractResolver() {
NamingStrategy = new CamelCaseNamingStrategy()
}
};
var schema = JsonSchema.FromType<Cat>(new JsonSchemaGeneratorSettings {
DefaultPropertyNameHandling = PropertyNameHandling.Default
});
var schemaData = schema.ToJson();
var errors = schema.Validate(req.ReadAsString() ?? "");
foreach (var error in errors)
Console.WriteLine(error.Path + ": " + error.Kind);
How can I use JsonSerializerSettings and retain lower camel case properties?