I am trying to create a JSON schema validator. My Json schema validates a certain property value and based on that it assigns value to another property. In my C# code I want to perform an action based on that. If you notice in my schema, if country is either "United States of America" or "Canada" then, I am setting effect be "compliant" else "non-complaint". And, In my C# code, I need the value of "effect" so that I can do some further processing. Is that possible? If not, what should be my approach? I am new to Json Schema (I have seen Azure polices doing something similar to this.)
Here is my Schema
{
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"enum": [ "United States of America", "Canada" ]
},
"effect": {"type": "string"}
},
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"effect": "Compliant"
},
"else": {
"effect": "Non-Compliant"
}
}
Here is my Document
{
"properties": {
"street_address": "1600 Pennsylvania Avenue NW",
"country": "Canada",
"postal_code": "20500"
}
}
Here is my C# Code
JObject data = null;
var currentDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
using (StreamReader r = new StreamReader(currentDirectory + @"/Documents/document.json"))
using (JsonTextReader reader = new JsonTextReader(r))
{
data = (JObject)JToken.ReadFrom(reader);
}
JsonSchema schema = JsonSchema.Parse(File.ReadAllText(currentDirectory + @"/Schemas/Schema.json"));
IList<string> messages;
var properties = (JObject)data["properties"];
bool valid = properties.IsValid(schema, out messages);