-Added PatternProperties, ExclusiveMinimum and ExclusiveMaximum support to JSON schema

This commit is contained in:
JamesNK
2011-01-02 09:19:53 +00:00
parent 3ad33f7329
commit f00d796e03
12 changed files with 463 additions and 85 deletions
+161 -61
View File
@@ -43,15 +43,15 @@ namespace Newtonsoft.Json
private class SchemaScope
{
private readonly JTokenType _tokenType;
private readonly JsonSchemaModel _schema;
private readonly IList<JsonSchemaModel> _schemas;
private readonly Dictionary<string, bool> _requiredProperties;
public string CurrentPropertyName { get; set; }
public int ArrayItemCount { get; set; }
public JsonSchemaModel Schema
public IList<JsonSchemaModel> Schemas
{
get { return _schema; }
get { return _schemas; }
}
public Dictionary<string, bool> RequiredProperties
@@ -64,19 +64,19 @@ namespace Newtonsoft.Json
get { return _tokenType; }
}
public SchemaScope(JTokenType tokenType, JsonSchemaModel schema)
public SchemaScope(JTokenType tokenType, IList<JsonSchemaModel> schemas)
{
_tokenType = tokenType;
_schema = schema;
_schemas = schemas;
if (_schema != null && _schema.Properties != null)
_requiredProperties = GetRequiredProperties(_schema).Distinct().ToDictionary(p => p, p => false);
else
_requiredProperties = new Dictionary<string, bool>();
_requiredProperties = schemas.SelectMany(GetRequiredProperties).Distinct().ToDictionary(p => p, p => false);
}
private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema)
{
if (schema == null || schema.Properties == null)
return Enumerable.Empty<string>();
return schema.Properties.Where(p => p.Value.Required).Select(p => p.Key);
}
}
@@ -154,47 +154,79 @@ namespace Newtonsoft.Json
return poppedScope;
}
private JsonSchemaModel CurrentSchema
private IEnumerable<JsonSchemaModel> CurrentSchemas
{
get { return _currentScope.Schema; }
get { return _currentScope.Schemas; }
}
private JsonSchemaModel CurrentMemberSchema
private IEnumerable<JsonSchemaModel> CurrentMemberSchemas
{
get
{
if (_currentScope == null)
return _model;
return new List<JsonSchemaModel>(new [] { _model });
if (_currentScope.Schema == null)
return null;
if (_currentScope.Schemas == null || _currentScope.Schemas.Count == 0)
return Enumerable.Empty<JsonSchemaModel>();
switch (_currentScope.TokenType)
{
case JTokenType.None:
return _currentScope.Schema;
return _currentScope.Schemas;
case JTokenType.Object:
if (_currentScope.CurrentPropertyName == null)
throw new Exception("CurrentPropertyName has not been set on scope.");
JsonSchemaModel propertySchema;
if (CurrentSchema.Properties != null && CurrentSchema.Properties.TryGetValue(_currentScope.CurrentPropertyName, out propertySchema))
return propertySchema;
return (CurrentSchema.AllowAdditionalProperties) ? CurrentSchema.AdditionalProperties : null;
case JTokenType.Array:
if (!CollectionUtils.IsNullOrEmpty(CurrentSchema.Items))
{
if (CurrentSchema.Items.Count == 1)
return CurrentSchema.Items[0];
if (_currentScope.CurrentPropertyName == null)
throw new Exception("CurrentPropertyName has not been set on scope.");
if (CurrentSchema.Items.Count > (_currentScope.ArrayItemCount - 1))
return CurrentSchema.Items[_currentScope.ArrayItemCount - 1];
IList<JsonSchemaModel> schemas = new List<JsonSchemaModel>();
foreach (JsonSchemaModel schema in CurrentSchemas)
{
JsonSchemaModel propertySchema;
if (schema.Properties != null && schema.Properties.TryGetValue(_currentScope.CurrentPropertyName, out propertySchema))
{
schemas.Add(propertySchema);
}
if (schema.PatternProperties != null)
{
foreach (KeyValuePair<string, JsonSchemaModel> patternProperty in schema.PatternProperties)
{
if (Regex.IsMatch(_currentScope.CurrentPropertyName, patternProperty.Key))
{
schemas.Add(patternProperty.Value);
}
}
}
if (schemas.Count == 0 && schema.AllowAdditionalProperties && schema.AdditionalProperties != null)
schemas.Add(schema.AdditionalProperties);
}
return schemas;
}
case JTokenType.Array:
{
IList<JsonSchemaModel> schemas = new List<JsonSchemaModel>();
foreach (JsonSchemaModel schema in CurrentSchemas)
{
if (!CollectionUtils.IsNullOrEmpty(schema.Items))
{
if (schema.Items.Count == 1)
schemas.Add(schema.Items[0]);
return (CurrentSchema.AllowAdditionalProperties) ? CurrentSchema.AdditionalProperties : null;
if (schema.Items.Count > (_currentScope.ArrayItemCount - 1))
schemas.Add(schema.Items[_currentScope.ArrayItemCount - 1]);
}
if (schema.AllowAdditionalProperties && schema.AdditionalProperties != null)
schemas.Add(schema.AdditionalProperties);
}
return schemas;
}
case JTokenType.Constructor:
return null;
return Enumerable.Empty<JsonSchemaModel>();
default:
throw new ArgumentOutOfRangeException("TokenType", "Unexpected token type: {0}".FormatWith(CultureInfo.InvariantCulture, _currentScope.TokenType));
}
@@ -371,58 +403,80 @@ namespace Newtonsoft.Json
_model = builder.Build(_schema);
}
//ValidateValueToken();
switch (_reader.TokenType)
{
case JsonToken.StartObject:
ProcessValue();
JsonSchemaModel objectSchema = (ValidateObject(CurrentMemberSchema))
? CurrentMemberSchema
: null;
Push(new SchemaScope(JTokenType.Object, objectSchema));
IList<JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
Push(new SchemaScope(JTokenType.Object, objectSchemas));
break;
case JsonToken.StartArray:
ProcessValue();
JsonSchemaModel arraySchema = (ValidateArray(CurrentMemberSchema))
? CurrentMemberSchema
: null;
Push(new SchemaScope(JTokenType.Array, arraySchema));
IList<JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
Push(new SchemaScope(JTokenType.Array, arraySchemas));
break;
case JsonToken.StartConstructor:
Push(new SchemaScope(JTokenType.Constructor, null));
break;
case JsonToken.PropertyName:
ValidatePropertyName(CurrentSchema);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidatePropertyName(schema);
}
break;
case JsonToken.Raw:
break;
case JsonToken.Integer:
ProcessValue();
ValidateInteger(CurrentMemberSchema);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateInteger(schema);
}
break;
case JsonToken.Float:
ProcessValue();
ValidateFloat(CurrentMemberSchema);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateFloat(schema);
}
break;
case JsonToken.String:
ProcessValue();
ValidateString(CurrentMemberSchema);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateString(schema);
}
break;
case JsonToken.Boolean:
ProcessValue();
ValidateBoolean(CurrentMemberSchema);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateBoolean(schema);
}
break;
case JsonToken.Null:
ProcessValue();
ValidateNull(CurrentMemberSchema);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateNull(schema);
}
break;
case JsonToken.Undefined:
break;
case JsonToken.EndObject:
ValidateEndObject(CurrentSchema);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndObject(schema);
}
Pop();
break;
case JsonToken.EndArray:
ValidateEndArray(CurrentSchema);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndArray(schema);
}
Pop();
break;
case JsonToken.EndConstructor:
@@ -528,11 +582,24 @@ namespace Newtonsoft.Json
long value = Convert.ToInt64(_reader.Value, CultureInfo.InvariantCulture);
if (schema.Maximum != null && value > schema.Maximum)
RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
if (schema.Maximum != null)
{
if (value > schema.Maximum)
RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
if (schema.ExclusiveMaximum && value == schema.Maximum)
RaiseError("Integer {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
}
if (schema.Minimum != null && value < schema.Minimum)
RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
if (schema.Minimum != null)
{
if (value < schema.Minimum)
RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
if (schema.ExclusiveMinimum && value == schema.Minimum)
RaiseError("Integer {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
}
if (schema.DivisibleBy != null && !IsZero(value % schema.DivisibleBy.Value))
RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
}
private void ProcessValue()
@@ -541,8 +608,11 @@ namespace Newtonsoft.Json
{
_currentScope.ArrayItemCount++;
if (CurrentSchema != null && CurrentSchema.Items != null && CurrentSchema.Items.Count > 1 && _currentScope.ArrayItemCount >= CurrentSchema.Items.Count)
RaiseError("Index {0} has not been defined and the schema does not allow additional items.".FormatWith(CultureInfo.InvariantCulture, _currentScope.ArrayItemCount), CurrentSchema);
foreach (JsonSchemaModel currentSchema in CurrentSchemas)
{
if (currentSchema != null && currentSchema.Items != null && currentSchema.Items.Count > 1 && _currentScope.ArrayItemCount >= currentSchema.Items.Count)
RaiseError("Index {0} has not been defined and the schema does not allow additional items.".FormatWith(CultureInfo.InvariantCulture, _currentScope.ArrayItemCount), currentSchema);
}
}
}
@@ -558,11 +628,21 @@ namespace Newtonsoft.Json
double value = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
if (schema.Maximum != null && value > schema.Maximum)
RaiseError("Float {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Maximum), schema);
if (schema.Maximum != null)
{
if (value > schema.Maximum)
RaiseError("Float {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Maximum), schema);
if (schema.ExclusiveMaximum && value == schema.Maximum)
RaiseError("Float {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Maximum), schema);
}
if (schema.Minimum != null && value < schema.Minimum)
RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Minimum), schema);
if (schema.Minimum != null)
{
if (value < schema.Minimum)
RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Minimum), schema);
if (schema.ExclusiveMinimum && value == schema.Minimum)
RaiseError("Float {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Minimum), schema);
}
if (schema.DivisibleBy != null && !IsZero(value % schema.DivisibleBy.Value))
RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
@@ -585,14 +665,34 @@ namespace Newtonsoft.Json
if (_currentScope.RequiredProperties.ContainsKey(propertyName))
_currentScope.RequiredProperties[propertyName] = true;
bool propertyDefinied = (schema.Properties != null && schema.Properties.ContainsKey(propertyName));
if (!schema.AllowAdditionalProperties)
{
bool propertyDefinied = IsPropertyDefinied(schema, propertyName);
if (!propertyDefinied && !schema.AllowAdditionalProperties)
RaiseError("Property '{0}' has not been defined and the schema does not allow additional properties.".FormatWith(CultureInfo.InvariantCulture, propertyName), schema);
if (!propertyDefinied)
RaiseError("Property '{0}' has not been defined and the schema does not allow additional properties.".FormatWith(CultureInfo.InvariantCulture, propertyName), schema);
}
_currentScope.CurrentPropertyName = propertyName;
}
private bool IsPropertyDefinied(JsonSchemaModel schema, string propertyName)
{
if (schema.Properties != null && schema.Properties.ContainsKey(propertyName))
return true;
if (schema.PatternProperties != null)
{
foreach (string pattern in schema.PatternProperties.Keys)
{
if (Regex.IsMatch(propertyName, pattern))
return true;
}
}
return false;
}
private bool ValidateArray(JsonSchemaModel schema)
{
if (schema == null)