keithasaurus / koda-validate

Typesafe, Composable Validation
MIT License
112 stars 0 forks source link

validate_signature ignores validators' coercion and processors #30

Closed koterpillar closed 1 year ago

koterpillar commented 1 year ago

validate_signature, when used with overridden validators, does not substitute the return values of arguments' validators when passing arguments to the function underneath.

This matters when validators include a coercer or processors:

Example code

from typing import Any
from koda.maybe import Maybe, Just, nothing
from koda_validate import StringValidator, upper_case, coercer
from koda_validate.signature import validate_signature

@validate_signature(overrides={"arg": StringValidator(preprocessors=[upper_case])})
def func1(arg: str) -> str:
    return arg

print(func1("hello"))

@coercer(int, str)
def allow_int_for_str(val: Any) -> Maybe[str]:
    if type(val) is int:
        return Just(str(val))
    if type(val) is str:
        return Just(val)
    return nothing

@validate_signature(overrides={"arg": StringValidator(coerce=allow_int_for_str)})
def func2(arg: str) -> str:
    return arg.capitalize()

print(func2(3))

Expected result

HELLO
3

Actual result

hello
...
AttributeError: 'int' object has no attribute 'capitalize'
keithasaurus commented 1 year ago

Thanks for the bug report! I'll work on a fix.