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) || NETSTANDARD1_1
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.GetValueOrDefault());
}
if (notDivisible)
{
RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
}
}
}