Reputation: 5913
I'd like to validate the following YAML file defines a defaultdict
that contains two dict
s named dev
and sha
.
!!python/object/apply:collections.defaultdict
args:
- !!python/name:builtins.dict ''
dictitems:
dev:
sha: 5b7
url: /path/to/here
shared:
sha: 58a
url: /path/to/there
using yaml.load()
(safe_load()
leads to a whole different question of creating constructors, so let's set that aside.) gives me this data structure:
defaultdict(<class 'dict'>, {'dev': {'sha': '5b7', 'url': '/path/to/here'},
'shared': {'sha': '58a', 'url': '/path/to/there'}})
I'd like to validate this data structure so I create this:
snapshot_schema = val.Schema({"dictitems":dict,"dev":dict,"shared":dict})
This successfully validates that I have one defaultdict
containing two dicts
. I'd like to validate that the sha
and url
tags in both of those dicts are really str
(and more validation later maybe.)
I could create an additional schema
new_schema = Schema({'sha':str, 'url':str})
new_schema(my_data['dev'])
new_schema(my_data['shared'])
But is there a more elegant way of doing this?
Upvotes: 0
Views: 1286
Reputation: 5913
Turns out one answer is to put the new_schema
into the snapshot_schema
:
repo_schema = Schema({"sha":str,"url":str})
snapshot_schema = Schema({"dictitems":dict,"dev":repo_schema,
"shared":repo_schema})
Also I guess you could do:
snapshot_schema=Schema({"dictitems":dict, "dev":{"sha":str,"url":str},
"shared":{"sha":str,"url":str})
Upvotes: 1