App-CSVUtils
view release on metacpan or search on metacpan
lib/App/CSVUtils/csv_check_field_values.pm view on Meta::CPAN
package App::CSVUtils::csv_check_field_values;
use 5.010001;
use strict;
use warnings;
use Log::ger;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2025-02-04'; # DATE
our $DIST = 'App-CSVUtils'; # DIST
our $VERSION = '1.036'; # VERSION
use App::CSVUtils qw(
gen_csv_util
compile_eval_code
);
gen_csv_util(
name => 'csv_check_field_values',
summary => 'Check the values of whole fields against code/schema',
description => <<'_',
Example `input.csv`:
ingredient,%weight
foo,81
bar,9
baz,10
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 => <<'_',
Code will be given the value of the rows of the field as an array of scalars and
should return a true value if value is valid.
_
},
with_schema => {
summary => 'Check with a Sah schema module',
schema => ['any*', of=>[
['str*', min_len=>1], # string schema
['array*', max_len=>2], # an array schema
]],
description => <<'_',
Should be the name of a Sah schema module without the `Sah::Schema::` prefix,
typically in the `Sah::Schema::array::` subnamespace.
_
completion => sub {
require Complete::Module;
my %args = @_;
$args{word} = "array/" unless length $args{word};
Complete::Module::complete_module(
word => $args{word},
ns_prefix => "Sah::Schema::",
);
},
},
quiet => {
schema => 'bool*',
cmdline_aliases => {q=>{}},
},
},
add_args_rels => {
req_one => ['with_code', 'with_schema'],
},
links => [
{url=>'prog:csv-check-cell-values', summary=>'Check single-cell values'},
{url=>'prog:csv-check-field-names', summary=>'Check the field names'},
],
tags => ['category:checking', 'accepts-schema', 'accepts-code',
#'accepts-regex',
],
writes_csv => 0,
on_input_data_row => sub {
my $r = shift;
# keys we add to the stash
$r->{value} //= [];
push @{ $r->{value} }, $r->{input_row}[ $r->{input_fields_idx}{ $r->{util_args}{field} } ];
},
after_close_input_files => sub {
my $r = shift;
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__
=pod
=encoding UTF-8
=head1 NAME
App::CSVUtils::csv_check_field_values - Check the values of whole fields against code/schema
=head1 VERSION
This document describes version 1.036 of App::CSVUtils::csv_check_field_values (from Perl distribution App-CSVUtils), released on 2025-02-04.
=head1 FUNCTIONS
=head2 csv_check_field_values
Usage:
csv_check_field_values(%args) -> [$status_code, $reason, $payload, \%result_meta]
Check the values of whole fields against codeE<sol>schema.
Example C<input.csv>:
ingredient,%weight
foo,81
bar,9
baz,10
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.
=item * B<input_escape_char> => I<str>
Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.
Defaults to C<\\> (backslash). Overrides C<--input-tsv> option.
=item * B<input_filename> => I<filename> (default: "-")
Input CSV file.
Use C<-> to read from stdin.
Encoding of input file is assumed to be UTF-8.
=item * B<input_header> => I<bool> (default: 1)
Specify whether input CSV has a header row.
By default, the first row of the input CSV will be assumed to contain field
names (and the second row contains the first data row). When you declare that
input CSV does not have header row (C<--no-input-header>), the first row of the
CSV is assumed to contain the first data row. Fields will be named C<field1>,
C<field2>, and so on.
=item * B<input_quote_char> => I<str>
Specify field quote character in input CSV, will be passed to Text::CSV_XS.
Defaults to C<"> (double quote). Overrides C<--input-tsv> option.
=item * B<input_sep_char> => I<str>
Specify field separator character in input CSV, will be passed to Text::CSV_XS.
Defaults to C<,> (comma). Overrides C<--input-tsv> option.
=item * B<input_skip_num_lines> => I<posint>
Number of lines to skip before header row.
This 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.
See also the alternative option: C<--input-skip-until-pattern>.
=item * B<input_skip_until_pattern> => I<re_from_str>
Skip rows until the first header row matches a regex pattern.
( run in 0.545 second using v1.01-cache-2.11-cpan-140bd7fdf52 )