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 key to be specified or left empty #53

Closed
smalleni opened this issue Mar 28, 2016 · 8 comments
Closed

Allow key to be specified or left empty #53

smalleni opened this issue Mar 28, 2016 · 8 comments

Comments

@smalleni
Copy link

Is this feature present in the current release?

I am using a single schema file to validate against different scenarios. Some of the scenarios have a specific key and some don't. I would like to be able to specify in the schema to ignore if the key isn't present.

@Grokzen
Copy link
Owner

Grokzen commented Mar 28, 2016

@smalleni Can you provide the schema/data you are having problems with or can you provide an example schema/data that breaks? Would make it easier to help.

@smalleni
Copy link
Author

So, I'm using pykwalify to verify the configuration file I am passing to a test suite.
Primarily, the test suite consists of tests for "rally", "perfkit" and "shaker" so all the options required for these tests are passed through a confiugration file.
However, sometimes a configuration file with only options for one of the tests mentioned above is passed and the code in the test suite executes only those tests related. So If i build a single schema file that covers all the three tests, I am unsu8re how to handle the case where the scenario file contains only one test and the schema file includes all the tests

Example scenario file:

rally:
  enabled: true
  sleep_before: 5
  sleep_after: 5
shaker:
  enabled: true
  server: (Address of machine running browbeat-Undercloud)
  port: 5555
perfkit:
  enabled: true
  sleep_before: 0
  sleep_after: 0
  venv: /home/stack/perfkit-venv/bin/activate
  default:
    image: centos7
    machine_type: m1.small

So my question is how do I write the schema file to ignore say "rally" if the scenario file doesnt contain "rally". This is more of a question rather than an issue maybe. Sorry.

@Grokzen
Copy link
Owner

Grokzen commented Mar 28, 2016

It sounds like you are looking for conditional validation and there is already a similar issue for that here #35 where you can read up on some of the things i suggested over there.

The short answer is that there is no direct support for simple conditional validation where you can say if this then that or if not this then that and similar things. This has not been implemented because it is not a part of the original kwalify specs and i have not yet come up with a schema specification that can implement this in a good way. The only thing i know right now that i have implemented is to use a custom extension to provide some custom logic that you can implement, but you should know that the extensions is limited and might not provide what you seek.

However, if you want to test a middle ground that i think might work for your case, it could be to use the required=False keyword in certain places. I tried the following data and schema and i get it to validate.

# Data
rally:
  enabled: true
  sleep_before: 5
  sleep_after: 5
perfkit:
  enabled: true
  sleep_before: 0
  sleep_after: 0
  venv: /home/stack/perfkit-venv/bin/activate
  default:
    image: centos7
    machine_type: m1.small

# Schema
type: map
mapping:
    rally:
        required: False
        type: map
        allowempty: True
    shaker:
        required: False
        type: map
        allowempty: True
    perfkit:
        required: False
        type: map
        allowempty: True

With this, i could for example remove the shaker block from your data and it would still validate the data for you. This would mean that a certain block could be missing from your data but it would still work. If this however is not good enough, i would suggest you try a custom extension and func on the mapping level and do a more custom validation logic if you have some other more custom if/else logic to your data.

@smalleni
Copy link
Author

@Grokzen ..thanks for the detailed explanation. Appreciate it

@Grokzen
Copy link
Owner

Grokzen commented Mar 28, 2016

No problems :) If you find a solution that works, pleas share it and close the issue if it works.

@smalleni
Copy link
Author

@Grokzen ..just another question..my scenario file has a list of dictionaries and the keys could be different each time, so i don't want to test for names but just want to check if the the sequence is a list of dictionaries.

#Data
rally:
  plugins:
    - netcreate-boot: rally/rally-plugins/netcreate-boot 
#Schema
rally:
    type: map
    allowempty: True
    mapping:
      plugins:
        type: seq
        sequence:
        - type: map
          allowempty: True

Validation throws an exception, looks like the key values are being expected for the mapping. Anyway to get around this?

@Grokzen
Copy link
Owner

Grokzen commented Mar 28, 2016

Yeah, that looks more like a bug. There are some special cases and processing that is done when a map is found inside a sequence and it could be a missed case that is not working properly. I will add it to my TODO list :)

@smalleni
Copy link
Author

@Grokzen I can open an issue for that if that helps. Thanks.
Having required set to False serves the purpose of getting around this issue. Closing it.

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

2 participants