Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Enforce more constraints for string values

  • Loading branch information...
commit 8404f3b27aeca416e4fb133c64d1c74de3fef2d9 1 parent fa0006f
@sorear authored
Showing with 12 additions and 3 deletions.
  1. +4 −0 lib/X12/Schema/Element.pm
  2. +8 −3 t/03-element.t
View
4 lib/X12/Schema/Element.pm
@@ -96,6 +96,10 @@ sub encode {
$string = "".$value;
$string =~ s/ *$//;
+ length($string) or die "Value $value must have at least one non-space for ".$self->name."\n";
+ $string =~ /$sink->{non_charset_re}/ and die "Value $value contains a character outside the destination charset for ".$self->name."\n";
+ $string =~ /\P{Print}/ and die "Value $value contains a non-printable character for ".$self->name."\n";
+
length($string) > $maxp and die "Value $value does not fit in $maxp characters for ".$self->name."\n";
length($string) < $minp and $string .= (" " x ($minp - length($string)));
}
View
11 t/03-element.t
@@ -1,12 +1,12 @@
use strict;
use warnings;
-use Test::More tests => 114;
+use Test::More tests => 121;
use Test::Exception;
BEGIN { use_ok 'X12::Schema::Element'; }
use X12::Schema::TokenSink;
-my $sink = X12::Schema::TokenSink->new( element_sep => '*', segment_term => "~\n", component_sep => '\\', repeat_sep => '^' );
+my $sink = X12::Schema::TokenSink->new( element_sep => '*', segment_term => "~\n", component_sep => '\\', repeat_sep => '^', non_charset_re => qr/[^\x00-\xFF]/ );
my $el;
@@ -14,7 +14,7 @@ throws_ok { X12::Schema::Element->new(name => 'Foo') } qr/type.*required/;
throws_ok { X12::Schema::Element->new(type => 'N 3/3') } qr/name.*required/;
throws_ok { X12::Schema::Element->new(name => 'Foo', type => 'X 2/3') } qr/type at BUILD must look like/;
-throws_ok { X12::Schema::Element->new(name => 'Foo', type => 'ID 2/3') } qr/expand required/;
+throws_ok { X12::Schema::Element->new(name => 'Foo', type => 'R 2/3', expand => { }) } qr/expand/;
throws_ok { X12::Schema::Element->new(name => 'Foo', type => 'R3 2/3') } qr/Numeric postfix/;
sub elem_test {
@@ -98,6 +98,11 @@ elem_test('AN 2/4',
encode => 'FFFF' => 'FFFF',
encode => 'FFFFF' => qr/Value FFFFF does not fit in 4 characters for/,
encode => 'F^' => qr/Value F\^ after encoding would contain a prohibited delimiter.*/,
+ encode => 'F ' => 'F ',
+ encode => ' F ' => ' F',
+ encode => ' ' => qr/one non-space.*/,
+ encode => "\r" => qr/non-print.*/,
+ encode => "\x{3BB}" => qr/charset.*/,
);
elem_test('DT 6/6',
Please sign in to comment.
Something went wrong with that request. Please try again.