Skip to content

Commit

Permalink
supporting validation for empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
daspek committed Feb 12, 2015
1 parent 04af441 commit 6e1c2c2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
5 changes: 4 additions & 1 deletion OneDrive.ApiDocumentation.Validation/CodeBlockAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public class CodeBlockAnnotation
/// Speicfy that the result is a collection of the resource type instead of a single instance.
/// </summary>
[JsonProperty("isCollection", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool IsCollection { get; set; }
public bool IsCollection { get; set; }

[JsonProperty("isEmpty", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool IsEmpty { get; set; }

[JsonProperty("truncated", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool TruncatedResult { get; set; }
Expand Down
64 changes: 48 additions & 16 deletions OneDrive.ApiDocumentation.Validation/Json/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,29 @@ public bool ValidateJson(string json, out ValidationError[] errors, Dictionary<s
}
else
{
var collectionMembers = obj[collectionPropertyName];
if (collectionMembers.Count() == 0)
{
detectedErrors.Add(new ValidationWarning(ValidationErrorCode.CollectionArrayEmpty, null, "Property contained an empty array that was not validated: {0}", collectionPropertyName));
}
var collectionMembers = obj[collectionPropertyName];
if (!collectionMembers.Any())
{
if (!annotation.IsEmpty)
{
detectedErrors.Add(
new ValidationWarning(
ValidationErrorCode.CollectionArrayEmpty,
null,
"Property contained an empty array that was not validated: {0}",
collectionPropertyName));
}
}
else if (annotation.IsEmpty)
{
detectedErrors.Add(
new ValidationWarning(
ValidationErrorCode.CollectionArrayNotEmpty,
null,
"Property contained a non-empty array that was expected to be empty: {0}",
collectionPropertyName));
}

foreach (JContainer container in collectionMembers)
{
ValidateJContainer(container, annotation, otherSchemas, detectedErrors);
Expand Down Expand Up @@ -142,11 +160,14 @@ private void ValidateJContainer(JContainer obj, bool allowTruncation, Dictionary
missingProperties.AddRange(from m in ExpectedProperties select m.Key);

foreach (JToken token in obj)
{
JsonProperty inputProperty = ParseProperty(token, this);
missingProperties.Remove(inputProperty.Name);
// This detects bad types, extra properties, etc.
ValidateProperty(inputProperty, otherSchemas, detectedErrors, allowTruncation);
{
JsonProperty inputProperty = ParseProperty(token, this, detectedErrors);
if (inputProperty != null)
{
missingProperties.Remove(inputProperty.Name);
// This detects bad types, extra properties, etc.
ValidateProperty(inputProperty, otherSchemas, detectedErrors, allowTruncation);
}
}

if (null != OptionalProperties)
Expand Down Expand Up @@ -256,7 +277,7 @@ private PropertyValidationOutcome ValidateProperty(JsonProperty inputProperty, D
inputProperty.Name, inputProperty.Type, schemaPropertyDef.Type));
return PropertyValidationOutcome.InvalidType;
}
}
}
else
{
detectedErrors.Add(new ValidationWarning(ValidationErrorCode.AdditionalPropertyDetected, null, "Extra property: property '{0}' [{1}] was not expected.", inputProperty.Name, inputProperty.Type));
Expand Down Expand Up @@ -477,9 +498,9 @@ private static JsonProperty ParseProperty(string name, JToken value, JsonSchema
}

return null;
}

private static JsonProperty ParseProperty(JToken token, JsonSchema containerSchema)
}

private static JsonProperty ParseProperty(JToken token, JsonSchema containerSchema, List<ValidationError> detectedErrors = null)
{
JsonProperty propertyInfo = null;
if (token.Type == JTokenType.Property)
Expand All @@ -488,8 +509,19 @@ private static JsonProperty ParseProperty(JToken token, JsonSchema containerSche
propertyInfo = ParseProperty(tokenProperty.Name, tokenProperty.Value, containerSchema);
}
else
{
Console.WriteLine("Unhandled token type: " + token.Type);
{
if (detectedErrors != null)
{
detectedErrors.Add(
new ValidationWarning(
ValidationErrorCode.JsonParserException,
token.Path,
"Unhandled token type: " + token.Type));
}
else
{
Console.WriteLine("Unhandled token type: " + token.Type);
}
}
return propertyInfo;
}
Expand Down
1 change: 1 addition & 0 deletions OneDrive.ApiDocumentation.Validation/ValidationError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ValidationErrorCode
JsonErrorObject,
MissingCollectionProperty,
CollectionArrayEmpty,
CollectionArrayNotEmpty,
RequiredPropertiesMissing,
AdditionalPropertyDetected,
ExpectedTypeDifferent,
Expand Down

0 comments on commit 6e1c2c2

Please sign in to comment.