| 1 | package Praxo::JsonSchema; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Exporter 'import'; | ||
| 6 | use Scalar::Util qw(looks_like_number); | ||
| 7 | use B qw(svref_2object SVf_IOK SVf_NOK SVf_POK); | ||
| 8 | use Praxo::Json (); | ||
| 9 | |||
| 10 | our @EXPORT_OK = qw(validate); | ||
| 11 | |||
| 12 | sub validate { | ||
| 13 | my ($schema, $data) = @_; | ||
| 14 | my @errors; | ||
| 15 | _visit($schema, $data, '$', \@errors); | ||
| 16 | return \@errors; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub _visit { | ||
| 20 | my ($schema, $data, $path, $errors) = @_; | ||
| 21 | return unless ref($schema) eq 'HASH'; | ||
| 22 | my $type = $schema->{type} // ''; | ||
| 23 | if (!defined($data)) { | ||
| 24 | push @$errors, { path => $path, message => "expected $type, got null" } if $type ne ''; | ||
| 25 | return; | ||
| 26 | } | ||
| 27 | if ($type eq 'object') { _visit_object($schema, $data, $path, $errors); return } | ||
| 28 | if ($type eq 'array') { _visit_array($schema, $data, $path, $errors); return } | ||
| 29 | if ($type eq 'string') { _visit_string($schema, $data, $path, $errors); return } | ||
| 30 | if ($type eq 'number' || $type eq 'integer') { | ||
| 31 | _visit_number($schema, $type, $data, $path, $errors); | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | if ($type eq 'boolean') { _visit_boolean($schema, $data, $path, $errors); return } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _visit_object { | ||
| 38 | my ($schema, $data, $path, $errors) = @_; | ||
| 39 | if (ref($data) ne 'HASH') { | ||
| 40 | push @$errors, { path => $path, message => 'expected object' }; | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | my $required = $schema->{required}; | ||
| 44 | if (ref($required) eq 'ARRAY') { | ||
| 45 | for my $name (@$required) { | ||
| 46 | next if ref($name); | ||
| 47 | unless (exists $data->{$name}) { | ||
| 48 | push @$errors, { path => "$path.$name", message => 'required field missing' }; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | my $properties = $schema->{properties}; | ||
| 53 | if (ref($properties) eq 'HASH') { | ||
| 54 | for my $name (keys %$properties) { | ||
| 55 | next unless exists $data->{$name}; | ||
| 56 | _visit($properties->{$name}, $data->{$name}, "$path.$name", $errors); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _visit_array { | ||
| 62 | my ($schema, $data, $path, $errors) = @_; | ||
| 63 | if (ref($data) ne 'ARRAY') { | ||
| 64 | push @$errors, { path => $path, message => 'expected array' }; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | my $items = $schema->{items}; | ||
| 68 | return unless ref($items) eq 'HASH'; | ||
| 69 | for my $index (0 .. $#$data) { | ||
| 70 | _visit($items, $data->[$index], "$path\[$index\]", $errors); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _visit_string { | ||
| 75 | my ($schema, $data, $path, $errors) = @_; | ||
| 76 | if (ref($data) || _is_number($data) || Praxo::Json::is_bool($data)) { | ||
| 77 | push @$errors, { path => $path, message => 'expected string' }; | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | my $length = length($data); | ||
| 81 | my $min = $schema->{minLength} // 0; | ||
| 82 | my $max = $schema->{maxLength} // 0; | ||
| 83 | if ($min > 0 && $length < $min) { | ||
| 84 | push @$errors, { path => $path, message => "min length $min, got $length" }; | ||
| 85 | } | ||
| 86 | if ($max > 0 && $length > $max) { | ||
| 87 | push @$errors, { path => $path, message => "max length $max, got $length" }; | ||
| 88 | } | ||
| 89 | if (defined $schema->{pattern}) { | ||
| 90 | my $regex = eval { qr/$schema->{pattern}/ }; | ||
| 91 | if (!$regex) { | ||
| 92 | push @$errors, { path => $path, message => "invalid pattern '$schema->{pattern}'" }; | ||
| 93 | } elsif ($data !~ $regex) { | ||
| 94 | push @$errors, { path => $path, message => "pattern '$schema->{pattern}' not matched" }; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | _check_enum($schema, $data, $path, $errors); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _visit_number { | ||
| 101 | my ($schema, $type, $data, $path, $errors) = @_; | ||
| 102 | if (Praxo::Json::is_bool($data) || !_is_number($data)) { | ||
| 103 | push @$errors, { path => $path, message => 'expected number' }; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | if ($type eq 'integer' && int($data) != $data) { | ||
| 107 | push @$errors, { path => $path, message => "expected integer, got $data" }; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | _check_enum($schema, $data, $path, $errors); | ||
| 111 | if (defined $schema->{minimum} && $schema->{minimum} != 0 && $data < $schema->{minimum}) { | ||
| 112 | push @$errors, { path => $path, message => 'value below minimum' }; | ||
| 113 | } | ||
| 114 | if (defined $schema->{maximum} && $schema->{maximum} != 0 && $data > $schema->{maximum}) { | ||
| 115 | push @$errors, { path => $path, message => 'value above maximum' }; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _visit_boolean { | ||
| 120 | my ($schema, $data, $path, $errors) = @_; | ||
| 121 | if (!Praxo::Json::is_bool($data)) { | ||
| 122 | push @$errors, { path => $path, message => 'expected boolean' }; | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | _check_enum($schema, $data, $path, $errors); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _check_enum { | ||
| 129 | my ($schema, $data, $path, $errors) = @_; | ||
| 130 | my $values = $schema->{enum}; | ||
| 131 | return unless ref($values) eq 'ARRAY' && @$values; | ||
| 132 | for my $value (@$values) { | ||
| 133 | return if Praxo::Json::equals($value, $data); | ||
| 134 | } | ||
| 135 | push @$errors, { path => $path, message => 'value not in enum' }; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _is_number { | ||
| 139 | my ($value) = @_; | ||
| 140 | return 0 if !defined($value) || ref($value); | ||
| 141 | my $flags = svref_2object(\$value)->FLAGS; | ||
| 142 | return 1 if ($flags & SVf_IOK) || ($flags & SVf_NOK); | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; |