Git Product home page Git Product logo

Comments (8)

crdoconnor avatar crdoconnor commented on August 10, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

from strictyaml.

KrishanBhasin avatar KrishanBhasin commented on August 10, 2024

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?

from strictyaml.

KrishanBhasin avatar KrishanBhasin commented on August 10, 2024

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

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

from strictyaml.

KrishanBhasin avatar KrishanBhasin commented on August 10, 2024

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

from strictyaml.

benlawraus avatar benlawraus commented on August 10, 2024

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

from strictyaml.

benlawraus avatar benlawraus commented on August 10, 2024

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

from strictyaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.