private static void Combine(JsonSchemaModel model, JsonSchema schema) { model.Optional = model.Optional && (schema.Optional ?? false); model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any); model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength); model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength); model.MaximumDecimals = MathUtils.Min(model.MaximumDecimals, schema.MaximumDecimals); model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum); model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum); model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems); model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems); model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties; if (schema.Enum != null) { if (model.Enum == null) model.Enum = new List<JToken>(); model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer()); } model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None); if (schema.Pattern != null) { if (model.Patterns == null) model.Patterns = new List<string>(); model.Patterns.AddDistinct(schema.Pattern); } }
public static JsonSchemaModel Create(IList<JsonSchema> schemata) { JsonSchemaModel model = new JsonSchemaModel(); foreach (JsonSchema schema in (IEnumerable<JsonSchema>) schemata) JsonSchemaModel.Combine(model, schema); return model; }
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); }
public SchemaScope(JTokenType tokenType, JsonSchemaModel schema) { _tokenType = tokenType; _schema = schema; if (_schema != null && _schema.Properties != null) _requiredProperties = GetRequiredProperties(_schema).Distinct().ToDictionary(p => p, p => false); else _requiredProperties = new Dictionary<string, bool>(); }
public static JsonSchemaModel Create(IList<JsonSchema> schemata) { JsonSchemaModel model = new JsonSchemaModel(); foreach (JsonSchema schema in schemata) { Combine(model, schema); } return model; }
private static void Combine(JsonSchemaModel model, JsonSchema schema) { // Version 3 of the Draft JSON Schema has the default value of Not Required /* STH: model.Required = model.Required || (schema.Required ?? false); */ if (schema.Required != null) { var required = schema.Required.Select(t => (string)t).Distinct().ToList(); if (model.Required != null) model.Required.AddRangeDistinct(required, null); else model.Required = required; } model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any); model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength); model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength); // not sure what is the best way to combine divisibleBy model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy); model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum); model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum); model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false); model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false); model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems); model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems); model.PositionalItemsValidation = model.PositionalItemsValidation || schema.PositionalItemsValidation; model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties; model.AllowAdditionalItems = model.AllowAdditionalItems && schema.AllowAdditionalItems; model.UniqueItems = model.UniqueItems || schema.UniqueItems; if (schema.Enum != null) { if (model.Enum == null) model.Enum = new List<JToken>(); model.Enum.AddRangeDistinct(schema.Enum, JToken.EqualityComparer); } model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None); if (schema.Pattern != null) { if (model.Patterns == null) model.Patterns = new List<string>(); model.Patterns.AddDistinct(schema.Pattern); } }
private static void Combine(JsonSchemaModel model, JsonSchema schema) { // Version 3 of the Draft JSON Schema has the default value of Not Required model.Required = model.Required || (schema.Required ?? false); model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any); model.MinimumLength = Newtonsoft.Json.Utilities.MathUtils.Max(model.MinimumLength, schema.MinimumLength); model.MaximumLength = Newtonsoft.Json.Utilities.MathUtils.Min(model.MaximumLength, schema.MaximumLength); // not sure what is the best way to combine divisibleBy model.DivisibleBy = Newtonsoft.Json.Utilities.MathUtils.Max(model.DivisibleBy, schema.DivisibleBy); model.Minimum = Newtonsoft.Json.Utilities.MathUtils.Max(model.Minimum, schema.Minimum); model.Maximum = Newtonsoft.Json.Utilities.MathUtils.Max(model.Maximum, schema.Maximum); model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false); model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false); model.MinimumItems = Newtonsoft.Json.Utilities.MathUtils.Max(model.MinimumItems, schema.MinimumItems); model.MaximumItems = Newtonsoft.Json.Utilities.MathUtils.Min(model.MaximumItems, schema.MaximumItems); model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties; if (schema.Enum != null) { if (model.Enum == null) model.Enum = new List<JToken>(); model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer()); } model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None); if (schema.Pattern != null) { if (model.Patterns == null) model.Patterns = new List<string>(); model.Patterns.AddDistinct(schema.Pattern); } }
private void ValidateEndObject(JsonSchemaModel schema) { if (schema == null) return; Dictionary<string, bool> requiredProperties = _currentScope.RequiredProperties; if (requiredProperties != null) { List<string> unmatchedRequiredProperties = requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key).ToList(); if (unmatchedRequiredProperties.Count > 0) RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties.ToArray())), schema); } }
private void ValidateCurrentToken() { // first time validate has been called. build model if (_model == null) { JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder(); _model = builder.Build(_schema); if (!JsonTokenUtils.IsStartToken(_reader.TokenType)) Push(new SchemaScope(JTokenType.None, CurrentMemberSchemas)); } switch (_reader.TokenType) { case JsonToken.StartObject: ProcessValue(); IList<JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList(); Push(new SchemaScope(JTokenType.Object, objectSchemas)); WriteToken(CurrentSchemas); break; case JsonToken.StartArray: ProcessValue(); IList<JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList(); Push(new SchemaScope(JTokenType.Array, arraySchemas)); WriteToken(CurrentSchemas); break; case JsonToken.StartConstructor: ProcessValue(); Push(new SchemaScope(JTokenType.Constructor, null)); WriteToken(CurrentSchemas); break; case JsonToken.PropertyName: WriteToken(CurrentSchemas); foreach (JsonSchemaModel schema in CurrentSchemas) { ValidatePropertyName(schema); } break; case JsonToken.Raw: ProcessValue(); break; case JsonToken.Integer: ProcessValue(); WriteToken(CurrentMemberSchemas); foreach (JsonSchemaModel schema in CurrentMemberSchemas) { ValidateInteger(schema); } break; case JsonToken.Float: ProcessValue(); WriteToken(CurrentMemberSchemas); foreach (JsonSchemaModel schema in CurrentMemberSchemas) { ValidateFloat(schema); } break; case JsonToken.String: ProcessValue(); WriteToken(CurrentMemberSchemas); foreach (JsonSchemaModel schema in CurrentMemberSchemas) { ValidateString(schema); } break; case JsonToken.Boolean: ProcessValue(); WriteToken(CurrentMemberSchemas); foreach (JsonSchemaModel schema in CurrentMemberSchemas) { ValidateBoolean(schema); } break; case JsonToken.Null: ProcessValue(); WriteToken(CurrentMemberSchemas); foreach (JsonSchemaModel schema in CurrentMemberSchemas) { ValidateNull(schema); } break; case JsonToken.EndObject: WriteToken(CurrentSchemas); foreach (JsonSchemaModel schema in CurrentSchemas) { ValidateEndObject(schema); } Pop(); break; case JsonToken.EndArray: WriteToken(CurrentSchemas); foreach (JsonSchemaModel schema in CurrentSchemas) { ValidateEndArray(schema); } Pop(); break; case JsonToken.EndConstructor: WriteToken(CurrentSchemas); Pop(); break; case JsonToken.Undefined: case JsonToken.Date: case JsonToken.Bytes: // these have no equivalent in JSON schema WriteToken(CurrentMemberSchemas); break; case JsonToken.None: // no content, do nothing break; default: throw new ArgumentOutOfRangeException(); } }
private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema) { if (schema == null || schema.Properties == null) return Enumerable.Empty<string>(); else return Enumerable.Select<KeyValuePair<string, JsonSchemaModel>, string>(Enumerable.Where<KeyValuePair<string, JsonSchemaModel>>((IEnumerable<KeyValuePair<string, JsonSchemaModel>>) schema.Properties, (Func<KeyValuePair<string, JsonSchemaModel>, bool>) (p => p.Value.Required)), (Func<KeyValuePair<string, JsonSchemaModel>, string>) (p => p.Key)); }
private bool ValidateObject(JsonSchemaModel schema) { if (schema == null) return true; return (TestType(schema, JsonSchemaType.Object)); }
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 void ValidateFloat(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.Float)) return; ValidateNotDisallowed(schema); double value = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture); 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) { 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) { double remainder = FloatingPointRemainder(value, schema.DivisibleBy.Value); if (!IsZero(remainder)) RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema); } }
private void ValidateString(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.String)) return; ValidateNotDisallowed(schema); string value = _reader.Value.ToString(); if (schema.MaximumLength != null && value.Length > schema.MaximumLength) RaiseError("String '{0}' exceeds maximum length of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.MaximumLength), schema); if (schema.MinimumLength != null && value.Length < schema.MinimumLength) RaiseError("String '{0}' is less than minimum length of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.MinimumLength), schema); if (schema.Patterns != null) { foreach (string pattern in schema.Patterns) { if (!Regex.IsMatch(value, pattern)) RaiseError("String '{0}' does not match regex pattern '{1}'.".FormatWith(CultureInfo.InvariantCulture, value, pattern), schema); } } }
private void ValidateFloat(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.Float)) return; ValidateInEnumAndNotDisallowed(schema); 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.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.MaximumDecimals != null && MathUtils.GetDecimalPlaces(value) > schema.MaximumDecimals) RaiseError("Float {0} exceeds the maximum allowed number decimal places of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.MaximumDecimals), schema); }
private void ValidateInteger(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.Integer)) return; ValidateInEnumAndNotDisallowed(schema); 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.Minimum != null && value < schema.Minimum) RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema); }
private void ValidateNull(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.Null)) return; ValidateInEnumAndNotDisallowed(schema); }
private void ValidateCurrentToken() { // first time validate has been called. build model if (_model == null) { JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder(); _model = builder.Build(_schema); } switch (_reader.TokenType) { case JsonToken.StartObject: ProcessValue(); JsonSchemaModel objectSchema = (ValidateObject(CurrentMemberSchema)) ? CurrentMemberSchema : null; Push(new SchemaScope(JTokenType.Object, objectSchema)); break; case JsonToken.StartArray: ProcessValue(); JsonSchemaModel arraySchema = (ValidateArray(CurrentMemberSchema)) ? CurrentMemberSchema : null; Push(new SchemaScope(JTokenType.Array, arraySchema)); break; case JsonToken.StartConstructor: Push(new SchemaScope(JTokenType.Constructor, null)); break; case JsonToken.PropertyName: ValidatePropertyName(CurrentSchema); break; case JsonToken.Raw: break; case JsonToken.Integer: ProcessValue(); ValidateInteger(CurrentMemberSchema); break; case JsonToken.Float: ProcessValue(); ValidateFloat(CurrentMemberSchema); break; case JsonToken.String: ProcessValue(); ValidateString(CurrentMemberSchema); break; case JsonToken.Boolean: ProcessValue(); ValidateBoolean(CurrentMemberSchema); break; case JsonToken.Null: ProcessValue(); ValidateNull(CurrentMemberSchema); break; case JsonToken.Undefined: break; case JsonToken.EndObject: ValidateEndObject(CurrentSchema); Pop(); break; case JsonToken.EndArray: ValidateEndArray(CurrentSchema); Pop(); break; case JsonToken.EndConstructor: Pop(); break; case JsonToken.Date: break; default: throw new ArgumentOutOfRangeException(); } }
private void ValidateInEnumAndNotDisallowed(JsonSchemaModel schema) { if (schema == null) return; JToken value = new JValue(_reader.Value); if (schema.Enum != null) { if (!schema.Enum.ContainsValue(value, new JTokenEqualityComparer())) RaiseError("Value {0} is not defined in enum.".FormatWith(CultureInfo.InvariantCulture, value), schema); } JsonSchemaType? currentNodeType = GetCurrentNodeSchemaType(); if (currentNodeType != null) { if (JsonSchemaGenerator.HasFlag(schema.Disallow, currentNodeType.Value)) RaiseError("Type {0} is disallowed.".FormatWith(CultureInfo.InvariantCulture, currentNodeType), schema); } }
private static void Combine(JsonSchemaModel model, JsonSchema schema) { bool required2; if (!model.Required) { bool?required = schema.Required; required2 = (required != null && required.Value); } else { required2 = true; } model.Required = required2; JsonSchemaType type = model.Type; JsonSchemaType?type2 = schema.Type; model.Type = (type & ((type2 == null) ? JsonSchemaType.Any : type2.Value)); model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength); model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength); model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy); model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum); model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum); bool exclusiveMinimum2; if (!model.ExclusiveMinimum) { bool?exclusiveMinimum = schema.ExclusiveMinimum; exclusiveMinimum2 = (exclusiveMinimum != null && exclusiveMinimum.Value); } else { exclusiveMinimum2 = true; } model.ExclusiveMinimum = exclusiveMinimum2; bool exclusiveMaximum2; if (!model.ExclusiveMaximum) { bool?exclusiveMaximum = schema.ExclusiveMaximum; exclusiveMaximum2 = (exclusiveMaximum != null && exclusiveMaximum.Value); } else { exclusiveMaximum2 = true; } model.ExclusiveMaximum = exclusiveMaximum2; model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems); model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems); model.AllowAdditionalProperties = (model.AllowAdditionalProperties && schema.AllowAdditionalProperties); if (schema.Enum != null) { if (model.Enum == null) { model.Enum = new List <JToken>(); } model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer()); } JsonSchemaType disallow = model.Disallow; JsonSchemaType?disallow2 = schema.Disallow; model.Disallow = (disallow | ((disallow2 == null) ? JsonSchemaType.None : disallow2.Value)); if (schema.Pattern != null) { if (model.Patterns == null) { model.Patterns = new List <string>(); } model.Patterns.AddDistinct(schema.Pattern); } }
private void ValidateEndArray(JsonSchemaModel schema) { if (schema == null) return; int arrayItemCount = _currentScope.ArrayItemCount; if (schema.MaximumItems != null && arrayItemCount > schema.MaximumItems) RaiseError("Array item count {0} exceeds maximum count of {1}.".FormatWith(CultureInfo.InvariantCulture, arrayItemCount, schema.MaximumItems), schema); if (schema.MinimumItems != null && arrayItemCount < schema.MinimumItems) RaiseError("Array item count {0} is less than minimum count of {1}.".FormatWith(CultureInfo.InvariantCulture, arrayItemCount, schema.MinimumItems), schema); }
private void ValidatePropertyName(JsonSchemaModel schema) { if (schema == null) return; string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture); if (_currentScope.RequiredProperties.ContainsKey(propertyName)) _currentScope.RequiredProperties[propertyName] = true; if (schema.Properties != null && !schema.Properties.ContainsKey(propertyName)) { IList<string> definedProperties = schema.Properties.Select(p => p.Key).ToList(); if (!schema.AllowAdditionalProperties && !definedProperties.Contains(propertyName)) { RaiseError("Property '{0}' has not been defined and the schema does not allow additional properties.".FormatWith(CultureInfo.InvariantCulture, propertyName), schema); } } _currentScope.CurrentPropertyName = propertyName; }
private void ValidateBoolean(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.Boolean)) return; ValidateNotDisallowed(schema); }
private static void Combine(JsonSchemaModel model, JsonSchema schema) { bool flag; bool flag1; bool flag2; JsonSchemaModel jsonSchemaModel = model; if (model.Required) { flag = true; } else { bool?required = schema.Required; flag = (!required.HasValue ? false : required.Value); } jsonSchemaModel.Required = flag; JsonSchemaModel jsonSchemaModel1 = model; JsonSchemaType type = model.Type; JsonSchemaType? nullable = schema.Type; jsonSchemaModel1.Type = type & (!nullable.HasValue ? JsonSchemaType.Any : nullable.Value); model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength); model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength); model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy); model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum); model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum); JsonSchemaModel jsonSchemaModel2 = model; if (model.ExclusiveMinimum) { flag1 = true; } else { bool?exclusiveMinimum = schema.ExclusiveMinimum; flag1 = (!exclusiveMinimum.HasValue ? false : exclusiveMinimum.Value); } jsonSchemaModel2.ExclusiveMinimum = flag1; JsonSchemaModel jsonSchemaModel3 = model; if (model.ExclusiveMaximum) { flag2 = true; } else { bool?exclusiveMaximum = schema.ExclusiveMaximum; flag2 = (!exclusiveMaximum.HasValue ? false : exclusiveMaximum.Value); } jsonSchemaModel3.ExclusiveMaximum = flag2; model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems); model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems); model.AllowAdditionalProperties = (!model.AllowAdditionalProperties ? false : schema.AllowAdditionalProperties); if (schema.Enum != null) { if (model.Enum == null) { model.Enum = new List <JToken>(); } model.Enum.AddRangeDistinct <JToken>(schema.Enum, new JTokenEqualityComparer()); } JsonSchemaModel jsonSchemaModel4 = model; JsonSchemaType disallow = model.Disallow; JsonSchemaType? disallow1 = schema.Disallow; jsonSchemaModel4.Disallow = disallow | (!disallow1.HasValue ? JsonSchemaType.None : disallow1.Value); if (schema.Pattern != null) { if (model.Patterns == null) { model.Patterns = new List <string>(); } model.Patterns.AddDistinct <string>(schema.Pattern); } }
private void ValidateInteger(JsonSchemaModel schema) { if (schema == null) return; if (!TestType(schema, JsonSchemaType.Integer)) return; ValidateNotDisallowed(schema); object value = _reader.Value; if (schema.Maximum != null) { if (JValue.Compare(JTokenType.Integer, value, schema.Maximum) > 0) RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema); if (schema.ExclusiveMaximum && JValue.Compare(JTokenType.Integer, value, schema.Maximum) == 0) RaiseError("Integer {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema); } if (schema.Minimum != null) { if (JValue.Compare(JTokenType.Integer, value, schema.Minimum) < 0) RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema); if (schema.ExclusiveMinimum && JValue.Compare(JTokenType.Integer, value, schema.Minimum) == 0) RaiseError("Integer {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema); } if (schema.DivisibleBy != null) { bool notDivisible; #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) if (value is BigInteger) { // not that this will lose any decimal point on DivisibleBy // so manually raise an error if DivisibleBy is not an integer and value is not zero BigInteger i = (BigInteger)value; bool divisibleNonInteger = !Math.Abs(schema.DivisibleBy.Value - Math.Truncate(schema.DivisibleBy.Value)).Equals(0); if (divisibleNonInteger) notDivisible = i != 0; else notDivisible = i % new BigInteger(schema.DivisibleBy.Value) != 0; } else #endif notDivisible = !IsZero(Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.Value); if (notDivisible) RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema); } }
private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema) { return schema.Properties.Where(p => !p.Value.Optional).Select(p => p.Key); }
private void ValidatePropertyName(JsonSchemaModel schema) { if (schema == null) return; string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture); if (_currentScope.RequiredProperties.ContainsKey(propertyName)) _currentScope.RequiredProperties[propertyName] = true; if (!schema.AllowAdditionalProperties) { bool propertyDefinied = IsPropertyDefinied(schema, propertyName); 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 void RaiseError(string message, JsonSchemaModel schema) { IJsonLineInfo lineInfo = this; string exceptionMessage = (lineInfo.HasLineInfo()) ? message + " Line {0}, position {1}.".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition) : message; OnValidationEvent(new JsonSchemaException(exceptionMessage, null, Path, lineInfo.LineNumber, lineInfo.LinePosition)); }
private bool ValidateArray(JsonSchemaModel schema) { if (schema == null) return true; return (TestType(schema, JsonSchemaType.Array)); }
private void ValidateNotDisallowed(JsonSchemaModel schema) { if (schema == null) return; JsonSchemaType? currentNodeType = GetCurrentNodeSchemaType(); if (currentNodeType != null) { if (JsonSchemaGenerator.HasFlag(schema.Disallow, currentNodeType.Value)) RaiseError("Type {0} is disallowed.".FormatWith(CultureInfo.InvariantCulture, currentNodeType), schema); } }
private bool TestType(JsonSchemaModel currentSchema, JsonSchemaType currentType) { if (!JsonSchemaGenerator.HasFlag(currentSchema.Type, currentType)) { RaiseError("Invalid type. Expected {0} but got {1}.".FormatWith(CultureInfo.InvariantCulture, currentSchema.Type, currentType), currentSchema); return false; } return true; }
private bool TestType(JsonSchemaModel currentSchema, JsonSchemaType currentType) { if (JsonSchemaGenerator.HasFlag(new JsonSchemaType?(currentSchema.Type), currentType)) return true; this.RaiseError(StringUtils.FormatWith("Invalid type. Expected {0} but got {1}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) currentSchema.Type, (object) currentType), currentSchema); return false; }