Skip to content

Improve type validation of numeric values #334

@oguzhanunlu

Description

@oguzhanunlu

Issue
TypeFactory uses jackson-databind's JsonNode.isIntegralNumber and JsonNode.isNumber to see if the node's type is integer or number, respectively. This result is used when comparing node type and schema type which is not precise enough in some cases.

if (node.isIntegralNumber())
return JsonType.INTEGER;
if (node.isNumber())
return JsonType.NUMBER;

Scenario
Assume that we have a schema where a property's type is integer.
Given a DecimalNode(v) where v is a value that can fit into Long, json-schema-validator thinks that node type is number and this results in type failure as number found, integer expected

Proposal
Update line 72-73 in the snippet above as following (canConvertToLong() is defined for DecimalNode at jackson-databind)

if (node.isIntegralNumber() || node.canConvertToLong())
    return JsonType.INTEGER; 

so that we can have a validator with better precision

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions