App-SahUtils

 view release on metacpan or  search on metacpan

script/validate-with-sah  view on Meta::CPAN

#!perl

use 5.010;
use strict;
use warnings;

use Perinci::CmdLine::Any;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2024-08-06'; # DATE
our $DIST = 'App-SahUtils'; # DIST
our $VERSION = '0.485'; # VERSION

our %SPEC;

$SPEC{validate_with_sah} = {
    v => 1.1,
    summary => 'Validate data with Sah schema',
    description => <<'_',

This script is useful for testing Sah schemas. You can quickly specify from the
CLI a schema with some data to validate it against. This script can also be used
to just normalize a Sah schema and show it (`--show-schema`), or compile a
schema and show the raw compilation result (`--show-raw-compile`), or generate
validator code and show it (`--show-code`).

_
    args_rels => {
        'choose_one&' => [
            [qw/show_schema show_raw_compile show_code/],
            [qw/data multiple_data data_file multiple_data_file/],
        ],
        'req_one&' => [
            [qw/schema schema_file schema_module/],
        ],
        # XXX: data multiple_data data_file multiple_data_file only required if there are now show_* arguments
        # doesn't work yet?
        #'dep_any&' => [
        #    ['linenum', [qw/show_code/]],
        #    ['schema_file_type', [qw/schema_file/]],
        #    ['data_file_type', [qw/data_file multiple_data_file/]],
        #],
    },
    args => {
        schema => {
            #schema=>['any*', prefilters=>['Str::try_decode_json']], # doesn't work yet because Perinci::Sub::GetArgs::Argv croaks before we get to here
            schema=>['any*'],
            pos=>0,
            tags => ['category:schema-specification'],
        },
        schema_file => {
            schema=>'str*',
            summary => 'Retrieve schema from file',
            description => <<'_',

JSON, YAML, and Perl formats are supported. File type will be guessed from
filename, defaults to JSON.

_
            cmdline_aliases => {f=>{}},
            'x.schema.entity' => 'filename',
            tags => ['category:schema-specification'],
        },
        schema_file_type => {
            schema=>['str*', in=>[qw/json yaml perl/]],
            summary => 'Give hint for schema file type',
            cmdline_aliases => {t=>{}},
            tags => ['category:schema-specification'],
        },
        schema_module => {
            schema => ['str*', match => qr/\A\w+(\::\w+)*\z/],
            cmdline_aliases => {m=>{}},
            tags => ['category:schema-specification'],
            completion => sub {
                require Complete::Module;
                my %args = @_;
                Complete::Module::complete_module(
                    word => $args{word}, ns_prefix => 'Sah::Schema',
                );
            },
        },

        data => {
            schema => ['any'],
            pos => 1,
            tags => ['category:data-specification'],
        },
        multiple_data => {
            summary => 'Validate multiple data (array of data) against schema',
            schema => ['array*', of=>'any'],
            tags => ['category:data-specification'],
        },
        data_file => {
            schema=>'str*',
            summary => 'Retrieve data from file',
            description => <<'_',

JSON, YAML, and Perl formats are supported. File type will be guessed from
filename, defaults to JSON.

_
            'x.schema.entity' => 'filename',
            tags => ['category:data-specification'],
        },
        multiple_data_file => {
            schema=>'str*',
            summary => 'Retrieve multiple data from file',
            description => <<'_',

This is like `data_file` except that for multiple data. Data must be an array.

_
            'x.schema.entity' => 'filename',
            tags => ['category:data-specification'],
        },
        data_file_type => {
            schema=>['str*', in=>[qw/json yaml perl/]],
            summary => 'Give hint for data file type',
            tags => ['category:data-specification'],
        },

        return_type => {
            schema=>['str*', {
                in => [qw/bool_valid bool_valid+val str_errmsg str_errmsg+val hash_details/],
                prefilters => [
                    ["Str::replace_map", {map=>{
                        "bool"     => "bool_valid",
                        "bool+val" => "bool_valid+val",
                        "str"      => "str_errmsg",
                        "str+val"  => "str_errmsg+val",
                        "full"     => "hash_details",
                    }}],
                ],
            }],
            default=>'str',
            cmdline_aliases => {
                r => {},
                bool => {
                    is_flag => 1,
                    summary => 'Shortcut for --return-type bool',
                    code => sub { $_[0]{return_type} = 'bool' },
                },
                bool_val => {
                    is_flag => 1,
                    summary => 'Shortcut for --return-type bool+val',
                    code => sub { $_[0]{return_type} = 'bool+val' },
                },
                str_val => {
                    is_flag => 1,
                    summary => 'Shortcut for --return-type str+val',
                    code => sub { $_[0]{return_type} = 'str+val' },
                },
                full => {
                    is_flag => 1,
                    summary => 'Shortcut for --return-type full',
                    code => sub { $_[0]{return_type} = 'full' },
                },
            },

script/validate-with-sah  view on Meta::CPAN

=head2 Configuration options

=over

=item B<--config-path>=I<s>

Set path to configuration file.

Can actually be specified multiple times to instruct application to read from
multiple configuration files (and merge them).


Can be specified multiple times.

=item B<--config-profile>=I<s>, B<-P>

Set configuration profile to use.

A single configuration file can contain profiles, i.e. alternative sets of
values that can be selected. For example:

 [profile=dev]
 username=foo
 pass=beaver
 
 [profile=production]
 username=bar
 pass=honey

When you specify C<--config-profile=dev>, C<username> will be set to C<foo> and
C<password> to C<beaver>. When you specify C<--config-profile=production>,
C<username> will be set to C<bar> and C<password> to C<honey>.


=item B<--no-config>

Do not use any configuration file.

If you specify C<--no-config>, the application will not read any configuration
file.


=back

=head2 Data specification options

=over

=item B<--data-file-type>=I<s>

Give hint for data file type.

Valid values:

 ["json","yaml","perl"]

=item B<--data-file>=I<filename>

Retrieve data from file.

JSON, YAML, and Perl formats are supported. File type will be guessed from
filename, defaults to JSON.


=item B<--data-json>=I<s>

See C<--data>.

Can also be specified as the 2nd command-line argument.

=item B<--data>=I<s>

(No description)


Can also be specified as the 2nd command-line argument.

=item B<--multiple-data-file>=I<filename>

Retrieve multiple data from file.

This is like C<data_file> except that for multiple data. Data must be an array.


=item B<--multiple-data-json>=I<s>

Validate multiple data (array of data) against schema (JSON-encoded).

See C<--multiple-data>.

=item B<--multiple-data>=I<s>

Validate multiple data (array of data) against schema.

=back

=head2 Environment options

=over

=item B<--no-env>

Do not read environment for default options.

If you specify C<--no-env>, the application wil not read any environment
variable.


=back

=head2 Logging options

=over

=item B<--debug>

Shortcut for --log-level=debug.

=item B<--log-level>=I<s>

Set log level.

script/validate-with-sah  view on Meta::CPAN

=item B<--json>

Set output format to json.

=item B<--linenum>, B<-l>

When showing source code, add line numbers.

=item B<--no-naked-res>

When outputing as JSON, add result envelope.

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

 [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing. In this case you can use
C<--naked-res> so you just get:

 [1,2,3]


=item B<--page-result>

Filter output through a pager.

This option will pipe the output to a specified pager program. If pager program
is not specified, a suitable default e.g. C<less> is chosen.


=item B<--view-result>

View output using a viewer.

This option will first save the output to a temporary file, then open a viewer
program to view the temporary file. If a viewer program is not chosen, a
suitable default, e.g. the browser, is chosen.


=back

=head2 Schema specification options

=over

=item B<--schema-file-type>=I<s>, B<-t>

Give hint for schema file type.

Valid values:

 ["json","yaml","perl"]

=item B<--schema-file>=I<filename>, B<-f>

Retrieve schema from file.

JSON, YAML, and Perl formats are supported. File type will be guessed from
filename, defaults to JSON.


=item B<--schema-json>=I<s>

See C<--schema>.

Can also be specified as the 1st command-line argument.

=item B<--schema-module>=I<s>, B<-m>

(No description)


=item B<--schema>=I<s>

(No description)


Can also be specified as the 1st command-line argument.

=back

=head2 Validator specification options

=over

=item B<--bool>

Shortcut for --return-type bool.

See C<--return-type>.

=item B<--bool-val>

Shortcut for --return-type bool+val.

See C<--return-type>.

=item B<--compiler>=I<s>, B<-C>

Select compiler.

Default value:

 "perl"

Valid values:

 ["perl","js"]

=item B<--core>

Generate Perl validator that avoids the use of non-core modules.

=item B<--core-or-pp>

Generate Perl validator that only uses core or pure-perl modules.

=item B<--full>



( run in 0.723 second using v1.01-cache-2.11-cpan-39bf76dae61 )