NAME

JSON::Schema::Fit - adjust data structure according to json-schema

VERSION

version 0.07

SYNOPSIS

my $data = get_dirty_result();
# raw data: got { num => "1.99999999997", flag => "1", junk => {...} }
my $bad_json = encode_json $data;
# {"num":"1.99999999997","flag":"1","junk":{...}}

# JSON::Schema-compatible
my $schema = { type => 'object', additionalProperties => 0, properties => {
    num => { type => 'integer' },
    flag => { type => 'boolean' },
}};
my $prepared_data = JSON::Schema::Fit->new()->get_adjusted($data, $schema);
my $cool_json = encode_json $prepared_data;
# {"num":2,"flag":true}

DESCRIPTION

The main goal of this package is preparing data to be encoded as json according to schema.

Actions implemented: adjusting value type (number/string/boolean), rounding numbers, filtering hash keys.

ATTRIBUTES

booleans

Explicitly set type for boolean values to JSON::true / JSON::false

Default: 1

numbers

Explicitly set type for numeric and integer values

Default: 1

round_numbers

Round numbers according to 'multipleOf' schema value

Default: 1

clamp_numbers

Crop numbers accordingly with 'maximum', 'minimum' attributes.

Values outside these limits will be set to the defined in the maximum/minimum attributes.

Default: 0

strings

Explicitly set type for strings

Default: 1

hash_keys

Filter out not allowed hash keys (where additionalProperties is false).

Default: 1

fill_defaults

Fill missing values that have 'default' attribute in schema.

Default: 0

replace_invalid_values

Replace incompatible with schema values with defaults (if defined). For now, only presence in 'enum' is checked.

Default: 0

METHODS

new

my $jsf = JSON::Schema::Fit->new(booleans => 0);

Create a new JSON::Schema::Fit instance. See bellow for valid options.

get_adjusted

Returns "semi-copy" of data structure with adjusted values. Original data is not affected.

CONSTRUCTOR

SEE ALSO

Related modules: JSON, JSON::Schema.

Json-schema home: http://json-schema.org/

AUTHOR

liosha <liosha@yandex-team.ru>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by liosha.

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