File: jv-string.t

package info (click to toggle)
libjson-validator-perl 3.06%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 616 kB
  • sloc: perl: 1,308; makefile: 6
file content (43 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use lib '.';
use t::Helper;
use Test::More;
use utf8;

my $schema = {
  type       => 'object',
  properties => {
    nick =>
      {type => 'string', minLength => 3, maxLength => 10, pattern => qr{^\w+$}}
  }
};

validate_ok {nick => 'batman'}, $schema;
validate_ok {nick => 1000},     $schema,
  E('/nick', 'Expected string - got number.');
validate_ok {nick => '1000'}, $schema;
validate_ok {nick => 'aa'}, $schema, E('/nick', 'String is too short: 2/3.');
validate_ok {nick => 'a' x 11}, $schema,
  E('/nick', 'String is too long: 11/10.');
like +join('', t::Helper->validator->validate({nick => '[nick]'})),
  qr{/nick: String does not match}, 'String does not match';

delete $schema->{properties}{nick}{pattern};
validate_ok {nick => 'Déjà vu'}, $schema;

t::Helper->validator->coerce(1);
validate_ok {nick => 1000}, $schema;

# https://github.com/mojolicious/json-validator/issues/134
validate_ok(
  {credit_card_number => '5252525252525252'},
  {
    type       => "object",
    required   => ["credit_card_number"],
    properties => {
      credit_card_number =>
        {type => "string", minLength => 15, maxLength => 16},
    }
  }
);

done_testing;