Skip to content
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

Full subclass support for nested schemas. #268

Merged
merged 4 commits into from
Oct 20, 2021

Conversation

Jerry-Ma
Copy link
Contributor

@Jerry-Ma Jerry-Ma commented Sep 23, 2021

Hi,

I have encounter some issues in integrating this package with the python's new dataclasses.dataclass. Most of them is due to the lack of customizability of the core classes including Schema, Optional, etc.

This pull request contains the following changes:

  • Added **kwargs to the function signature of Schema.validate(). This allows subclass to override the behavior of validate in a propagate-able fashion. Nested schema.Schema class will ignore any kwargs, but nested MySchema class will behave accordingly. This fix A post validation hook at the schema level would be handy. #262 (@tolomea). Moreover, the kwargs passed to validate() is picked up by the Optional(default=) value when it is a callable:
assert Schema({Optional('a', default=lambda **k: k['some_value'])}).validate({}, some_value=1) == {'a': 1}
class MyOptional(Optional):
    @property
    def default(self):
        def wrapped(**k):
            return k['some_value']
        return wrapped
    @default.setter
    def default(self, _):
        pass # this is needed to satisfy the Optional.__init__ which does `self.default=default`
  • Added **kwargs to the signature of Schema.json_schema(), and changed the handling of default values such that callable will be evaluated with the kwargs. This makes it more inline with the behavior of the Schema.validate:
    def default_func(**kwargs):
        return 'Hello, ' + kwargs['name']
    s = Schema({Optional("test", default=default_func): str})
    assert s.json_schema("my-id", name='World!') == {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "$id": "my-id",
        "properties": {"test": {"default": "Hello, World!", "type": "string"}},
        "required": [],
        "additionalProperties": False,
        "type": "object",
    }

For more details of the changes and their implications, please take a look at the test cases I added.

Please let me know what you think!

@skorokithakis
Copy link
Collaborator

This seems great, thank you! I especially appreciate the detailed PR description. Could you add some documentation to the README about these features so we can merge?

@Jerry-Ma
Copy link
Contributor Author

@skorokithakis

Sorry for the long wait. I've just updated the README with a short example demonstrate this feature.

@skorokithakis
Copy link
Collaborator

skorokithakis commented Oct 20, 2021

Looks good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Derived class from Optional is not allowed A post validation hook at the schema level would be handy.
2 participants