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

Allow extra keys in a yaml file #54

Open
KrishanBhasin opened this issue Apr 10, 2019 · 8 comments
Open

Allow extra keys in a yaml file #54

KrishanBhasin opened this issue Apr 10, 2019 · 8 comments

Comments

@KrishanBhasin
Copy link

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!

@crdoconnor
Copy link
Owner

crdoconnor commented Apr 10, 2019 via email

@crdoconnor
Copy link
Owner

crdoconnor commented Apr 10, 2019 via email

@KrishanBhasin
Copy link
Author

KrishanBhasin commented Apr 11, 2019

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?

@KrishanBhasin
Copy link
Author

@crdoconnor any chance you could point me in the direction of where I could make a PR to change this?

@crdoconnor
Copy link
Owner

crdoconnor commented May 13, 2019 via email

@KrishanBhasin
Copy link
Author

Hi @crdoconnor, any chance you could point me at where I can make the change to make a PR?

@benlawraus
Copy link

benlawraus commented Feb 27, 2023

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

@benlawraus
Copy link

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

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

3 participants