App-CSVUtils

 view release on metacpan or  search on metacpan

lib/App/CSVUtils/csv_check_cell_values.pm  view on Meta::CPAN

            },
        },
        with_regex => {
            schema => 're_from_str*',
        },

        quiet => {
            schema => 'bool*',
            cmdline_aliases => {q=>{}},
        },
        print_validated => {
            summary => 'Print the validated values of each cell',
            schema => 'bool*',
            description => <<'_',

When validating with schema, will print each validated (possible coerced,
filtered) value of each cell.

_
        },
    },
    add_args_rels => {
        req_one => ['with_code', 'with_schema', 'with_regex'],
    },

    links => [

lib/App/CSVUtils/csv_check_cell_values.pm  view on Meta::CPAN

            my $selected_fields = $res->[2][0];
            my $selected_fields_idx_array = $res->[2][1];
            die [412, "At least one field must be selected"]
                unless @$selected_fields;
            $r->{selected_fields_idx_array_sorted} = [sort { $b <=> $a } @$selected_fields_idx_array];
        }

        for my $idx (@{ $r->{selected_fields_idx_array_sorted} }) {
            my $res = $r->{code}->( $r->{input_row}[$idx] );
            if ($res->[0]) {
                my $msg = "Row #$r->{input_data_rownum} field '$r->{input_fields}[$idx]': Value '$r->{input_row}[$idx]' does NOT validate: $res->[0]";
                $r->{result} = [400, $msg, $r->{util_args}{quiet} ? undef : $msg];
                $r->{wants_skip_files}++;
            } else {
                if ($r->{util_args}{print_validated}) {
                    print $res->[1], "\n";
                }
            }
        }
    },

    after_close_input_files => sub {
        my $r = shift;

        $r->{result} //= [200, "OK", $r->{util_args}{quiet} ? undef : "All cells validate"];
    },
);

1;
# ABSTRACT: Check the value of single cells of CSV against code/schema/regex

__END__

=pod

lib/App/CSVUtils/csv_check_cell_values.pm  view on Meta::CPAN

pattern.

=item * B<input_tsv> => I<true>

Inform that input file is in TSV (tab-separated) format instead of CSV.

Overriden by C<--input-sep-char>, C<--input-quote-char>, C<--input-escape-char>
options. If one of those options is specified, then C<--input-tsv> will be
ignored.

=item * B<print_validated> => I<bool>

Print the validated values of each cell.

When validating with schema, will print each validated (possible coerced,
filtered) value of each cell.

=item * B<quiet> => I<bool>

(No description)

=item * B<show_selected_fields> => I<true>

Show selected fields and then immediately exit.

lib/App/CSVUtils/csv_check_field_values.pm  view on Meta::CPAN

Example `input2.csv`:

    ingredient,%weight
    foo,81
    bar,9
    baz,10

Check that ingredients are sorted in descending %weight:

    % csv-check-field-values input.csv %weight --with-schema array::num::rev_sorted
    ERROR 400: Field '%weight' does not validate with schema 'array::num::rev_sorted'

    % csv-check-field-values input2.csv %weight --with-schema array::num::rev_sorted
    Field '%weight' validates with schema 'array::num::rev_sorted'

_

    add_args => {
        %App::CSVUtils::argspec_field_1,
        with_code => {
            summary => 'Check with Perl code',
            schema => $App::CSVUtils::sch_req_str_or_code,
            description => <<'_',

lib/App/CSVUtils/csv_check_field_values.pm  view on Meta::CPAN

        if ($r->{util_args}{with_schema}) {
            require Data::Dmp;
            require Data::Sah;
            my $sch = $r->{util_args}{with_schema};
            if (!ref($sch)) {
                $sch =~ s!/!::!g;
            }
            my $vdr = Data::Sah::gen_validator($sch, {return_type=>"str_errmsg"});
            my $res = $vdr->($r->{value});
            if ($res) {
                my $msg = "Field '$r->{util_args}{field}' does NOT validate with schema ".Data::Dmp::dmp($sch).": $res";
                $r->{result} = [400, $msg, $r->{util_args}{quiet} ? undef : $msg];
            } else {
                my $msg = "Field '$r->{util_args}{field}' validates with schema ".Data::Dmp::dmp($sch);
                $r->{result} = [200, "Sorted", $r->{util_args}{quiet} ? undef : $msg];
            }
        } elsif ($r->{util_args}{with_code}) {
            my $code = compile_eval_code($r->{util_args}{with_code}, 'with_code');
            my $res; { local $_ = $r->{value}; $res = $code->($_) }
            if (!$res) {
                my $msg = "Field '$r->{util_args}{field}' does NOT validate with code'";
                $r->{result} = [400, $msg, $r->{util_args}{quiet} ? undef : $msg];
            } else {
                my $msg = "Field '$r->{util_args}{field}' validates with code";
                $r->{result} = [200, "Sorted", $r->{util_args}{quiet} ? undef : $msg];
            }
        }
    },
);

1;
# ABSTRACT: Check the values of whole fields against code/schema

__END__

lib/App/CSVUtils/csv_check_field_values.pm  view on Meta::CPAN

Example C<input2.csv>:

 ingredient,%weight
 foo,81
 bar,9
 baz,10

Check that ingredients are sorted in descending %weight:

 % csv-check-field-values input.csv %weight --with-schema array::num::rev_sorted
 ERROR 400: Field '%weight' does not validate with schema 'array::num::rev_sorted'
 
 % csv-check-field-values input2.csv %weight --with-schema array::num::rev_sorted
 Field '%weight' validates with schema 'array::num::rev_sorted'

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4

=item * B<field>* => I<str>

Field name.

script/csv-check-cell-values  view on Meta::CPAN

=head1 VERSION

This document describes version 1.036 of csv-check-cell-values (from Perl distribution App-CSVUtils), released on 2025-02-04.

=head1 SYNOPSIS

B<csv-check-cell-values> B<L<--help|/"--help, -h, -?">> (or B<L<-h|/"--help, -h, -?">>, B<L<-?|/"--help, -h, -?">>)

B<csv-check-cell-values> B<L<--version|/"--version, -v">> (or B<L<-v|/"--version, -v">>)

B<csv-check-cell-values> [B<L<--debug|/"--debug">>|B<L<--log-level|/"--log-level=s">>=I<level>|B<L<--quiet|/"--quiet">>|B<L<--trace|/"--trace">>|B<L<--verbose|/"--verbose">>] [B<L<--exclude-field-pat|/"--exclude-field-pat=s">>=I<re>|B<L<-A|/"--exclud...



See examples in the L</EXAMPLES> section.

=head1 DESCRIPTION

Example C<input.csv>:

 ingredient,%weight

script/csv-check-cell-values  view on Meta::CPAN

This is an alternative to the C<--input-skip-num-lines> and can be useful if you
have a CSV files (usually some generated reports, sometimes converted from
spreadsheet) that have additional header lines or info before the CSV header
row.

With C<--input-skip-num-lines>, you skip a fixed number of lines. With this
option, rows will be skipped until the first field matches the specified regex
pattern.


=item B<--print-validated>

Print the validated values of each cell.

When validating with schema, will print each validated (possible coerced,
filtered) value of each cell.


=item B<--quiet-arg>, B<-q>

(No description)


=item B<--show-selected-fields>

script/csv-check-field-values  view on Meta::CPAN

Example C<input2.csv>:

 ingredient,%weight
 foo,81
 bar,9
 baz,10

Check that ingredients are sorted in descending %weight:

 % csv-check-field-values input.csv %weight --with-schema array::num::rev_sorted
 ERROR 400: Field '%weight' does not validate with schema 'array::num::rev_sorted'
 
 % csv-check-field-values input2.csv %weight --with-schema array::num::rev_sorted
 Field '%weight' validates with schema 'array::num::rev_sorted'

=head1 OPTIONS

C<*> marks required options.

=head2 Main options

=over

=item B<--field>=I<s>*, B<-f>



( run in 0.527 second using v1.01-cache-2.11-cpan-4d50c553e7e )