| 1 | package App::CSVUtils::csv_check_cell_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_cell_values', | ||||||||||||||||||||
| 20 | summary => 'Check the value of single cells of CSV against code/schema/regex', | ||||||||||||||||||||
| 21 | description => <<'MARKDOWN', | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | Example `input.csv`: | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | ingredient,%weight | ||||||||||||||||||||
| 26 | foo,81 | ||||||||||||||||||||
| 27 | bar,9 | ||||||||||||||||||||
| 28 | baz,10 | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Check that ingredients do not contain number: | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | % csv-check-cell-values input.csv -f ingredient --with-regex '/\\A[A-Za-z ]+\\z/' | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | Check that all %weight is between 0 and 100: | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | % csv-check-cell-values input.csv -f %weight --with-code '$_>0 && $_<=100' | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | MARKDOWN | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | add_args => { | ||||||||||||||||||||
| 41 | %App::CSVUtils::argspecsopt_field_selection, | ||||||||||||||||||||
| 42 | with_code => { | ||||||||||||||||||||
| 43 | summary => 'Check with Perl code', | ||||||||||||||||||||
| 44 | schema => $App::CSVUtils::sch_req_str_or_code, | ||||||||||||||||||||
| 45 | description => <<'MARKDOWN', | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | Code will be given the value of the cell and should return a true value if value | ||||||||||||||||||||
| 48 | is valid. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | MARKDOWN | ||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||
| 52 | with_schema => { | ||||||||||||||||||||
| 53 | summary => 'Check with a Sah schema', | ||||||||||||||||||||
| 54 | schema => ['any*', of=>[ | ||||||||||||||||||||
| 55 | ['str*', min_len=>1], # string schema | ||||||||||||||||||||
| 56 | ['array*', max_len=>2], # an array schema | ||||||||||||||||||||
| 57 | ]], | ||||||||||||||||||||
| 58 | 0 | ▶ | completion => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 59 | 0 | require Complete::Module; | |||||||||||||||||||
| 60 | 0 | my %args = @_; | |||||||||||||||||||
| 61 | Complete::Module::complete_module( | ||||||||||||||||||||
| 62 | word => $args{word}, | ||||||||||||||||||||
| 63 | 0 | ns_prefix => "Sah::Schema::", | |||||||||||||||||||
| 64 | ); | ||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||
| 66 | }, | ||||||||||||||||||||
| 67 | with_regex => { | ||||||||||||||||||||
| 68 | schema => 're_from_str*', | ||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | quiet => { | ||||||||||||||||||||
| 72 | schema => 'bool*', | ||||||||||||||||||||
| 73 | cmdline_aliases => {q=>{}}, | ||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||
| 75 | print_validated => { | ||||||||||||||||||||
| 76 | summary => 'Print the validated values of each cell', | ||||||||||||||||||||
| 77 | schema => 'bool*', | ||||||||||||||||||||
| 78 | description => <<'MARKDOWN', | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | When validating with schema, will print each validated (possible coerced, | ||||||||||||||||||||
| 81 | filtered) value of each cell. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | MARKDOWN | ||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||
| 86 | add_args_rels => { | ||||||||||||||||||||
| 87 | req_one => ['with_code', 'with_schema', 'with_regex'], | ||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | links => [ | ||||||||||||||||||||
| 91 | {url=>'prog:csv-check-field-values', summary=>'Check of the values of whole fields'}, | ||||||||||||||||||||
| 92 | ], | ||||||||||||||||||||
| 93 | tags => ['accepts-schema', 'accepts-regex', 'category:checking'], | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | examples => [ | ||||||||||||||||||||
| 96 | { | ||||||||||||||||||||
| 97 | summary => 'Check whether the `rank` field has monotonically increasing values', | ||||||||||||||||||||
| 98 | argv => ['formula.csv', '-f', 'rank', '--with-schema', 'array/num//monotonically_increasing'], | ||||||||||||||||||||
| 99 | test => 0, | ||||||||||||||||||||
| 100 | 'x.doc.show_result' => 0, | ||||||||||||||||||||
| 101 | }, | ||||||||||||||||||||
| 102 | ], | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | writes_csv => 0, | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=13 SCAR=52.0
| |||||||||||||||||||||
| 107 | 0 | my $r = shift; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | # key we add to the stash | ||||||||||||||||||||
| 110 | 0 | ▶ | unless (defined $r->{code}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | ▶ | if ($r->{util_args}{with_schema}) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 112 | 0 | require Data::Sah; | |||||||||||||||||||
| 113 | 0 | my $sch = $r->{util_args}{with_schema}; | |||||||||||||||||||
| 114 | 0 | ▶ | if (!ref($sch)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | $sch =~ s!/!::!g; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | 0 | $r->{code} = Data::Sah::gen_validator($sch, {return_type=>"str_errmsg+val"}); | |||||||||||||||||||
| 118 | } elsif ($r->{util_args}{with_code}) { | ||||||||||||||||||||
| 119 | 0 | my $code0 = compile_eval_code($r->{util_args}{with_code}, 'with_code'); | |||||||||||||||||||
| 120 | 0 | ▶ | $r->{code} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 121 | 0 | local $_ = $_[0]; my $res = $code0->($_); | |||||||||||||||||||
| 122 | 0 | ▶ | [($res ? "":"FAIL"), $res]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | }; | |||||||||||||||||||
| 124 | } elsif (defined $r->{util_args}{with_regex}) { | ||||||||||||||||||||
| 125 | 0 | ▶ | $r->{code} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 126 | 0 | ▶ | $_[0] =~ $r->{util_args}{with_regex} ? ["", $_[0]] : ["Does not match regex $r->{util_args}{with_regex}", $_[0]]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 0 | }; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # key we add to the stash | ||||||||||||||||||||
| 132 | 0 | ▶ | unless ($r->{selected_fields_idx_array_sorted}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | my $res = App::CSVUtils::_select_fields($r->{input_fields}, $r->{input_fields_idx}, $r->{util_args}); | |||||||||||||||||||
| 134 | 0 | ▶ | die $res unless $res->[0] == 100; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | my $selected_fields = $res->[2][0]; | |||||||||||||||||||
| 136 | 0 | my $selected_fields_idx_array = $res->[2][1]; | |||||||||||||||||||
| 137 | 0 | ▶ | die [412, "At least one field must be selected"] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | unless @$selected_fields; | ||||||||||||||||||||
| 139 | 0 | $r->{selected_fields_idx_array_sorted} = [sort { $b <=> $a } @$selected_fields_idx_array]; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | for my $idx (@{ $r->{selected_fields_idx_array_sorted} }) { | |||||||||||||||||||
| 143 | 0 | my $res = $r->{code}->( $r->{input_row}[$idx] ); | |||||||||||||||||||
| 144 | 0 | ▶ | if ($res->[0]) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 0 | my $msg = "Row #$r->{input_data_rownum} field '$r->{input_fields}[$idx]': Value '$r->{input_row}[$idx]' does NOT validate: $res->[0]"; | |||||||||||||||||||
| 146 | 0 | ▶ | $r->{result} = [400, $msg, $r->{util_args}{quiet} ? undef : $msg]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 0 | $r->{wants_skip_files}++; | |||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||
| 149 | 0 | ▶ | if ($r->{util_args}{print_validated}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 0 | print $res->[1], "\n"; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 0 | ▶ | after_close_input_files => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 157 | 0 | my $r = shift; | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | $r->{result} //= [200, "OK", $r->{util_args}{quiet} ? undef : "All cells validate"]; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$r->{"result"} //= [200, "OK", $r->{"util_args"}{"quiet"} ? undef : "All cells validate"]
MC/DC0%0/1
$r->{"result"} //= [200, "OK", $r->{"util_args"}{"quiet"} ? undef : "All cells validate"]
$r->{"result"}
Truth table0%0/2
A
A= $r->{"result"}
| |||||||||||||||||||||
| 160 | }, | ||||||||||||||||||||
| 161 | 1 | ); | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 1 | 1; | |||||||||||||||||||
| 164 | # ABSTRACT: Check the value of single cells of CSV against code/schema/regex | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||