Comments (16)
Implemented in version 0.15.0. Let me know if you see any issues.
from strictyaml.
Yeah, this sounds like a good idea. Something like this you mean?
Map({Optional("optional"): Str(), "mandatory": Str()}, defaults={"optional": "mydefault"})
I have the same kind of problem with some of my apps, although I usually fill in the default by going yaml.data.get("optional", "thedefaultiwanted"). When looking at https://github.com/gjcarneiro/yacron/blob/master/yacron/config.py by @gjcarneiro I figured it might be better for coherence if he could define defaults alongside the schema instead of in a different structure.
from strictyaml.
Sorry that took so long. It's fixed now in 0.15.1 and your tests now seem to be passing.
from strictyaml.
I don't know if and in case how this is done elsewhere. Would be good to be consistent. In my opinion putting default values alongside the schema seems suitable as well. It could be reasonable to put the definition of the default value closer to the definition of the key e.g. something like Map({Optional(key="optional", default="mydefault"): Str(), "mandatory": Str()})
.
from strictyaml.
Yeah, maybe that would be better actually. It would be closer to the validator and it would naturally prevent putting in default keys for keys that don't actually exist.
A few other things to bear in mind:
- The default, whatever it is, should be validated against the schema and raise an exception if it's not of the correct type. I think catching YAMLSerializationError and raising a different exception (YAMLSchemaException perhaps) should work.
- I'm not sure if turning data in to YAML should respect the defaults. i.e. should we get:
as_document({"nonoptional": "x", "optional": "mydefault"}, yourschema).as_yaml() == "nonptional: x"
Or:
as_document({"nonoptional": "x", "optional": "mydefault"}, yourschema).as_yaml() == "nonptional: x\nmydefault: mydefault"
from strictyaml.
- I'm not sure if turning data in to YAML should respect the defaults. i.e. should we get:
Me neither. Speaking with my use case in mind I'd consider default values in generated YAML "noise" because I am biased and know my schema and the default values will hardly change. Having them in the YAML wouldn't require a user to know the schema and the documentation (which could be inconsistent with the schema).
from strictyaml.
Ok so option 1? I was leaning towards that myself too.
Btw, you seemed keen to do a PR yesterday. Would you prefer it if:
- I write this.
- I write the spec/tests on a new branch, you do a PR with the code changes.
- You write this.
I'm ok with any of these.
from strictyaml.
It's not blocking me and it's minor for me right now. We can keep this open until first of us both is blocked.
from strictyaml.
Docs : https://hitchdev.com/strictyaml/using/alpha/compound/optional-keys-with-defaults/
from strictyaml.
An implication w.r.t. schema design:
Default values are only supported with load(
and not with revalidate(
, right? From a logical point of view this makes total sense but implies limitations w.r.t. separation of validation logic for big schemas. I usually validate big schemas in sub-sequence logic: validate overall schema as part of load(
), revalidate parts of the overall schema with separate revalidate(
steps. In case one want's to use default values for optional keys the corresponding part of the schema needs to be validated as part of load load(
. E.g. for deeply nested optional keys with default values in the lowest layer one is forced to validate the overall schema with a single step using load(
.
from strictyaml.
It should work. I just tested:
loose_schema = MapPattern(Str(), Str())
strict_schema = Map({"a": Int(), Optional("b", default=False): Bool(), })
myyaml = load(yaml_snippet, loose_schema)
myyaml.revalidate(strict_schema)
assert myyaml.data == OrderedDict([('b', False), ('a', 1)])
Or did you mean another kind of revalidate scenario?
FWIW I've been exploratory testing it but I'm still not 100% convinced this feature is totally bug free, so if you do see any odd behavior please do let me know.
from strictyaml.
Or did you mean another kind of revalidate scenario?
I have two branches config-defaults-revalidate
and config-defaults
. In the first one I added default values with my current re-validation logic (tests fail). In the second branch I added default values and refactored back to single validation logic with load(
(tests fail).
I don't know yet why tests fail in both cases. Tests can easily be run locally (pipenv run tox -e pytest
is sufficient) like described in contribution docs.
FWIW I've been exploratory testing it but I'm still not 100% convinced this feature is totally bug free, so if you do see any odd behavior please do let me know.
No worries. Is 100% confidence into tests realistic? 😄
from strictyaml.
Cool! I'll check it out and figure out why it's failing.
from strictyaml.
Thx a lot.
from strictyaml.
Yep, this is a bug. Thanks for letting me know!
I will try and roll out a fix later today.
from strictyaml.
Thanks both for implementing this most useful feature.
from strictyaml.
Related Issues (20)
- Problem loading document with a single string
- Repeated revalidate() raises Invalid state HOT 2
- Failed revalidation leads to inconsistant state
- Fasjo HOT 3
- Website fails to acknowledge that the Norway problem was fixed in YAML 1.2 HOT 2
- strictyaml does not act as "a near-drop in replacement for pyyaml" HOT 4
- The type order of optional arguments affects the results HOT 2
- Date vs Datetime HOT 2
- Support python2.7.18 HOT 2
- 1.7.0 is broken (cannot import name 'ruamel' from partially initialized module 'strictyaml') HOT 5
- Map containing only Optionals does not validate an empty dict HOT 1
- as_document and load have dissimilar results for CommaSeparated(Int())
- Doesn't Work At All For Lists / Arrays? HOT 4
- New lines after comment line causes `NotImplementedError`
- Feature request/Question: "Optionalize" maps HOT 1
- Thoughts regarding case-sensitivity of keys HOT 6
- Why not: non-Turing complete configuration languages
- Cannot iterate on emtpy list
- BUG: MapCombined documentation different traceback
- Feature: strictyaml.scalar.Time
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strictyaml.