Consumer Schema is a tool to enable Consumer Driven Contracts (https://martinfowler.com/articles/consumerDrivenContracts.html) in C# by generating JSON Schemas (https://json-schema.org/) against POCOs, and then asserting those schemas against the actual classes generated by a producer.
As it is only POCOs and JSON, it can be used to check the shape of any data being sent, and can run within an IDE or on a build server.
There are two parts: the schema generator and the schema checker.
A consumer will first generate the schemas for the messages they are using, passing in the consumer name, the types of the messages and a folder to generate the schemas to.
var consumerName = "The Consumer Name";
var folderToCreateSchemasTo = "C:/temp";
var messagesIConsume = new[] { typeof(MessageOne), typeof(MessageTwo) };
var schemaGenerator = new SchemaGenerator();
schemaGenerator.GenerateSchemaDefinitionsToFolder(folderToCreateSchemasTo, consumerName, messagesIConsume);
This will generate a .json.schema file for each of the consumed messages in the folder specified. This should then be sent to the producer of the messages.
Nuget package: https://www.nuget.org/packages/ConsumerSchema.Generator/
Once the producer has received the schema files, they can be used in a test to assert that the shape of the actual messages being produced hasn't changed from the consumers expectations. Here's an example (using NUnit):
[Test]
public void CheckSchemaDefinitions(){
var messagesIProduce = new[] { typeof(MessageOne), typeof(MessageTwo) };
var schemaChecker = new SchemaChecker();
var schemaCheck = schemaChecker.CheckSchemasFromFolder("C:/PathToSchemas", messagesIProduce);
if (schemaCheck.HasErrors())
{
Assert.Fail(schemaCheck.GetErrorsSummary());
}
else
{
Assert.Pass();
}
}
If the message produced doesn't match the schema definition, the errors summary will display what doesn't match, for example:
"Class Name: MessageOne. Consumer: The Consumer Name. Errors: Required properties are missing from object: PropertyOne. Path ''."
Nuget package: https://www.nuget.org/packages/ConsumerSchema.Checker/