File: marcvalidate

package info (click to toggle)
libmarc-schema-perl 0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: perl: 187; makefile: 7
file content (108 lines) | stat: -rw-r--r-- 2,405 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env perl

use strict;
use warnings;

our $VERSION = '0.10';

use Getopt::Long 'HelpMessage';
use MARC::Parser::RAW;
use MARC::Parser::XML;
use MARC::Schema;

GetOptions(
    'file|f=s'   => \my $file,
    'type|t=s'   => \( my $type = 'RAW' ),
    'schema|s=s' => \my $schema_file,
    'help|h'     => sub { HelpMessage() },
) or HelpMessage();

$file = shift unless defined($file);

HelpMessage() unless defined $file and -e $file;

my $schema = MARC::Schema->new({file => $schema_file});

my $parser;
if ( $type eq 'RAW' ) {
    $parser = MARC::Parser::RAW->new($file);
}
elsif ( $type eq 'XML' ) {
    $parser = MARC::Parser::XML->new($file);
}
else {
    print q{type '$type' not supported. Use 'RAW' or 'XML'};
}

my $record_count = 0;
while ( my $record = $parser->next() ) {
    $record_count++;
    my $id = _id($record);
    my @error = $schema->check($record);
    if (@error > 0) {
        foreach my $error (@error) {
            if (exists $error->{value}) {
                print qq{$id\t$error->{tag}\t$error->{error}\t$error->{value}\n};
            } else {
                print qq{$id\t$error->{tag}\t$error->{error}\t\n};
            }
        }
    }
}

sub _id {
    my ($record) = @_;
    my ($id) = map { $_->[-1] } grep { $_->[0] eq '001' } @$record;
    $id = defined $id ? $id : $record_count;
    return $id;
}

__END__

=encoding utf-8

=head1 NAME

marcvalidate - Validate a file with MARC21 records

=head1 SYNOPSIS

  $ marcvalidate [options] FILE

  options:
  --type,-t         Type of MARC21 serialization (RAW|XML, default: RAW)
  --schema,-s       Location MARC JSON schema
  --help,-h         Print this help

=head1 DESCRIPTION

C<marcvalidate> validates a file with MARC21 records against L<MARC::Schema> 
and prints out errors as tab separated list (id, field tag, error, value). 
The ID is extracted from MARC21 field L<001|https://www.loc.gov/marc/bibliographic/bd001.html>.
For a detailed description of the (default) schema see L<"MARC21 structure in JSON"|https://pkiraly.github.io/2018/01/28/marc21-in-json/>.


=head1 AUTHOR

Johann Rolschewski E<lt>jorol@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright 2018- Johann Rolschewski

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<Catmandu::Validator>

L<JSON::Schema>

L<PICA::Schema>

L<MARC::Lint>

=cut