stripe-perl icon indicating copy to clipboard operation
stripe-perl copied to clipboard

move advanced validation to BUILD routines?

Open sherrardb opened this issue 5 years ago • 1 comments

not sure why i did not see this at the time, but

https://github.com/lukec/stripe-perl/blob/3151556f52b21465c547862f740fa6f21ff25743/lib/Net/Stripe.pm#L291-L325

probably belongs in a BUILD routine for Net::Stripe::Charge, rather than in the method.

whether that kind of change is prudent stylistically will hopefully be clearer as more cases of advanced validation are added. it will also depend on whether there is a difference in presentation of the errors when triggered during BUILD rather than during the method.

sherrardb avatar Jan 23 '20 13:01 sherrardb

this turns out to be a bit of a mixed bag. for validation that applies across all instances of an object, BUILD is appropriate. but for conditions that are specific to a given method, the validation belongs there:

method update_payment_method(
    StripePaymentMethodId :$payment_method_id!,
    HashRef :$billing_details?,
    HashRef :$card?,
    HashRef :$metadata?,
) {
    my %args = (
        card => $card,
        billing_details => $billing_details,
        metadata => $metadata,
    );
    my $payment_method_obj = Net::Stripe::PaymentMethod->new( %args );

    my @one_of = qw/ billing_details card metadata /;
    my @defined = grep { defined( $payment_method_obj->$_ ) } @one_of;

    die Net::Stripe::Error->new(
        type => "update_payment_method error",
        message => sprintf( "at least one of: %s is required to update a payment_method",
            join( ', ', @one_of ),
        ),
    ) if ! @defined;

    return $self->_post("payment_methods/$payment_method_id", $payment_method_obj);
}

sherrardb avatar Jan 26 '20 16:01 sherrardb