Data-Processor

 view release on metacpan or  search on metacpan

lib/Data/Processor.pm  view on Meta::CPAN


            #merge members subtree recursively
            if (exists $otherSubSchema->{$elem}->{members}) {
                exists $subSchema->{$elem}->{members} || ($subSchema->{$elem}->{members} = {});
                $mergeSubSchema->($subSchema->{$elem}->{members}, $otherSubSchema->{$elem}->{members});
            }

            #check elements
            for my $key (qw(description example default error_msg regex array value)){
                $checkKey->($elem, $key);
            }

            #special handler for transformer
            if ($otherSubSchema->{$elem}->{transformer}) {
                croak "merging element '$elem': merging conflicting transformers not allowed"
                    if $subSchema->{$elem}->{transformer}
                        && $subSchema->{$elem}->{transformer} != $otherSubSchema->{$elem}->{transformer};

                $subSchema->{$elem}->{transformer} = $otherSubSchema->{$elem}->{transformer};
            }

            #special handler for optional: set it mandatory if at least one is not optional
            delete $subSchema->{$elem}->{optional}
                if !($subSchema->{$elem}->{optional} && $otherSubSchema->{$elem}->{optional});

            #special handler for validator: combine validator subs
            if ($otherSubSchema->{$elem}->{validator}) {
                if ((my $validator = $subSchema->{$elem}->{validator})
                    && $subSchema->{$elem}->{validator} != $otherSubSchema->{$elem}->{validator}) {

                    $subSchema->{$elem}->{validator} = sub {
                        return $validator->(@_) // $otherSubSchema->{$elem}->{validator}->(@_);
                    };
                }
                else{
                    $subSchema->{$elem}->{validator}
                        = $otherSubSchema->{$elem}->{validator};
                }
            }
        }
    };

    $mergeSubSchema->($mergeNode, $schema);

    return $self->validate_schema;
}

=head2 schema

Returns the schema. Useful after schema merging.

=cut

sub schema{
    return shift->{schema};
}

=head2 transform_data

Transform one key in the data according to rules specified
as callbacks that themodule calls for you.
Transforms the data in-place.

 my $validator = Data::Processor::Validator->new($schema, data => $data)
 my $error_string = $processor->transform($key, $schema_key, $value);

This is not tremendously useful at the moment, especially because validate()
transforms during validation.

=cut
# XXX make this traverse a data tree and transform everything
# XXX across.
# XXX Before hacking something here, think about factoring traversal out of
# XXX D::P::Validator
sub transform_data{
    my $self = shift;
    my $key  = shift;
    my $schema_key = shift;
    my $val  = shift;

    return Data::Processor::Transformer->new()->transform($key, $schema_key, $val);
}

=head2 make_data

Writes a data template using the information found in the schema.

 my $data = $processor->make_data(data=>$data);

=cut
sub make_data{
    my $self = shift;
    my $entry_point = shift // $self->{schema};
    return Data::Processor::Generator::make_data_template($entry_point);
}

=head2 make_pod

Write descriptive pod from the schema.

 my $pod_string = $processor->make_pod();

=cut
sub pod_write{
    my $self = shift;
    return Data::Processor::PodWriter::pod_write(
        $self->{schema},
        "=head1 Schema Description\n\n"
    );
}

=head1 SCHEMA REFERENCE


=head2 Top-level keys and members

The schema is described by a nested hash. At the top level, and within a
members definition, the keys are the same as the structure you are
describing. So for example:

 my $schema = {



( run in 0.536 second using v1.01-cache-2.11-cpan-39bf76dae61 )