jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

'type' equals to 'string' breaks for number strings. #134

Closed lucasterra closed 5 years ago

lucasterra commented 5 years ago

Steps to reproduce the behavior

my $validator = JSON::Validator->new();

my $data = {
    credit_card_number => "5252525252525252",
};
my $schema = {
    type       => "object",
    required   => ["credit_card_number"],
    properties => {
        credit_card_number => { type => "string", minLength => 15, maxLength => 16 },
    }
};

my @errors = $validator->validate($data, $schema);

Expected behavior

I expected the validation to pass.

Actual behavior

It fails saying that "credit_card_number" is a number, not a string.

jhthorsen commented 5 years ago

That is not something I can reproduce. I think either...

  1. Your example is incorrect
  2. Your data is incorrect
jhthorsen commented 5 years ago

You could try with $validator->coerce(1) and see if it still reports any errors. If enabling coercion works, then it must be your input data.

You can look at https://github.com/mojolicious/json-validator/blob/4b30d066d2b0065789a7dd59e75fe4514878eed7/t/jv-string.t for more details.

lucasterra commented 5 years ago

Hey @jhthorsen adding $validator->coerce(1) worked for me. Thanks a lot for the help. I'll close the issue, if that's ok.

jhthorsen commented 5 years ago

That must mean that you had: {credit_card_number=>5252525252525252} and not {credit_card_number=>”5252525252525252”}