Comments (8)
from strictyaml.
from strictyaml.
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.
@crdoconnor any chance you could point me in the direction of where I could make a PR to change this?
from strictyaml.
from strictyaml.
Hi @crdoconnor, any chance you could point me at where I can make the change to make a PR?
from strictyaml.
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.
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)
- Problem loading document with a single string
- Repeated revalidate() raises Invalid state HOT 2
- Failed revalidation leads to inconsistant state
- Fasjo HOT 3
- Website fails to acknowledge that the Norway problem was fixed in YAML 1.2 HOT 2
- strictyaml does not act as "a near-drop in replacement for pyyaml" HOT 4
- The type order of optional arguments affects the results HOT 2
- Date vs Datetime HOT 2
- Support python2.7.18 HOT 2
- 1.7.0 is broken (cannot import name 'ruamel' from partially initialized module 'strictyaml') HOT 5
- Map containing only Optionals does not validate an empty dict
- as_document and load have dissimilar results for CommaSeparated(Int())
- Doesn't Work At All For Lists / Arrays? HOT 4
- New lines after comment line causes `NotImplementedError`
- Feature request/Question: "Optionalize" maps HOT 1
- Thoughts regarding case-sensitivity of keys HOT 6
- Why not: non-Turing complete configuration languages
- Cannot iterate on emtpy list
- BUG: MapCombined documentation different traceback
- Feature: strictyaml.scalar.Time
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strictyaml.