Skip to content

Uncustomizable type casting #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
muzhig opened this issue Jul 6, 2018 · 3 comments
Closed

Uncustomizable type casting #53

muzhig opened this issue Jul 6, 2018 · 3 comments
Labels
area/schema Indicates an issue on schema area kind/enhancement

Comments

@muzhig
Copy link

muzhig commented Jul 6, 2018

Currently the Schema class does automatic type casting, for string it doesn't do even that.
Which actually could be not the desired behavior: some might need to force strict type match for example, just like me. I solved the problem currently by monkey-patching DEFAULT_CAST_CALLABLE_GETTER.
Does it sound reasonable to make this customizable?

@p1c2u
Copy link
Collaborator

p1c2u commented Jul 9, 2018

@muzhig Hi. Can you provide simple example?

@muzhig
Copy link
Author

muzhig commented Jul 9, 2018

    @classmethod
    def set_strict_type_casting(cls):
        def force_type(*types):
            def type_caster(value):
                if not isinstance(value, types):
                    raise ValueError(value)
                return value

            return type_caster

        cls.DEFAULT_CAST_CALLABLE_GETTER.update({
            SchemaType.BOOLEAN: force_type(bool),
            SchemaType.INTEGER: force_type(int),
            SchemaType.NUMBER: force_type(int, float),
            SchemaType.STRING: force_type(str),
        })

this way it will allow to force types to exactly match the schema.
Maybe other users of the library need other scenarios, we can try to discuss this.

@p1c2u
Copy link
Collaborator

p1c2u commented Jul 10, 2018

@muzhig thanks for the example now I see your point. You want to skip value casting for schema. I think optional argument should do the trick.

@p1c2u p1c2u added the area/schema Indicates an issue on schema area label Aug 17, 2018
@p1c2u p1c2u closed this as completed in #81 Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/schema Indicates an issue on schema area kind/enhancement
Projects
None yet
Development

No branches or pull requests

2 participants