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

Validate if list contains dictionaries without validating keys in dictionary #54

Closed
smalleni opened this issue Mar 29, 2016 · 7 comments

Comments

@smalleni
Copy link

If scenario file has a list of dictionaries and the keys could be different each time, and user doesn't want to test for key names but just wants to check if the the sequence is a list of dictionaries.

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

In the above case, user just wants to check if plugins is a list of key value pairs.
The above schema validation throws an exception. It would be nice to have this feature as discussed in #53

@jmtd
Copy link

jmtd commented Jul 27, 2016

I think I've hit this. For completion's sake here's a minimal test case, and the exception thrown

data:

 - zomg: lol

schema:

seq:
  - map:
      allowempty: True

exception:

Traceback (most recent call last):
  File "kwalbug.py", line 20, in <module>
    c.validate(raise_exception=True)
  File "pykwalify/core.py", line 147, in validate
    self._start_validate(self.source)
  File "pykwalify/core.py", line 187, in _start_validate
    root_rule = Rule(schema=self.schema)
  File "pykwalify/rule.py", line 62, in __init__
    self.init(schema, "")
  File "pykwalify/rule.py", line 346, in init
    func_mapping[k](v, rule, path)
  File "pykwalify/rule.py", line 833, in init_mapping_value
    rule.init(v, u"{}/mapping/{}".format(path, k))
  File "pykwalify/rule.py", line 346, in init
    func_mapping[k](v, rule, path)
  File "pykwalify/rule.py", line 764, in init_sequence_value
    rule.init(elem, u"{}/sequence/{}".format(path, i))
  File "pykwalify/rule.py", line 346, in init
    func_mapping[k](v, rule, path)
  File "pykwalify/rule.py", line 833, in init_mapping_value
    rule.init(v, u"{}/mapping/{}".format(path, k))
  File "pykwalify/rule.py", line 278, in init
    include = schema.get("include", None)
AttributeError: 'bool' object has no attribute 'get'

Version: 1.5.1 (via PyPI)

@jmtd
Copy link

jmtd commented Jul 27, 2016

..actually I hit it with even the following, no seq involved

schema:

map:
  foo: {type: str}
  allowempty: True

data:

foo: asads
bar: asad

@Grokzen
Copy link
Owner

Grokzen commented Jul 27, 2016

@jmtd Have you tried the schema/data with the latest commit in unstable branch?

@jmtd
Copy link

jmtd commented Aug 26, 2016

Hi, sorry for the delay. Yes I can confirm this with 6596293

@Grokzen
Copy link
Owner

Grokzen commented Dec 21, 2016

@smalleni Your specific usecase and schema you posted is now fixed as of this commit cd012c9 and you can see your data/schema was used and it now validated properly as expected.

@smalleni @jmtd I would NOT recommend to use the map: keyword for now becuase it is still buggy with the first example you provided @jmtd

# DO NOT USE
seq:
  - map:
      allowempty: True

# USE THIS INSTEAD
type: seq
sequence:
  - type: map
    allowempty: True

I will keep investigating why seq & map bugs out in that way.

@Grokzen
Copy link
Owner

Grokzen commented Dec 21, 2016

@jmtd A comment after some thinking regarding the problems with seq: and - map: is that it is currently not possible to have allowempty: True in the way i showed in the last comment becuase the reason that keyword/shortcut was implemented was to make it easier to write instead of having to specify type: map & mapping: {...} so it would be easier to write and understand what you wanted. The problem with this is that all subkeys that you specify inside the value part for the map will be considered a regular key/value pair that it should validate against data and not a keyword that it acctually is. I consider this correct behavriour from the implementation and i basically say that all extra keywords that you normally can specify for a seq and map keywords is not possible to have in the schema as it is right now. The only consitent way would be to do the following:

seq:
  - map:
      allowempty: True
      mapping: {}

but by doing this it makes it really confusing compared to the normal way it is done and i would have to rewrite alot of logic just to change it into this and that is not something i will do. So the current implementaitno and the suggestion/directive i mentioned above to use USE THIS INSTEAD section stands as the only supported way to use extra keywords for a map type.

I am closing this issue becuase there is no more issues to deal within this issue. The original issue was solved as mentioned above through commit cd012c9

@Grokzen Grokzen closed this as completed Dec 21, 2016
@jmtd
Copy link

jmtd commented Jan 10, 2017

I think I understand, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants