Skip to content

Commit

Permalink
Merge pull request #10 from Humanstate/valters/new_schema_options
Browse files Browse the repository at this point in the history
Ignore schema defaults via arg and do not set min. values by default
  • Loading branch information
Lee J committed Sep 2, 2019
2 parents f602d19 + 4e14586 commit 934ca3a
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 19 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Revision history for Perl extension DBIx::Result::Convert::JSONSchema.

0.05 2019-09-02
- Add option to ignore default value presets
- By default hide minimum values - having 0's on schema as minimums do not make a lot of sense
- Add option to show minimum values from defaults

0.04 2019-08-26
- Fix bug with one constructor arguments
- Add `include_required` argument
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NAME
DBIx::Result::Convert::JSONSchema - Convert DBIx result schema to JSON schema

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

<div>
<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>
Expand All @@ -8,7 +9,7 @@

# VERSION

0.04
0.05

# SYNOPSIS

Expand Down Expand Up @@ -38,6 +39,7 @@ Returns somewhat equivalent JSON schema based on DBIx result source name.
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',
Expand Down Expand Up @@ -86,12 +88,25 @@ Optional arguments to change how JSON schema is generated:

**Default**: 0

- ignore\_property\_defaults

Do not set schema **default** property field based on default in DBIx schema

**Default**: 0

- allow\_additional\_properties

Define if the schema accepts additional keys in given payload.

**Default**: 0

- 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.

**Default**: 0

- overwrite\_schema\_property\_keys

HashRef representing mapping between old property name and new property name to overwrite existing schema keys,
Expand Down
19 changes: 17 additions & 2 deletions README.pod
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
=head1 NAME
DBIx::Result::Convert::JSONSchema - Convert DBIx result schema to JSON schema

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

=begin html

Expand All @@ -10,7 +11,7 @@

=head1 VERSION

0.04
0.05

=head1 SYNOPSIS

Expand Down Expand Up @@ -40,6 +41,7 @@ Returns somewhat equivalent JSON schema based on DBIx result source name.
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',
Expand Down Expand Up @@ -90,12 +92,25 @@ Generate schema description for fields e.g. 'Optional numeric type value for fie

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,
Expand Down
33 changes: 25 additions & 8 deletions lib/DBIx/Result/Convert/JSONSchema.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package DBIx::Result::Convert::JSONSchema;

our $VERSION = '0.04';
our $VERSION = '0.05';


=head1 NAME
Expand All @@ -16,7 +16,7 @@ DBIx::Result::Convert::JSONSchema - Convert DBIx result schema to JSON schema
=head1 VERSION
0.04
0.05
=head1 SYNOPSIS
Expand Down Expand Up @@ -132,6 +132,7 @@ Returns somewhat equivalent JSON schema based on DBIx result source name.
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',
Expand Down Expand Up @@ -182,12 +183,25 @@ Generate schema description for fields e.g. 'Optional numeric type value for fie
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,
Expand Down Expand Up @@ -239,9 +253,11 @@ sub get_json_schema {
# additional schema generation options
my $decimals_to_pattern = $args->{decimals_to_pattern};
my $has_schema_property_description = $args->{has_schema_property_description};
my $ignore_property_defaults = $args->{ignore_property_defaults};
my $overwrite_schema_property_keys = $args->{overwrite_schema_property_keys} // {};
my $add_schema_properties = $args->{add_schema_properties};
my $overwrite_schema_properties = $args->{overwrite_schema_properties} // {};
my $add_property_minimum_value = $args->{add_property_minimum_value};
my %exclude_required = map { $_ => 1 } @{ $args->{exclude_required} || [] };
my %include_required = map { $_ => 1 } @{ $args->{include_required} || [] };
my %exclude_properties = map { $_ => 1 } @{ $args->{exclude_properties} || [] };
Expand Down Expand Up @@ -286,7 +302,7 @@ sub get_json_schema {

# DBIx schema size constraint -> JSON schema size constraint
if ( ! $format_type && $self->length_map->{ $column_info->{data_type} } ) {
$self->_set_json_schema_property_range( \%json_schema, $column_info, $column );
$self->_set_json_schema_property_range( \%json_schema, $column_info, $column, $add_property_minimum_value );
}

# DBIx schema required -> JSON schema required
Expand All @@ -297,7 +313,7 @@ sub get_json_schema {
}

# DBIx schema defaults -> JSON schema defaults (no refs e.g. current_timestamp)
if ( $column_info->{default_value} && ! ref $column_info->{default_value} ) {
if ( ! $ignore_property_defaults && $column_info->{default_value} && ! ref $column_info->{default_value} ) {
$json_schema{properties}->{ $column }->{default} = $column_info->{default_value};
}

Expand Down Expand Up @@ -432,18 +448,19 @@ sub _get_json_schema_property_description {

# Convert from DBIx field length to JSON schema field length based on field type
sub _set_json_schema_property_range {
my ( $self, $json_schema, $column_info, $column ) = @_;
my ( $self, $json_schema, $column_info, $column, $add_property_minimum_value ) = @_;

my $json_schema_min_type = $self->length_type_map->{ $self->type_map->{ $column_info->{data_type} } }->[0];
my $json_schema_max_type = $self->length_type_map->{ $self->type_map->{ $column_info->{data_type} } }->[1];

my $json_schema_min = $self->_get_json_schema_property_min_max_value( $column_info, 0 );
my $json_schema_max = $self->_get_json_schema_property_min_max_value( $column_info, 1 );

# bump min value to 0 (don't see how this starts from negative)
$json_schema_min = 0 if $column_info->{is_auto_increment};
# bump min value to 1 (don't see how this starts from negative)
$json_schema_min = 1 if $column_info->{is_auto_increment};

$json_schema->{properties}->{ $column }->{ $json_schema_min_type } = $json_schema_min;
$json_schema->{properties}->{ $column }->{ $json_schema_min_type } = $json_schema_min
if $add_property_minimum_value;
$json_schema->{properties}->{ $column }->{ $json_schema_max_type } = $json_schema_max;

if ( $column_info->{size} ) {
Expand Down
13 changes: 12 additions & 1 deletion t/03-convert-default.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ throws_ok {
$converter->get_json_schema('Dog');
} qr/Can't find source for Dog/;

my $json_schema = $converter->get_json_schema('MySQLTypeTest');
my $json_schema = $converter->get_json_schema(
'MySQLTypeTest',
{
add_property_minimum_value => 1,
}
);
is ref $json_schema, 'HASH', 'got json schema HashRef';

subtest 'JSON schema keys' => sub {
Expand Down Expand Up @@ -83,6 +88,12 @@ subtest 'string types have default minLength and maxLength' => sub {
my $json_schema_type = $json_schema->{properties}->{ $json_schema_key }->{type};

if ( $json_schema_type && $json_schema_type eq 'string' ) {

if ( my $format = $json_schema->{properties}->{ $json_schema_key }->{format} ) {
note("Skipping '$json_schema_key' key because format '$format' is defined");
next;
}

ok defined $json_schema->{properties}->{ $json_schema_key }->{minLength},
"JSON schema property $json_schema_key has minLength for string type";
ok defined $json_schema->{properties}->{ $json_schema_key }->{maxLength},
Expand Down
7 changes: 6 additions & 1 deletion t/04-convert-custom.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ $converter->pattern_map({
%{ $pattern_map },
});

my $json_schema = $converter->get_json_schema('MySQLTypeTest');
my $json_schema = $converter->get_json_schema(
'MySQLTypeTest',
{
add_property_minimum_value => 1,
}
);

foreach my $schema_key ( keys %{ $json_schema->{properties} } ) {
my $property_type = $json_schema->{properties}->{ $schema_key}->{type};
Expand Down
17 changes: 13 additions & 4 deletions t/05-convert-options.t
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ subtest 'schema_declaration' => sub {

subtest 'schema_overwrite' => sub {

my $json_schema = $converter->get_json_schema('MySQLTypeTest', {
my $schema = $converter->get_json_schema('MySQLTypeTest', {
schema_overwrite => {
'$schema' => 'overwritten',
type => 'overwritten',
Expand All @@ -107,7 +107,7 @@ subtest 'schema_overwrite' => sub {
},
});

cmp_deeply $json_schema, {
cmp_deeply $schema, {
'$schema' => 'overwritten',
'additionalProperties' => 0,
'type' => 'overwritten',
Expand All @@ -122,13 +122,22 @@ subtest 'schema_overwrite' => sub {

subtest 'dependencies' => sub {

my $json_schema = $converter->get_json_schema('MySQLTypeTest', {
my $schema = $converter->get_json_schema('MySQLTypeTest', {
dependencies => {
fieldA => [ qw/ fieldB fieldC / ],
},
});

cmp_deeply $json_schema->{dependencies}, { fieldA => [ qw/ fieldB fieldC / ] }, 'got dependency fields';
cmp_deeply $schema->{dependencies}, { fieldA => [ qw/ fieldB fieldC / ] }, 'got dependency fields';

};

subtest 'ignore_property_defaults' => sub {

is $json_schema->{properties}->{date}->{default}, '2019-08-21', 'original schema has default key for date';

my $schema = $converter->get_json_schema( 'MySQLTypeTest', { ignore_property_defaults => 1 } );
is $schema->{properties}->{date}->{default}, undef, 'schema does not have defaults set';

};

Expand Down
2 changes: 1 addition & 1 deletion t/lib/Test/Schema/Result/MySQLTypeTest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ __PACKAGE__->add_columns(
"json",
{ data_type => "json", is_nullable => 1 },
"date",
{ data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 1 },
{ data_type => "date", datetime_undef_if_invalid => 1, default_value => "2019-08-21" },
"datetime",
{
data_type => "datetime",
Expand Down

0 comments on commit 934ca3a

Please sign in to comment.