Example #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="actualJObject"></param>
 /// <param name="settings"></param>
 /// <param name="description"></param>
 /// <param name="propertiesToIgnore">The full paths of properties to ignore. Should contain properties that correspond to user-provided property bag keys.
 /// Since they don't correspond to OM properties, their casing is left exactly how the user specified.</param>
 public static void ValidatePropertyNameCasing(
     JObject actualJObject,
     JsonSerializerSettings settings,
     string description,
     HashSet <string> propertiesToIgnore)
 {
     JsonComparer.ValidatePropertyNameCasing(
         actualJObject,
         JsonComparer.IsCamelCase(settings),
         description,
         propertiesToIgnore);
 }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="actualJToken"></param>
        /// <param name="isCamelCase"></param>
        /// <param name="description"></param>
        /// <param name="propertiesToIgnore">The full paths of properties to ignore. Should contain properties that correspond to user-provided property bag keys.
        /// Since they don't correspond to OM properties, their casing is left exactly how the user specified.</param>
        public static void ValidatePropertyNameCasing(JToken actualJToken, bool isCamelCase, string description, HashSet <string> propertiesToIgnore)
        {
            if (actualJToken == null)
            {
                return;
            }

            switch (actualJToken.Type)
            {
            case JTokenType.Object:
                JObject actualJObject = (JObject)actualJToken;

                foreach (KeyValuePair <string, JToken> pair in actualJObject)
                {
                    string propertyName = pair.Key;
                    JToken value        = pair.Value;

                    string propertyPath = string.IsNullOrEmpty(description)
                                                  ? propertyName
                                                  : description + "." + propertyName;

                    if (!string.Equals(propertyName, JsonUtilities.Type, JsonUtilities.PropertyNameComparison))
                    {
                        if (propertiesToIgnore == null || !propertiesToIgnore.Contains(propertyPath))
                        {
                            // Check the property name.
                            if (isCamelCase)
                            {
                                bool isFirstCharLowerCase = (propertyName[0] == char.ToLower(propertyName[0], CultureInfo.CurrentCulture));
                                Assert.True(isFirstCharLowerCase, propertyPath + " is not camelCase.");
                            }
                            else
                            {
                                bool isFirstCharUpperCase = (propertyName[0] == char.ToUpper(propertyName[0], CultureInfo.CurrentCulture));
                                Assert.True(isFirstCharUpperCase, propertyPath + " is not PascalCase.");
                            }

                            // Check nested values
                            JsonComparer.ValidatePropertyNameCasing(value, isCamelCase, propertyPath, propertiesToIgnore);
                        }
                        else
                        {
                            // Ensure the property casing did not get changed.
                            string expectedPath = propertiesToIgnore.Single(val => propertiesToIgnore.Comparer.Equals(val, propertyPath));
                            string expectedName = expectedPath.Substring(expectedPath.Length - propertyName.Length);
                            Assert.Equal(expectedName, propertyName);
                        }
                    }
                }
                break;

            case JTokenType.Array:
                JArray jArray = (JArray)actualJToken;
                for (int i = 0; i < jArray.Count; i++)
                {
                    JsonComparer.ValidatePropertyNameCasing(jArray[i], isCamelCase, description + "[" + i + "]", propertiesToIgnore);
                }
                break;

            case JTokenType.Boolean:
            case JTokenType.Bytes:
            case JTokenType.Comment:
            case JTokenType.Date:
            case JTokenType.Float:
            case JTokenType.Guid:
            case JTokenType.Integer:
            case JTokenType.Raw:
            case JTokenType.String:
            case JTokenType.TimeSpan:
            case JTokenType.Uri:
            case JTokenType.Property:
            case JTokenType.Null:
                // Nothing to do.
                break;

            case JTokenType.Constructor:
            case JTokenType.None:
            case JTokenType.Undefined:
            default:
                throw new NotImplementedException("Validation logic not implemented for JTokenType " + actualJToken.Type);
            }
        }