Skip to content

Commit

Permalink
Schema check subfield order (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Mar 19, 2018
1 parent c29bb52 commit fd67d3b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
17 changes: 16 additions & 1 deletion lib/PICA/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ sub check_field {

my %errors;
if ($spec->{subfields}) {
my $order;
my %sfcounter;
my (undef, undef, @subfields) = @$field;

Expand All @@ -92,6 +93,16 @@ sub check_field {
repeated => 1,
message => "subfield $id\$$code is not repeatable",
};
} elsif (!$options{ignore_subfield_order} && defined $sfspec->{order}) {
if (defined $order && $order > $sfspec->{order}) {
$errors{$code} = {
code => $code,
order => $sfspec->{order},
message => "wrong subfield order of $id\$$code"
}
} else {
$order = 1*$sfspec->{order};
}
}
$sfcounter{$code}++;
} elsif (!$options{ignore_unknown_subfields}) {
Expand Down Expand Up @@ -183,6 +194,10 @@ Don't report fields not included in the schema.
Don't report subfields not included in the schema.
=item ignore_subfield_order
Don't report errors resulting on wrong subfield order.
=back
Errors are given as list of hash reference with keys C<tag> and C<occurrence>
Expand All @@ -209,7 +224,7 @@ field tag and optional occurrence if the tag starts with C<0>.
The current version does not properly validate required field on level 1 and 2.
Field types and subfield order have neither been implemented yet.
Field types have neither been implemented yet.
=head1 SEE ALSO
Expand Down
38 changes: 30 additions & 8 deletions t/files/schema-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
---
schemas:
021A-required:
021A:
fields:
021A:
required: true
subfields:
a:
required: 1
order: 1
d:
order: 2
x: {}
021A-repeatable:
fields:
Expand All @@ -21,24 +24,27 @@ records:
021A: "021A $atitle"
021A-repeated: "021A $atitle\n021A $atitle"
021A-repeated-subfield: "021A $atitle$atitle"
021A-missing-subfield: "021A $xy"
021A-missing-subfield: "021A $dsubtitle"
021A-ordered-subfields: "021A $atitle$dsubtitle"
021A-unordered-subfields: "021A $dsubtitle$atitle"
021A-subfield: "021A $atitle$atitle"
099X: "099X $xy"

tests:

- schema: 021A-required
- schema: 021A
record: 021A
- schema: 021A-repeatable
record: 021A-repeated

- schema: 021A-required
- schema: 021A
record: 021A-repeated
result:
- message: field 021A is not repeatable
repeated: 1
tag: 021A

- schema: 021A-required
- schema: 021A
record: 099X
result:
- message: unknown field 099X
Expand All @@ -64,7 +70,7 @@ tests:
options:
ignore_unknown_subfields: 1

- schema: 021A-required
- schema: 021A
record: 021A-repeated-subfield
result:
- tag: 021A
Expand All @@ -74,16 +80,32 @@ tests:
message: subfield 021A$a is not repeatable
repeated: 1

- schema: 021A-required
- schema: 021A
record: 021A-missing-subfield
result:
- tag: 021A
subfields:
a:
code: a
message: 'missing subfield 021A$a'
message: missing subfield 021A$a
required: 1

- schema: 021A
record: 021A-ordered-subfields
- schema: 021A
record: 021A-unordered-subfields
result:
- tag: 021A
subfields:
a:
code: a
order: 1
message: wrong subfield order of 021A$a
- schema: 021A
record: 021A-unordered-subfields
options:
ignore_subfield_order: 1

# TODO:
# - check fields in level 1 and level 2 (uniqueness per local copy!)

0 comments on commit fd67d3b

Please sign in to comment.