Grokzen / pykwalify

Python YAML/JSON schema validation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using enum with type scalar

jess-sol opened this issue · comments

commented

Running into an error where I'd like to use type: scalar and enum: [true, false, recursive]. Using version 1.6.1.

Schema:

type: scalar
enum: [true, false, recursive]

Example data:

create: recursive

Output:

pykwalify -d test.yaml -s test-schema.yaml -vvvvv
 DEBUG - Setting verbose level: 5
 DEBUG - Arguments from CLI: {'--allow-assertions': False,
 '--data-file': 'test.yaml',
 '--extension': [],
 '--fix-ruby-style-regex': False,
 '--quiet': False,
 '--schema-file': ['test-schema.yaml'],
 '--strict-rule-validation': False,
 '--verbose': 5}
 DEBUG - Using yaml library: /usr/local/lib/python3.6/site-packages/yaml/__init__.py
 DEBUG - source_file: test.yaml
 DEBUG - schema_file: ['test-schema.yaml']
 DEBUG - source_data: None
 DEBUG - schema_data: None
 DEBUG - extension files: []
 DEBUG - loading all extensions : []
 DEBUG - []
 DEBUG - []
 DEBUG - starting core
 DEBUG - Building root rule object
 DEBUG - Init schema: {'type': 'scalar', 'enum': [True, False, 'recursive']}
 DEBUG - Init type value :
 DEBUG - Type: scalar Rule: {'type': 'scalar', 'enum': [True, False, 'recursive']}
 DEBUG - Init enum value :
Traceback (most recent call last):
  File "/usr/local/bin/pykwalify", line 11, in <module>
    sys.exit(cli_entrypoint())
  File "/usr/local/lib/python3.6/site-packages/pykwalify/cli.py", line 95, in cli_entrypoint
    run(parse_cli())
  File "/usr/local/lib/python3.6/site-packages/pykwalify/cli.py", line 82, in run
    c.validate()
  File "/usr/local/lib/python3.6/site-packages/pykwalify/core.py", line 155, in validate
    self._start_validate(self.source)
  File "/usr/local/lib/python3.6/site-packages/pykwalify/core.py", line 197, in _start_validate
    root_rule = Rule(schema=self.schema)
  File "/usr/local/lib/python3.6/site-packages/pykwalify/rule.py", line 66, in __init__
    self.init(schema, "")
  File "/usr/local/lib/python3.6/site-packages/pykwalify/rule.py", line 443, in init
    func_mapping[k](v, rule, path)
  File "/usr/local/lib/python3.6/site-packages/pykwalify/rule.py", line 847, in init_enum_value
    if not isinstance(item, self.type_class):
TypeError: isinstance() arg 2 must be a type or tuple of types

Just commenting out lines 847-852 in pykwalify/rule.py works around the issue. Can't say I know of a better solution to the problem, would like input there.

First off, this is a interesting bug that you found out and that might require some additional checks to avoid it.

But you are still using the rules wrong. In your example, your data section is basically the following json data {"create": "recursive"} and that schema you have will not validate against that. What you basically say with your schema is that you should have one top level item of some kind like only a string or boolean value. But the problem is that you really can't have that kind of data structure becuase you need to have a list or a dict as data on the top level.

So basically you need to alter your schema to something like the following

mapping:
  create:
    type: scalar
    enum: [True, False, Recursvie]

(Did not test the schema but it should work i think)