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

plan to develop a feature about 'default value for optional key'? #12

Closed
reedboat opened this issue Mar 22, 2013 · 6 comments
Closed

plan to develop a feature about 'default value for optional key'? #12

reedboat opened this issue Mar 22, 2013 · 6 comments

Comments

@reedboat
Copy link

No description provided.

@keleshev
Copy link
Owner

Hm... interesting. Something like:

Schema({'foo': Or(str, Default('bar'))})

or maybe:

Schema({'foo': Or(str, default='bar')})

What do you think?

@keleshev
Copy link
Owner

Or maybe something like:

>>> from schema import Optional
>>> Schema({'name': str,
...         Optional('occupation', default='dentist'): str}).validate({'name': 'Sam'})
{'name': 'Sam', 'occupation': 'dentist'}

What do you think?

@reedboat
Copy link
Author

I have implemented the second idea based on your program, but I like the first more. :)

@Rogdham
Copy link
Contributor

Rogdham commented May 14, 2013

I see a problem here with your proposals: you are defining the default value in the key part.

The problem is visible when keys are something like int for example.

Trying to find an API… tell me what you prefer! (and they are not to be exclusive)

>>> Schema({Optional(int, default=42): Default(str, 'The answer')}) # option 1
>>> Schema({Optional(int, default=42): Or(str, Default('The answer'))}) # option 2
>>> Schema({Optional(int, default=42): Or(str, default='The answer')}) # option 3

I am for option 3, as it makes the default argument uniform with what is in Optional.

Also, in case Optional has no default argument, I propose to create the default as followed:

  • if the first argument is of type type, use its default value (e.g. Optional(int) would be the same as Optional(int, 0) as int() is 0)
  • else try to find a .default attribute in the first argument (e.g. for Optional(Use(…)))
  • and if it fails, use the first argument directly (e.g. Optional(42) would be the same as Optional(42, default=42)

Tell me if it makes sense, or if you have any other idea.

@Rogdham
Copy link
Contributor

Rogdham commented May 18, 2013

I tried to implement the third API, and I added a Default class as well, because it made sense.

I let you go and see #19 if you want to give it a try and comment there ;-)

Rogdham added a commit to Rogdham/schema that referenced this issue May 18, 2013
Rogdham added a commit to Rogdham/schema that referenced this issue May 18, 2013
Rogdham added a commit to Rogdham/schema that referenced this issue Apr 30, 2014
Rogdham added a commit to Rogdham/schema that referenced this issue Apr 30, 2014
Rogdham added a commit to Rogdham/schema that referenced this issue Apr 30, 2014
Rogdham added a commit to Rogdham/schema that referenced this issue Apr 30, 2014
Rogdham added a commit to Rogdham/schema that referenced this issue Apr 30, 2014
@sjakobi
Copy link
Contributor

sjakobi commented Sep 25, 2015

As the initial request has been implemented and the discussion has shifted to #19, I believe this can be closed.

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

5 participants