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

Define property with list of references #386

Open
aleksandr-kotlyar opened this issue Mar 3, 2019 · 2 comments
Open

Define property with list of references #386

aleksandr-kotlyar opened this issue Mar 3, 2019 · 2 comments

Comments

@aleksandr-kotlyar
Copy link

Hi! I am interested in, is it possible to define schemas in voluptuous like in openapi 3.0.0
Here is an example of openapi:

data:
  allOf:
    - $ref: '#/components/schemas/Project'
    - $ref: '#/components/schemas/WithProvider'
    - $ref: '#/components/schemas/WithGallery'

I talk about something like:

Schema(
  {
    'data': All({
      Project,
      WithProvider,
      WithGallery
    })
  }
)

may be not like this, maybe like that:

Schema({
    'data': {
      $ref: Project,
      $ref: WithProvider,
      $ref: WithGallery
    }
  }
)

or maybe like this:

Schema({
    'data': {
      Project,
      WithProvider,
      [WithGallery]
    }
  }
)

I think that this feature with list of references inside of property could be very helpful for the whole community to define more schemas!

Thank you!
Best regards, Aleksandr

@svisser
Copy link
Collaborator

svisser commented Mar 6, 2019

@aleksandr-kotlyar are these dictionaries? If so, it'd be possible to use .extend()

data = Schema({})
data = data.extend({ Required('a'): 3 })  # project
data = data.extend({ Required('b'): 4 })  # with provider
data = data.extend({ Required('c'): 5 })  # with gallery

The data schema now expects the keys 'a', 'b' and 'c'.

@aleksandr-kotlyar
Copy link
Author

@svisser thank you for your answer!

are these dictionaries?

No, they are Schemas with more properties inside them.
I have tried to do this thing:

WithGallery = Schema({'a': str})
WithAvailability = Schema({'c': float})
ScheduleProject = Schema({'b': int})

ScheduleEvent = Schema(
    {
        'beginDate': Match(date_pattern),
        'endDate': Match(date_pattern),
        'project': ScheduleProject.extend(WithGallery).extend(WithChannel).extend(WithAvailability)
    },
    extra=PREVENT_EXTRA,
    required=True)

and this:

ScheduleEventProject = ScheduleProject\
    .extend(WithGallery) \
    .extend(WithChannel) \
    .extend(WithAvailability)

ScheduleEvent = Schema(
    {
        'beginDate': Match(date_pattern),
        'endDate': Match(date_pattern),
        'project': ScheduleEventProject
    },
    extra=PREVENT_EXTRA,
    required=True)

but both have failed with the same error

tests/schemas/test_schedule.py:None (tests/schemas/test_schedule.py)
test_schedule.py:4: in <module>
    from resources.widget import ScheduleEvent
..\..\resources\widget.py:216: in <module>
    .extend(WithGallery) \
..\..\.venv\lib\site-packages\voluptuous\schema_builder.py:734: in extend
    assert type(self.schema) == dict and type(schema) == dict, 'Both schemas must be dictionary-based'
E   AssertionError: Both schemas must be dictionary-based

And i expected that this would work.

I understood your suggestion, but i think it will not work with more complex Schemas.
Is there a way to extend Schema by more Schemas, not dictionaries?

Thank you!
Best regards, Aleksandr

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

No branches or pull requests

2 participants