Allow extra keys in a yaml file
Currently, I am unable to set the equivalent to the json additionalProperties = True schema property, getting the error message
unexpected key not in schema '
'
Would it be possible to add an optional flag to allow/disallow this?
My use case is strictly specifying some fields within a larger yaml file that I do not care about. I would be happy to contribute a PR if you are able to help guide me to where to add this!
Possibly, yeah. I'll have a think about it.
Bear in mind that it's possible to use MapPattern(Str(), Any()) and then use revalidation to tighten the screws on the schema for certain key/value combinations.
On Wed, 10 Apr 2019, 11:57 Krishan Bhasin, [email protected] wrote:
Currently, I am unable to set the equivalent to the json additionalProperties = True schema property, getting the error message
unexpected key not in schema ''
Would it be possible to add an optional flag to allow/disallow this?
My use case is strictly specifying some fields within a larger yaml file that I do not care about. I would be happy to contribute a PR if you are able to help guide me to where to add this!
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/54, or mute the thread https://github.com/notifications/unsubscribe-auth/AFyVNdxT3nw9vqFOugYyjyUg7Rmzf28Lks5vfcOQgaJpZM4cmtRc .
That's if you want a solution right now - I'm cognizant that this is not necessarily the ideal solution to your problem.
On Wed, 10 Apr 2019, 12:30 Colm O'Connor, [email protected] wrote:
Possibly, yeah. I'll have a think about it.
Bear in mind that it's possible to use MapPattern(Str(), Any()) and then use revalidation to tighten the screws on the schema for certain key/value combinations.
On Wed, 10 Apr 2019, 11:57 Krishan Bhasin, [email protected] wrote:
Currently, I am unable to set the equivalent to the json additionalProperties = True schema property, getting the error message
unexpected key not in schema ''
Would it be possible to add an optional flag to allow/disallow this?
My use case is strictly specifying some fields within a larger yaml file that I do not care about. I would be happy to contribute a PR if you are able to help guide me to where to add this!
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/54, or mute the thread https://github.com/notifications/unsubscribe-auth/AFyVNdxT3nw9vqFOugYyjyUg7Rmzf28Lks5vfcOQgaJpZM4cmtRc .
I was hoping to try that (specify the my schema then add a MapPattern() that allows any other key/value pairs to exist) but I couldn't get that to work. Or am I misunderstanding your suggestion?
@crdoconnor any chance you could point me in the direction of where I could make a PR to change this?
I think I created a branch for this with a story. I'll double check later.
On Mon, 13 May 2019, 16:10 Krishan Bhasin, [email protected] wrote:
@crdoconnor https://github.com/crdoconnor any chance you could point me in the direction of where I could make a PR to change this?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/54?email_source=notifications&email_token=ABOJKNI4TU2LSGWLUVJO5QLPVGAHXA5CNFSM4HE22ROKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVITZUQ#issuecomment-491863250, or mute the thread https://github.com/notifications/unsubscribe-auth/ABOJKNK4L4NFUNKVNNO7GCDPVGAHXANCNFSM4HE22ROA .
Hi @crdoconnor, any chance you could point me at where I can make the change to make a PR?
I asked chatGPT: "in strictyaml , how to ignore unexpected keys?" It answered: QUOTE
In StrictYAML, you can use the allow_unused_keys option to ignore unexpected keys when parsing a YAML document. This option is set to False by default, which means that an error will be raised if the document contains any unexpected keys.
To allow unexpected keys to be ignored, you can set the allow_unused_keys option to True when parsing the document. Here's an example:
import strictyaml
yaml_text = '''
key1: value1
key2: value2
key3: value3
'''
schema = strictyaml.Map({
'key1': strictyaml.Str(),
'key2': strictyaml.Str(),
}, allow_unused_keys=True)
data = schema.validate(strictyaml.as_document(yaml_text))
print(data)
In this example, we're parsing a YAML document that contains three keys (key1, key2, and key3). However, our schema only defines two of these keys (key1 and key2). By setting allow_unused_keys to True, we're telling StrictYAML to ignore any unexpected keys in the document and only validate the keys that are defined in the schema.
When we run this code, we'll get the following output:
{'key1': 'value1', 'key2': 'value2'}
As you can see, the key3 key is ignored because it's not defined in the schema and allow_unused_keys is set to True.
UNQUOTE
Work-around:
def match_keys_with_schema(multikey_dict, schema) -> Dict:
out_dict = {}
for k in schema._required_keys:
if k in multikey_dict:
out_dict[k] = multikey_dict[k]
else:
out_dict[k] = None
return out_dict