/// <summary> /// Validates a brightness trait. /// </summary> /// <param name="deviceTrait">Device trait to validate.</param> /// <param name="command">Command to verify.</param> /// <param name="commandParams">Command params expected for command.</param> /// <param name="stateKeys">State keys expected for command.</param> /// <param name="attributeKeys">Attribute keys expected for command.</param> /// <returns>Validation errors.</returns> private static IEnumerable <string> ValidateTrait( DeviceTrait deviceTrait, CommandType command, IEnumerable <string> commandParams, IEnumerable <string> stateKeys, IEnumerable <string> attributeKeys = null) { var validationErrors = new List <string>(); var commandName = command.ToEnumString(); if (command != CommandType.Unknown && !deviceTrait.Commands.ContainsKey(commandName)) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing required command '{commandName}'"); } if (commandParams != null) { foreach (var commandParam in commandParams) { if (commandParam.EndsWith(".*")) { if (!deviceTrait.Commands[commandName].Keys.Any(x => x.StartsWith(commandParam.Substring(0, commandParam.Length - 2)))) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing command param '{commandParam}' for command '{commandName}'"); } } else { if (!deviceTrait.Commands[commandName].ContainsKey(commandParam)) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing command param '{commandParam}' for command '{commandName}'"); } } } } if (stateKeys != null) { foreach (var stateKey in stateKeys) { if (stateKey.EndsWith(".*")) { if (!deviceTrait.State.Keys.Any(x => x.StartsWith(stateKey.Substring(0, stateKey.Length - 2)))) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing state '{stateKey}' for command '{commandName}'"); } } else { if (!deviceTrait.State.ContainsKey(stateKey)) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing state '{stateKey}' for command '{commandName}'"); } } } } if (attributeKeys != null) { var flattenedAttributes = deviceTrait.Attributes != null ? deviceTrait.Attributes.ToFlatDictionary() : new Dictionary <string, object>(); foreach (var attributeKey in attributeKeys) { if (attributeKey.EndsWith(".*")) { if (!flattenedAttributes.Keys.Any(x => x.StartsWith(attributeKey.Substring(0, attributeKey.Length - 2)))) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing attribute '{attributeKey}' for command '{commandName}'"); } } else { if (!flattenedAttributes.ContainsKey(attributeKey)) { validationErrors.Add($"Trait '{deviceTrait.Trait}' is missing attribute '{attributeKey}' for command '{commandName}'"); } } } } return(validationErrors); }