App-jsonvalidate
view release on metacpan or search on metacpan
scripts/jsonvalidate view on Meta::CPAN
{
$by_id{ $s->{'$id'} } = $s;
}
}
$js->set_resolver(sub
{
my( $abs_uri ) = @_;
my $orig_uri = $abs_uri;
# Strip fragment for all decisions below
my( $doc_uri, $fragment ) = ( $abs_uri =~ /^([^#]+)(#.*)?$/ );
$doc_uri //= $abs_uri;
my $has_fragment = defined( $fragment ) && length( $fragment );
# First: exact $id match from any schema we were given on the command line
if( exists( $by_id{ $abs_uri } ) || exists( $by_id{ $doc_uri } ) )
{
my $schema = $by_id{ $abs_uri } // $by_id{ $doc_uri };
_message( 6, "Resolver: found pre-loaded schema for ", $by_id{ $abs_uri } ? $abs_uri : $doc_uri );
return( $schema );
}
# Optional: allow relative/local file refs (very common and safe)
scripts/jsonvalidate view on Meta::CPAN
_message( 6, "Resolver: loading file:// URI $path" );
return( $path->load_json );
}
}
}
# Only if user explicitly asked for remote fetching
# Can also be called as --allow_http. It is aliased to --remote_refs
if( $doc_uri =~ m{^https?://}i )
{
# The specs say that, if there is a fragment, we should always fetch the remote document.
my $should_fetch = $has_fragment || $opts->{remote_refs};
if( $should_fetch )
{
_message( 5, "Resolver: fetching remote document $doc_uri (fragment=$has_fragment, remote-refs=", ( $opts->{remote_refs} // 0 ), ")" );
my $doc = _fetch_http_json( $doc_uri, $json );
return( $doc ) if( defined( $doc ) );
}
else
{
_message( 4, "Resolver: refusing to fetch remote $doc_uri (no fragment and --remote-refs not used)" );
return;
}
}
my $path = file( $doc_uri );
# 4. Last resort: maybe it's a bare filename without base?
if( $path->exists && $path->is_file )
{
_message( 6, "Resolver: fallback loading bare path $path" );
return( $path->load_json );
}
# Fail loudly with helpful message
bailout( <<EOF );
Cannot resolve \$ref "$orig_uri"
Document URI: $doc_uri
Fragment : $fragment
This is probably a fictional/internal \$id (very common and correct!).
The validator will NOT auto-fetch such URIs by default (security + correctness).
This looks like a reference to a remote document with a JSON Pointer fragment.
Such references MUST be retrieved (per JSON Schema spec).
Solutions:
⢠Pass --remote-refs to allow HTTP(S) fetching
⢠Provide the referenced schema via additional --schema arguments
⢠Rewrite the schema to use relative references + --schema-base
EOF
});
# Optional: capture $comment into trace (no-op handler by default)
( run in 1.496 second using v1.01-cache-2.11-cpan-b16cb0d3907 )