Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...