App-jsonvalidate

 view release on metacpan or  search on metacpan

scripts/jsonvalidate  view on Meta::CPAN

            while( defined( my $line = <STDIN> ) )
            {
                next if $line =~ /\A\s*\z/;
                my $data = _decode_json_or_die( $line, $json, "STDIN: line $.: " );
                _run_one( $js, $data, \$total_ok, \$total_fail, ++$run_idx, $json );
            }
        }
        else
        {
            _message( 5, "No JSON data file was provided, reading from STDIN all in one go." );
            my $data = &_get_stdin();
            _run_one( $js, $data, \$total_ok, \$total_fail, ++$run_idx, $json );
        }
    }
    else
    {
        for my $inst_file ( @{$opts->{instance}} )
        {
            _message( 5, "Processing JSON data file <green>$inst_file</>" );
            if( !$inst_file->exists )
            {
                bailout( "The instance file \"$inst_file\" does not exist." );
            }
            elsif( $inst_file->is_empty )
            {
                _message( 1, "The instance file \"$inst_file\" is empty." );
                next;
            }
            my $raw = $inst_file->load( binmode => 'raw' );
            if( !defined( $raw ) )
            {
                _message( 1, "The instance file \"$inst_file\" content could not be retrieved: ", $inst_file->error );
                next;
            }
    
            if( $opts->{jsonl} )
            {
                _message( 5, "Processing the file <green>$inst_file</> data line-by-line." );
                my $ln = 0;
                for my $line ( split( /\n/, $raw, -1 ) )
                {
                    ++$ln;
                    next if( $line =~ /\A\s*\z/ );
                    my $data = _decode_json_or_die( $line, $json, "$inst_file:$ln: " );
                    _run_one( $js, $data, \$total_ok, \$total_fail, ++$run_idx, $json );
                }
            }
            else
            {
                _message( 5, "Processing the file <green>$inst_file</> data all in one go." );
                my $data = _decode_json_or_die( $raw, $json, "$inst_file: " );
                _run_data_maybe_array( $js, $data, \$total_ok, \$total_fail, \$run_idx, $json );
            }
        }
    }

    if( $opts->{emit_js} )
    {
        my $js_code = $js->compile_js( $opts->{ecma} ? ( ecma => $opts->{ecma} ) : () );
        my $source = join( ', ', map{ $_->basename } @{$opts->{schema}} );
        require DateTime;
        my $now    = DateTime->now->iso8601;
        $out->print( <<HEADER );
// =============================================================================
// JSON Schema Validator — Client-Side (Compiled from Perl)
// =============================================================================
// Schema   : $source
// Generated: $now
// Validator: JSON::Schema::Validate $JSON::Schema::Validate::VERSION (Perl)
// Module   : $INC{'JSON/Schema/Validate.pm'}
// Compiler : jsonvalidate --emit-js
// =============================================================================

HEADER
        $out->print( $js_code );
        return( $total_fail ? 0 : 1 );
    }
    # Summary & exit code
    elsif( !$opts->{quiet} && !$opts->{json} )
    {
        $out->print( "Summary: OK=$total_ok  FAIL=$total_fail\n" );
    }
    # Return 0 on failure, and 1 on success
    _message( 5, "<red>$total_fail</> total fail. Returning ", ( $total_fail ? 0 : 1 ) );
    return( $total_fail ? 0 : 1 );
}

sub _cleanup_and_exit
{
    my $exit = shift( @_ );
    $exit = 0 if( !length( $exit // '' ) || $exit !~ /^\d+$/ );
    exit( $exit );
}

sub _decode_json_or_die
{
    my( $raw, $json, $label ) = @_;
    my $data;
    local $@;
    eval
    {
        $data = $json->decode( $raw );
        1;
    }
    or do
    {
        bailout( "Failed to decode JSON ($label): $@" );
    };
    return( $data );
}

sub _fetch_http_json
{
    my( $url, $json ) = @_;

    local $@;
    eval
    {
        require LWP::UserAgent;
        require HTTP::Request;
        my $ua = LWP::UserAgent->new( timeout => 10 );
        my $res = $ua->get( $url );



( run in 0.816 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )