| 1 | package App::CSVUtils::csv_check_field_values; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Log::ger; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 9 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||||||
| 10 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||||
| 11 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | ▶ | use App::CSVUtils qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | gen_csv_util | ||||||||||||||
| 15 | compile_eval_code | ||||||||||||||
| 16 | 1 | ); | |||||||||||||
| 17 | |||||||||||||||
| 18 | gen_csv_util( | ||||||||||||||
| 19 | name => 'csv_check_field_values', | ||||||||||||||
| 20 | summary => 'Check the values of whole fields against code/schema', | ||||||||||||||
| 21 | description => <<'MARKDOWN', | ||||||||||||||
| 22 | |||||||||||||||
| 23 | Example `input.csv`: | ||||||||||||||
| 24 | |||||||||||||||
| 25 | ingredient,%weight | ||||||||||||||
| 26 | foo,81 | ||||||||||||||
| 27 | bar,9 | ||||||||||||||
| 28 | baz,10 | ||||||||||||||
| 29 | |||||||||||||||
| 30 | Example `input2.csv`: | ||||||||||||||
| 31 | |||||||||||||||
| 32 | ingredient,%weight | ||||||||||||||
| 33 | foo,81 | ||||||||||||||
| 34 | bar,9 | ||||||||||||||
| 35 | baz,10 | ||||||||||||||
| 36 | |||||||||||||||
| 37 | Check that ingredients are sorted in descending %weight: | ||||||||||||||
| 38 | |||||||||||||||
| 39 | % csv-check-field-values input.csv %weight --with-schema array::num::rev_sorted | ||||||||||||||
| 40 | ERROR 400: Field '%weight' does not validate with schema 'array::num::rev_sorted' | ||||||||||||||
| 41 | |||||||||||||||
| 42 | % csv-check-field-values input2.csv %weight --with-schema array::num::rev_sorted | ||||||||||||||
| 43 | Field '%weight' validates with schema 'array::num::rev_sorted' | ||||||||||||||
| 44 | |||||||||||||||
| 45 | MARKDOWN | ||||||||||||||
| 46 | |||||||||||||||
| 47 | add_args => { | ||||||||||||||
| 48 | %App::CSVUtils::argspec_field_1, | ||||||||||||||
| 49 | with_code => { | ||||||||||||||
| 50 | summary => 'Check with Perl code', | ||||||||||||||
| 51 | schema => $App::CSVUtils::sch_req_str_or_code, | ||||||||||||||
| 52 | description => <<'MARKDOWN', | ||||||||||||||
| 53 | |||||||||||||||
| 54 | Code will be given the value of the rows of the field as an array of scalars and | ||||||||||||||
| 55 | should return a true value if value is valid. | ||||||||||||||
| 56 | |||||||||||||||
| 57 | MARKDOWN | ||||||||||||||
| 58 | }, | ||||||||||||||
| 59 | with_schema => { | ||||||||||||||
| 60 | summary => 'Check with a Sah schema module', | ||||||||||||||
| 61 | schema => ['any*', of=>[ | ||||||||||||||
| 62 | ['str*', min_len=>1], # string schema | ||||||||||||||
| 63 | ['array*', max_len=>2], # an array schema | ||||||||||||||
| 64 | ]], | ||||||||||||||
| 65 | description => <<'MARKDOWN', | ||||||||||||||
| 66 | |||||||||||||||
| 67 | Should be the name of a Sah schema module without the `Sah::Schema::` prefix, | ||||||||||||||
| 68 | typically in the `Sah::Schema::array::` subnamespace. | ||||||||||||||
| 69 | |||||||||||||||
| 70 | MARKDOWN | ||||||||||||||
| 71 | 0 | ▶ | completion => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 72 | 0 | require Complete::Module; | |||||||||||||
| 73 | 0 | my %args = @_; | |||||||||||||
| 74 | 0 | ▶ | $args{word} = "array/" unless length $args{word}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | Complete::Module::complete_module( | ||||||||||||||
| 76 | word => $args{word}, | ||||||||||||||
| 77 | 0 | ns_prefix => "Sah::Schema::", | |||||||||||||
| 78 | ); | ||||||||||||||
| 79 | }, | ||||||||||||||
| 80 | }, | ||||||||||||||
| 81 | quiet => { | ||||||||||||||
| 82 | schema => 'bool*', | ||||||||||||||
| 83 | cmdline_aliases => {q=>{}}, | ||||||||||||||
| 84 | }, | ||||||||||||||
| 85 | }, | ||||||||||||||
| 86 | add_args_rels => { | ||||||||||||||
| 87 | req_one => ['with_code', 'with_schema'], | ||||||||||||||
| 88 | }, | ||||||||||||||
| 89 | links => [ | ||||||||||||||
| 90 | {url=>'prog:csv-check-cell-values', summary=>'Check single-cell values'}, | ||||||||||||||
| 91 | {url=>'prog:csv-check-field-names', summary=>'Check the field names'}, | ||||||||||||||
| 92 | ], | ||||||||||||||
| 93 | tags => ['category:checking', 'accepts-schema', 'accepts-code', | ||||||||||||||
| 94 | #'accepts-regex', | ||||||||||||||
| 95 | ], | ||||||||||||||
| 96 | |||||||||||||||
| 97 | writes_csv => 0, | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 100 | 0 | my $r = shift; | |||||||||||||
| 101 | |||||||||||||||
| 102 | # keys we add to the stash | ||||||||||||||
| 103 | 0 | ▶ | $r->{value} //= []; | ||||||||||||
|
Condition0%0/2
$r->{"value"} //= []
MC/DC0%0/1
$r->{"value"} //= []
$r->{"value"}
Truth table0%0/2
A
A= $r->{"value"}
| |||||||||||||||
| 104 | |||||||||||||||
| 105 | 0 | push @{ $r->{value} }, $r->{input_row}[ $r->{input_fields_idx}{ $r->{util_args}{field} } ]; | |||||||||||||
| 106 | }, | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | ▶ | after_close_input_files => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||||
| 109 | 0 | my $r = shift; | |||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | ▶ | if ($r->{util_args}{with_schema}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 112 | 0 | require Data::Dmp; | |||||||||||||
| 113 | 0 | require Data::Sah; | |||||||||||||
| 114 | 0 | my $sch = $r->{util_args}{with_schema}; | |||||||||||||
| 115 | 0 | ▶ | if (!ref($sch)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 0 | $sch =~ s!/!::!g; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | 0 | my $vdr = Data::Sah::gen_validator($sch, {return_type=>"str_errmsg"}); | |||||||||||||
| 119 | 0 | my $res = $vdr->($r->{value}); | |||||||||||||
| 120 | 0 | ▶ | if ($res) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 0 | my $msg = "Field '$r->{util_args}{field}' does NOT validate with schema ".Data::Dmp::dmp($sch).": $res"; | |||||||||||||
| 122 | 0 | ▶ | $r->{result} = [400, $msg, $r->{util_args}{quiet} ? undef : $msg]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | } else { | ||||||||||||||
| 124 | 0 | my $msg = "Field '$r->{util_args}{field}' validates with schema ".Data::Dmp::dmp($sch); | |||||||||||||
| 125 | 0 | ▶ | $r->{result} = [200, "Sorted", $r->{util_args}{quiet} ? undef : $msg]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | } | ||||||||||||||
| 127 | } elsif ($r->{util_args}{with_code}) { | ||||||||||||||
| 128 | 0 | my $code = compile_eval_code($r->{util_args}{with_code}, 'with_code'); | |||||||||||||
| 129 | 0 | my $res; { local $_ = $r->{value}; $res = $code->($_) } | |||||||||||||
| 130 | 0 | ▶ | if (!$res) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | my $msg = "Field '$r->{util_args}{field}' does NOT validate with code'"; | |||||||||||||
| 132 | 0 | ▶ | $r->{result} = [400, $msg, $r->{util_args}{quiet} ? undef : $msg]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | } else { | ||||||||||||||
| 134 | 0 | my $msg = "Field '$r->{util_args}{field}' validates with code"; | |||||||||||||
| 135 | 0 | ▶ | $r->{result} = [200, "Sorted", $r->{util_args}{quiet} ? undef : $msg]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | } | ||||||||||||||
| 137 | } | ||||||||||||||
| 138 | }, | ||||||||||||||
| 139 | 1 | ); | |||||||||||||
| 140 | |||||||||||||||
| 141 | 1 | 1; | |||||||||||||
| 142 | # ABSTRACT: Check the values of whole fields against code/schema | ||||||||||||||
| 143 | |||||||||||||||
| 144 | __END__ | ||||||||||||||