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

Create raw list Schema #2212

Closed
l00p1n6 opened this issue Nov 29, 2023 · 3 comments
Closed

Create raw list Schema #2212

l00p1n6 opened this issue Nov 29, 2023 · 3 comments
Labels

Comments

@l00p1n6
Copy link

l00p1n6 commented Nov 29, 2023

How can one create Schema derived class from a list only data?

For input with a field value, i.e. dictionary, e.g.:

{
  "string_list": [
    "one",
    "two",
    "three"
  ]
}

one can use;

class StringListSchema(Schema):
    string_list = fields.List(fields.Str())

But how can one so it when input is just a list:

[
  "one",
  "two",
  "three"
]

?

NB: both are valid JSONs

@lafrech
Copy link
Member

lafrech commented Jan 18, 2024

I don't think you can do that with marshmallow. I'm pretty sure there was once a discussion about it, maybe involving sort of merging Schema and Field. I can't find it.

You could probably use a pre_load to wrap the payload into a dict with a single key and using that key as name for the List field in the schema, but this sucks, of course.

@sloria
Copy link
Member

sloria commented Jan 2, 2025

for deserialization, you could call deserialize on the List field.

from marshmallow import fields


field = fields.List(fields.String())

field.deserialize(["one", "two", "three"])  # ['one', 'two', 'three']

@sloria sloria closed this as completed Jan 2, 2025
@sloria
Copy link
Member

sloria commented Jan 3, 2025

note: this is the proposed in #1694

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

No branches or pull requests

3 participants