NAME

JSON::Schema::Validate::Error - JSON Schema Exception

SYNOPSIS

use JSON::Schema::Validate::Error;

# Legacy 2-arg form:
my $err = JSON::Schema::Validate::Error->new( '/user/name', 'minLength violated' );

# Named-arg form (recommended):
my $err = JSON::Schema::Validate::Error->new(
    path            => '/user/name',
    message         => 'string shorter than minLength 1',
    keyword         => 'minLength',
    schema_pointer  => '#/properties/name/minLength',
);

# String context:
say "$err"; # "#/properties/name/minLength → /user/name: string shorter than minLength 1"

# Comparisons (compare by message+path):
say 'same' if( $err eq JSON::Schema::Validate::Error->new( '/user/name', 'string shorter than minLength 1' ) );

# Hash view for serialization
my $h = $err->as_hash;

would return the following hash reference:

{
    keyword         => 'minLength',
    message         => 'string shorter than minLength 1',
    path            => '/user/name',
    schema_pointer  => '#/properties/name/minLength',
}

VERSION

v0.1.0

DESCRIPTION

This class (JSON::Schema::Validate::Error) represents JSON::Schema::Validate errors that are found upon validating the schema provided.

The objects of this class are overloadable, which means they stringify if called in a string context. They can also be serialised as plain hashes.

They can also be compared, such as:

say $err1 eq $err2;

This will not check the two objects are the same, but rather tha their path and their message are identical.

CONSTRUCTOR

my $err = JSON::Schema::Validate::Error->new( $json_pointer, $error_message );

my $err = JSON::Schema::Validate::Error->new(
    path            => $json_pointer,
    message         => $error_message,
    keyword         => $keyword,         # optional (e.g., 'minLength', 'type', '$ref', ...)
    schema_pointer  => $schema_pointer,  # optional JSON Pointer into the schema
);

Instantiate a new JSON::Schema::Validate::Error object, and returns it.

The constructor supports two instantiation patterns:

Legacy

It then takes the following parameters:

  • $json_pointer

    A JSON pointer representing the path in the JSON data where the error occurred.

  • $error_message

    An error text message that describes the error.

Modern

It accepts an hash of the following parameters:

  • keyword

    The schema keyword

  • message

    The error message.

  • path

    The field path in the data.

  • schema_pointer

    The path in the schema.

METHODS

as_string

say $err; # some/path/field: some error message

Returns the human-friendly string representation of the error.

If schema_pointer is set, the format is:

schema_pointer → path: message

otherwise:

path: message

as_hash

my $href = $err->as_hash;

Returns a plain hash reference with keys keyword, message, path, and schema_pointer. Useful for logging or JSON serialisation.

keyword

$err->keyword( 'minLength' );
my $k = $err->keyword; # minLength

Sets or gets the JSON Schema keyword that failed, when known.

message

$err->message( "Some error" );
$err->message; # Some error

Sets or gets the object error message.

path

$err->path( $json_pointer );
$err->path; # /some/where/field

Sets or gets the object error path (JSON pointer).

schema_pointer

$err->schema_pointer( '#/properties/name/minLength' );
my $sp = $err->schema_pointer; # #/properties/name/minLength

Sets or gets the JSON Pointer to the schema location associated with this error.

OPERATOR OVERLOADING

Error objects are overloadable:

  • Stringification

    "$err" calls "as_string".

  • Boolean

    Always true.

  • Equality / Inequality

    eq, ne, ==, and != compare the pair (message, path). This is intentional so tests can assert equivalence of observable failures without caring about object identity.

THREAD SAFETY

This module is thread-safe.

It does not use nor manipulate global variables, and each instance of JSON::Schema::Validate::Error is self-contained and does not share any mutable state across threads.

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

JSON::Schema::Validate

COPYRIGHT & LICENSE

Copyright(c) 2025 DEGUEST Pte. Ltd.

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.