=head1 NAME

DBIx::Result::Convert::JSONSchema - Convert DBIx result schema to JSON schema

=begin html

    <a href='https://travis-ci.org/Humanstate/p5-dbix-result-convert-jsonschema?branch=master'><img src='https://travis-ci.org/Humanstate/p5-dbix-result-convert-jsonschema.svg?branch=master' alt='Build Status' /></a>
    <a href='https://coveralls.io/github/Humanstate/p5-dbix-result-convert-jsonschema?branch=master'><img src='https://coveralls.io/repos/github/Humanstate/p5-dbix-result-convert-jsonschema/badge.svg?branch=master' alt='Coverage Status' /></a>

=end html

=head1 VERSION

    0.06

=head1 SYNOPSIS

    use DBIx::Result::Convert::JSONSchema;

    my $SchemaConvert = DBIx::Result::Convert::JSONSchema->new( schema => Schema );
    my $json_schema = $SchemaConvert->get_json_schema( DBIx::Class::ResultSource );

=head1 DESCRIPTION

This module attempts basic conversion of L<DBIx::Class::ResultSource> to equivalent
of L<http://json-schema.org/>.
By default the conversion assumes that the L<DBIx::Class::ResultSource> originated
from MySQL database. Thus all the types and defaults are set based on MySQL
field definitions.
It is, however, possible to overwrite field type map and length map to support
L<DBIx::Class::ResultSource> from other database solutions.

Note, relations between tables are not taken in account!

=head2 get_json_schema

Returns somewhat equivalent JSON schema based on DBIx result source name.

    my $json_schema = $converted->get_json_schema( 'TableSource', {
        schema_declaration              => 'http://json-schema.org/draft-04/schema#',
        decimals_to_pattern             => 1,
        has_schema_property_description => 1,
        allow_additional_properties     => 0,
        ignore_property_defaults        => 1,
        overwrite_schema_property_keys  => {
            name    => 'cat',
            address => 'dog',
        },
        add_schema_properties           => {
            address => { ... },
            bank_account => '#/definitions/bank_account',
        },
        overwrite_schema_properties     => {
            name => {
                _action  => 'merge', # one of - merge/overwrite
                minimum  => 10,
                maximum  => 20,
                type     => 'number',
            },
        },
        include_required   => [ qw/ street city / ],
        exclude_required   => [ qw/ name address / ],
        exclude_properties => [ qw/ mouse house / ],

        dependencies => {
            first_name => [ qw/ middle_name last_name / ],
        },
    });

Optional arguments to change how JSON schema is generated:

=over 8

=item * schema_declaration

Declare which version of the JSON Schema standard that the schema was written against.

L<https://json-schema.org/understanding-json-schema/reference/schema.html>

B<Default>: "http://json-schema.org/schema#"

=item * decimals_to_pattern

1/0 - value to indicate if 'number' type field should be converted to 'string' type with
RegExp pattern based on decimal place definition in database.

B<Default>: 0

=item * has_schema_property_description

Generate schema description for fields e.g. 'Optional numeric type value for field context e.g. 1'.

B<Default>: 0

=item * ignore_property_defaults

Do not set schema B<default> property field based on default in DBIx schema

B<Default>: 0

=item * allow_additional_properties

Define if the schema accepts additional keys in given payload.

B<Default>: 0

=item * add_property_minimum_value

If field does not have format type add minimum values for number and string types based on DB field type.
This might not make sense in most cases as the minimum is either 0 or the lower bound if number is signed.

B<Default>: 0

=item * overwrite_schema_property_keys

HashRef representing mapping between old property name and new property name to overwrite existing schema keys,
Properties from old key will be assigned to the new property.

B<Note> The key conversion is executed last, every other option e.g. C<exclude_properties> will work only on original
database column names.

=item * overwrite_schema_properties

HashRef of property name and new attributes which can be either overwritten or merged based on given B<_action> key.

=item * exclude_required

ArrayRef of database column names which should always be EXCLUDED from REQUIRED schema properties.

=item * include_required

ArrayRef of database column names which should always be INCLUDED in REQUIRED schema properties

=item * exclude_properties

ArrayRef of database column names which should be excluded from JSON schema AT ALL

=item * dependencies

L<https://json-schema.org/understanding-json-schema/reference/object.html#property-dependencies>

=item * add_schema_properties

HashRef of custom schema properties that must be included in final definition
Note that custom properties will overwrite defaults

=item * schema_overwrite

HashRef of top level schema properties e.g. 'required', 'properties' etc. to overwrite

=back

=head1 SEE ALSO

L<DBIx::Class::ResultSource> - Result source object

=head1 AUTHOR

malishew - C<malishew@cpan.org>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. If you would like to contribute documentation
or file a bug report then please raise an issue / pull request:

    https://github.com/Humanstate/p5-dbix-result-convert-jsonschema

