0

I am trying to pass a YAML file into a python script that compares the YAML file to a list of key:value pairs grabbed from an API.

YAML file

required_tags:
  environment:
    - "dev"
    - "qa"
  owner:
    - "*"

key:values from API

{
  "environment": "prod",
  "owner": "billy"
}

So I am trying to say, if the key:values from the API don't match any of the required_tags from the YAML file, send a message with the key:value pair that's wrong. So in this case, "environment": "prod" does not match any of the "environment" values from the required_tags section, so I would like to print the violating key:value pair. I was thinking perhaps I have to generate a list of key:values from the YAML first to make them consistent with the data structure of the API call? So the YAML would look more like this?

{
  "environment":"dev",
  "environment":"qa",
  "owner":"*"
}

Still new to some of the dictionary/tuple stuff, so any help would be appreciated.

  • 1
    Does this answer your question? [Validating a yaml document in python](https://stackoverflow.com/questions/3262569/validating-a-yaml-document-in-python) – Tomalak Jan 13 '21 at 02:12
  • 1
    Instead of rolling your own validation code, use existing tools. JSON schema (as described in [this answer](https://stackoverflow.com/a/22231372/18771), also see comments) will do the task just fine. – Tomalak Jan 13 '21 at 02:16
  • Not necessarily. I am trying to say "for each key in YAML, check if any of the values of that key are in the response of the API" – lognarly Jan 13 '21 at 14:05
  • Your API response is JSON. YAML is nothing but a fancy way to write JSON. You could design the YAML so that it conforms to JSON schema structurally, or you can insert values from the YAML into a JSON Schema skeleton and then run the verification. It's easily justifiably to write a bit of plumbing code when that means you can use a tried-and-tested library for the verification task, instead of implementing your own verification solution. (Of course this assumes that your task is a bit larger than *"check that this value is one of those two reference values"*) – Tomalak Jan 13 '21 at 14:21

1 Answers1

0

For data consistency, you should preserve the data structure of the YAML in the dictionary like this:

{
  'environment': ['dev','qa'],
  'owner': '*'
}

So when validating the API_key:API_value of API, you can check the data types of YAML_value then make the right action.

If YAML_value is a list, check if the list contains API_value, or else compare API_value & YAML_value directly

Lê Quang Duy
  • 767
  • 7
  • 14
  • This is what I am doing while utilizing yaml.load. I suppose this is really the other way around, checking the YAML_key:YAML_value and making sure one of those values is returned by the API. So in essence, I am doing: Does the key exist in the API response? If no, flag it. If yes, check the value of the key returned and make sure it is one of those in the YAML values for that key. – lognarly Jan 13 '21 at 13:48