Skip to content

Commit

Permalink
Merge fdefeb3 into c630d71
Browse files Browse the repository at this point in the history
  • Loading branch information
jorol committed Apr 6, 2019
2 parents c630d71 + fdefeb3 commit bf873db
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ requires 'MARC::File::MiJ' , '0.04';
requires 'MARC::Record', '2.0.6';
requires 'MARC::Lint', '0';
requires 'MARC::Parser::RAW', '0';
requires 'MARC::Schema', '0';
requires 'MARC::Spec', '2.0.3';
requires 'Memoize', '0';
requires 'Moo', '1.0';
Expand Down
112 changes: 112 additions & 0 deletions lib/Catmandu/Validator/MARC.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package Catmandu::Validator::MARC;
use Catmandu::Sane;
use Catmandu::Util qw(:io :is :check);
use MARC::Schema;
use Moo;

our $VERSION = '1.241';

with qw(Catmandu::Validator);

has schema => ( is => 'ro' );
has ignore_unknown_fields => ( is => 'ro' );
has ignore_unknown_subfields => ( is => 'ro' );

has options => (
is => 'ro',
init_arg => undef,
builder => sub {
return {
ignore_unknown_fields => $_[0]->ignore_unknown_fields,
ignore_unknown_subfields => $_[0]->ignore_unknown_subfields,
}
},
);

sub BUILD {
my ($self, $args) = @_;
if (defined $self->schema) {
unless (is_instance($self->schema, 'MARC::Schema')) {
if (is_string($self->schema)) {
$self->{schema} = MARC::Schema->new({file => $self->schema});
}
}
} else {
$self->{schema} = MARC::Schema->new();
}
}

sub validate_data {
my ($self, $record) = @_;

my @errors = $self->schema->check($record, %{$self->options});

return @errors ? \@errors : undef;
}

1;
__END__
=head1 NAME
Catmandu::Validator::MARC - Validate MARC records against a MARC21 Schema
=head1 SYNOPSIS
In Perl code:
use Catmandu::Validator::MARC;
Catmandu::Validator::MARC;
use DDP;
# load default MARC schema
my $validator = Catmandu::Validator::MARC->new();
# ... or load custom MARC schema
my $validator = Catmandu::Validator::MARC->new( schema => 'schema.json' );
my $importer = Catmandu::Importer::MARC->new(
file => 't/camel.mrc',
type => "ISO"
);
$importer->each( sub {
my $record = shift;
unless($validator->validate($record)){
p $_ for @{$validator->last_errors()};
}
});
In Catmandu Fix language:
# reject all items not conforming to the default MARC schema
select valid(., MARC)
# reject all items not conforming to a custom MARC schema
select valid(., MARC, schema: 'schema.json')
=head1 DESCRIPTION
This L<Catmandu::Validator> can be used to check MARC records against an MARC21 schema. For more information see L<MARC::Schema> and L<"MARC21 structure in JSON"|https://pkiraly.github.io/2018/01/28/marc21-in-json/>.
See also L<Catmandu::Fix::validate>, and L<Catmandu::Fix::Condition::valid> for usage of validators in Catmandu Fix language.
=head1 CONFIGURATION
=over
=item schema
MARC Schema given as filename (JSON) or instance of L<MARC::Schema>.
=item ignore_unknown_fields
Don't report fields not included in the schema.
=item ignore_unknown_subfields
Don't report subfields not included in the schema.
=back
=cut
31 changes: 31 additions & 0 deletions t/Catmandu/Validator/MARC.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use strict;
use warnings;
use Test::Exception;
use Test::More;

use Catmandu::Importer::MARC;
use Catmandu::Validator::MARC;
use Catmandu qw(importer);

require_ok( 'Catmandu::Validator::MARC' );
can_ok('Catmandu::Validator::MARC', ('validate_data'));
# load default MARC schema
my $validator = Catmandu::Validator::MARC->new();

my $importer = Catmandu::Importer::MARC->new(
file => 't/camel.mrc',
type => "ISO"
);

my @errors;
$importer->each( sub {
my $record = shift;
unless($validator->validate($record)){
push @errors, $_ for @{$validator->last_errors()};
}
});

is (@errors, 1, 'got one error');
is ($errors[0]->{tag}, 100, 'error in field 100');

done_testing;

0 comments on commit bf873db

Please sign in to comment.