/// <summary> /// Instantiates a TreeWalkerParameters object with the properties that are required to instantiate a TreeWalkerSession object. /// </summary> /// <param name="sessionId">The unique identifier for this session.</param> /// <param name="forgeTree">The ForgeTree for this session.</param> /// <param name="forgeState">The Forge state.</param> /// <param name="callbacks">The callbacks object.</param> /// <param name="token">The cancellation token.</param> public TreeWalkerParameters( Guid sessionId, ForgeTree forgeTree, IForgeDictionary forgeState, ITreeWalkerCallbacks callbacks, CancellationToken token) { if (sessionId == Guid.Empty) { throw new ArgumentNullException("sessionId"); } if (forgeTree == null) { throw new ArgumentNullException("forgeTree"); } if (forgeState == null) { throw new ArgumentNullException("forgeState"); } if (callbacks == null) { throw new ArgumentNullException("callbacks"); } if (token == null) { throw new ArgumentNullException("token"); } this.SessionId = sessionId; this.ForgeTree = forgeTree; this.ForgeState = forgeState; this.Callbacks = callbacks; this.Token = token; }
private static List <JObject> ConvertStringToJObjectList(string schema, bool validateAsDictionary) { List <JObject> schemaList = new List <JObject>(); // There could be three possible cases: // 1. schema is a Dictionary<string, ForgeTree> and should be validated as a dictionary. // 2. schema is a Dictionary<string, ForgeTree> and should be validated as individual ForgeTree's. // 3. schema is a ForgeTree and should be validated as a single ForgeTree. Dictionary <string, ForgeTree> forgeTrees = JsonConvert.DeserializeObject <Dictionary <string, ForgeTree> >(schema); if (validateAsDictionary) { schemaList.Add(JObject.Parse(schema)); } else { foreach (KeyValuePair <string, ForgeTree> kvp in forgeTrees) { ForgeTree forgeTree = kvp.Value; if (forgeTree.Tree == null) { // Deserialize into Dictionary does not throw exception but will have null "Tree" property if schema is just a ForgeTree. // Try to deserialize string into ForgeTree directly. JsonConvert.DeserializeObject <ForgeTree>(schema); schemaList.Add(JObject.Parse(schema)); break; } schemaList.Add(SerializeToJObject(forgeTree)); } } return(schemaList); }
public void TestInitialize() { rulesForForgeTree = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "contracts\\ForgeSchemaValidationRules.json")); rulesForForgeTreeDictionary = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "contracts\\ForgeSchemaDictionaryValidationRules.json")); jSchemaRulesForForgeTree = JSchema.Parse(rulesForForgeTree); linkedRulesForForgeTreeDictionary = ForgeSchemaValidator.GetLinkedJSchemaRules(rulesForForgeTreeDictionary, rulesForForgeTree, "//ForgeSchemaValidationRules.json"); pathToForgeTree = Path.Combine(Environment.CurrentDirectory, "test\\ExampleSchemas\\TardigradeSchema.json"); directoryPathToMultipleForgeTree = Path.Combine(Environment.CurrentDirectory, "test\\ExampleSchemas"); forgeTreeAsString = File.ReadAllText(pathToForgeTree); forgeTree = JsonConvert.DeserializeObject <ForgeTree>((string)forgeTreeAsString); forgeTreeDictionaryAsString = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "test\\ExampleSchemas\\SubroutineSchema.json")); forgeTreeDictionary = new Dictionary <string, ForgeTree>(); forgeTreeDictionary.Add("tree1", forgeTree); forgeTreeDictionary.Add("tree2", forgeTree); forgeTreeDictionary.Add("tree3", forgeTree); invalidSchemaWithErrorContent = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "test\\InvalidTestSchemas\\InvalidTestSchemaErrorContent.json")); invalidSchemaDirectoryPath = "test\\ExampleSchemas\\TardigradeSchema.json"; }
public void Test_ValidationRulesAreValid() { // Get all the const string schemas from ForgeSchemaHelper. List <FieldInfo> schemas = this.GetAllPublicConstantFields(typeof(ForgeSchemaHelper)); Console.WriteLine("Count of unique schemas getting evaluated: " + schemas.Count); int treeCount = 0; // Iterate through each schema to run validations. foreach (FieldInfo fieldInfo in schemas) { Console.WriteLine(fieldInfo.Name); string jsonSchema = (string)fieldInfo.GetRawConstantValue(); List <Tuple <string, bool> > jsonTrees = new List <Tuple <string, bool> >(); // jsonSchema may be deserialized to either a Dictionary<string, ForgeTree> (containing multiple trees), or a single ForgeTree. // Gather all the individual ForgeTree(s) from the schema and cache them in jsonTrees with their expectedResult. try { Dictionary <string, ForgeTree> forgeTrees = JsonConvert.DeserializeObject <Dictionary <string, ForgeTree> >(jsonSchema); foreach (var kvp in forgeTrees) { string treeName = kvp.Key; ForgeTree forgeTree = kvp.Value; if (forgeTree.Tree == null) { // Deserialize into Dictionary does not throw exception but will have null "Tree" property if schema is just a ForgeTree. // Throw exception here to trigger deserializing into ForgeTree directly. throw new NullReferenceException(); } string jsonSubSchema = JsonConvert.SerializeObject( forgeTree, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, // Prevent default values from getting added to serialized json schema. Converters = new List <JsonConverter> { new Newtonsoft.Json.Converters.StringEnumConverter() } // Use string enum values instead of numerical. }); // expectedResult is false if this schema/TreeName is blacklisted. bool expectedResult = !(this.jsonSchemaFailureBlacklist.TryGetValue(fieldInfo.Name, out List <string> list) && list.Contains(treeName)); jsonTrees.Add(new Tuple <string, bool>(jsonSubSchema, expectedResult)); } } catch (Exception) { try { // Verify that schema can be deserialized. JsonConvert.DeserializeObject <ForgeTree>(jsonSchema); // expectedResult is false if this schema/TreeName is blacklisted. bool expectedResult = !(this.jsonSchemaFailureBlacklist.TryGetValue(fieldInfo.Name, out List <string> list) && list.Contains("NA")); jsonTrees.Add(new Tuple <string, bool>(jsonSchema, expectedResult)); } catch (Exception) { Assert.Fail("ForgeSchema for property (" + fieldInfo.Name + ") did not deserialize to a ForgeTree or Dictionary<string, ForgeTree>."); } } // Validate each ForgeTree in this schema according to their expectedResult. foreach (var tuple in jsonTrees) { treeCount++; string schema = tuple.Item1; bool expectedResult = tuple.Item2; this.Validate(jsonSchema: schema, expectedResult: expectedResult); } } Console.WriteLine("Count of unique ForgeTrees getting evaluated: " + treeCount); }
/// <summary> /// Validates the ForgeTree schema with the given rules. /// </summary> /// <param name="schema">The schema to be validated.</param> /// <param name="rules">The rules used to validate input schemas is only allowed in string or JSchema type.</param> /// <param name="errorList">The errorList that contains ValidationError objects created in schema validation.</param> /// <returns>The result of schema validation.</returns> public static bool ValidateSchemaAsForgeTree(ForgeTree schema, object rules, out IList <ValidationError> errorList) { return(Validate(new List <JObject> { SerializeToJObject(schema) }, rules, out errorList)); }