strictyaml icon indicating copy to clipboard operation
strictyaml copied to clipboard

Allow extra keys in a yaml file

Open KrishanBhasin opened this issue 6 years ago • 8 comments

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!

KrishanBhasin avatar Apr 10 '19 10:04 KrishanBhasin

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 .

crdoconnor avatar Apr 10 '19 11:04 crdoconnor

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 .

crdoconnor avatar Apr 10 '19 11:04 crdoconnor

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 avatar Apr 11 '19 09:04 KrishanBhasin

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

KrishanBhasin avatar May 13 '19 15:05 KrishanBhasin

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 .

crdoconnor avatar May 13 '19 16:05 crdoconnor

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

KrishanBhasin avatar Aug 12 '19 10:08 KrishanBhasin

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 avatar Feb 27 '23 14:02 benlawraus

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

benlawraus avatar Feb 28 '23 11:02 benlawraus