Commit ac509ac8 authored by Mattias Päivärinta's avatar Mattias Päivärinta
Browse files

PDTT-399 - Give appropriate validation message

parent 24b69846
......@@ -65,10 +65,10 @@ A subrule value HASHREF may have the following keys:
=item quantifier
Values: 'required' | 'optional-free' | 'optional-not-empty' |
'optional-constrained' | 'empty-constrained' | 'omitted-constrained' |
/repeatable (max \d+)?/ | /optional-repeatable (max \d+)?/ (default:
required)
Values: 'required' | 'required-strict' | 'optional-free' |
'optional-not-empty' | 'optional-constrained' | 'empty-constrained' |
'omitted-constrained' | /repeatable (max \d+)?/ |
/optional-repeatable (max \d+)?/ (default: required)
=item line
......@@ -212,7 +212,7 @@ Fax number section:
Fax number section type B: { quantifier: required }
Fax number section type C: { quantifier: required }
Fax number section type A:
- Fax Number: { line: field, type: phone number, quantifier: required }
- Fax Number: { line: field, type: phone number, quantifier: required-strict }
- Fax Ext: { line: field, type: token, quantifier: optional-free }
Fax number section type B:
- Fax Number: { line: field, type: void, quantifier: empty-constrained }
......@@ -293,7 +293,7 @@ Name server section:
Name server section type B: { quantifier: repeatable }
Name server section type C: { quantifier: repeatable }
Name server section type A:
- Name Server: { line: field, type: hostname }
- Name Server: { quantifier: required-strict, line: field, type: hostname }
- IP address section: { quantifier: repeatable }
Name server section type B:
- Name Server: { quantifier: empty-constrained, line: field, type: void }
......
......@@ -217,7 +217,7 @@ sub _occurances {
my $min_occurs;
my $max_occurs;
for ( $quantifier ) {
when ( 'required' ) {
when ( /^required$|^required-strict$/ ) {
$min_occurs = 1;
$max_occurs = 1;
}
......@@ -380,7 +380,7 @@ sub _line {
$subtype = 'field';
}
else {
if ( $quantifier =~ /^(?:omitted-constrained|required)$/ ) {
if ( $quantifier =~ /^(?:omitted-constrained|required-strict)$/ ) {
return;
}
$subtype = 'empty field';
......
......@@ -2,7 +2,7 @@ use strict;
use warnings;
use 5.014;
use Test::More tests => 13;
use Test::More tests => 14;
use Test::Differences;
use Test::MockObject;
......@@ -10,10 +10,14 @@ require_ok('PDT::TS::Whois::Validator');
use PDT::TS::Whois::Validator qw( validate );
my $grammar = {
'Simple field' => [
'Required field' => [
{ 'Domain Name' => { type => 'hostname', }, },
{ 'EOF' => { line => 'EOF', }, },
],
'Required-strict field' => [
{ 'Domain Name' => { line => 'field', type => 'hostname', quantifier => 'required-strict' }, },
{ 'EOF' => { line => 'EOF', }, },
],
'Optional-constrained field' => [
{ 'Domain Name' => { line => 'field', type => 'hostname', quantifier => 'optional-constrained', }, },
{ 'Referral URL' => { line => 'field', type => 'http url', quantifier => 'optional-constrained', }, },
......@@ -138,16 +142,17 @@ sub make_mock_lexer {
});
}
subtest 'Simple line' => sub {
plan tests => 3;
subtest 'Required field' => sub {
plan tests => 6;
{
my $lexer = make_mock_lexer (
['field', ['Domain Name', [], 'DOMAIN.EXAMPLE'], []],
['EOF', undef, []],
);
my @errors = validate( rule => 'Simple field', lexer => $lexer, grammar => $grammar, types => $types);
my @errors = validate( rule => 'Required field', lexer => $lexer, grammar => $grammar, types => $types);
eq_or_diff \@errors, [], 'Should accept field line';
is $lexer->line_no(), 2, 'Should consume non-empty-field line';
}
{
......@@ -155,8 +160,9 @@ subtest 'Simple line' => sub {
['non-empty line', 'gibberish', []],
['EOF', undef, []],
);
my @errors = validate(rule => 'Simple field', lexer => $lexer, grammar => $grammar, types => $types);
my @errors = validate(rule => 'Required field', lexer => $lexer, grammar => $grammar, types => $types);
cmp_ok scalar(@errors), '>=', 1, 'Should reject non-field line';
is $lexer->line_no(), 1, 'Should not consume non-field line';
}
{
......@@ -164,11 +170,48 @@ subtest 'Simple line' => sub {
['field', ['Domain Name', [], undef], []],
['EOF', undef, []],
);
my @errors = validate( rule => 'Simple field', lexer => $lexer, grammar => $grammar, types => $types);
my @errors = validate( rule => 'Required field', lexer => $lexer, grammar => $grammar, types => $types);
cmp_ok scalar(@errors), '>=', 1, 'Should reject empty-field line';
is $lexer->line_no(), 2, 'Should consume empty-field line';
}
};
subtest 'Required-strict field' => sub {
plan tests => 7;
{
my $lexer = make_mock_lexer(
[ 'field', [ 'Domain Name', [], 'DOMAIN.EXAMPLE' ], [] ],
[ 'EOF', undef, [] ],
);
my @errors = validate( rule => 'Required-strict field', lexer => $lexer, grammar => $grammar, types => $types );
eq_or_diff \@errors, [], 'Should accept non-empty field';
is $lexer->line_no(), 2, 'Should consume non-empty field line';
}
{
my $lexer = make_mock_lexer (
['non-empty line', 'gibberish', []],
['EOF', undef, []],
);
my @errors = validate(rule => 'Required-strict field', lexer => $lexer, grammar => $grammar, types => $types);
cmp_ok scalar(@errors), '>=', 1, 'Should reject non-field line';
is $lexer->line_no(), 1, 'Should not consume non-field line';
}
{
my $lexer = make_mock_lexer(
[ 'field', [ 'Domain Name', [], undef ], [] ],
[ 'EOF', undef, [] ],
);
my @errors = validate( rule => 'Required-strict field', lexer => $lexer, grammar => $grammar, types => $types );
cmp_ok scalar(@errors), '>=', 1, 'Should reject empty field';
like $errors[0], qr/line 1/, 'Should refer to line number of the invalid field';
is $lexer->line_no(), 1, 'Should not consume empty field line';
}
};
subtest 'Optional-free subrule' => sub {
plan tests => 3;
......@@ -319,7 +362,7 @@ subtest 'Error propagation' => sub {
['field', ['Domain Name', [], 'DOMAIN.EXAMPLE'], ['BOOM!']],
['EOF', undef, []],
);
my @errors = validate( rule => 'Simple field', lexer => $lexer, grammar => $grammar, types => $types );
my @errors = validate( rule => 'Required field', lexer => $lexer, grammar => $grammar, types => $types );
eq_or_diff \@errors, ['BOOM!'], 'Should propagate errors from lexer';
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment