Skip to content

Support for custom tags? #154

@yamokosk

Description

@yamokosk

I apologize if I missed this in the docs for kwalify or pykwalify, or if this was already addressed in an earlier issue (I have searched but cannot find anything). I'd like to validate a schema that uses custom tags. Between the pyyaml and pykwalify documentation this is the best I was able to come up with. But its obviously not right. Example:

import yaml
from pykwalify.core import Core 

class MyTag(yaml.YAMLObject):
    yaml_tag = u'!MyTag'
    def __init__(self, name, parameter):
        self.name = name
        self.parameter = parameter

    def __repr__(self):
        return "%s(name=%r, parameter=%r)" % (
            self.__class__.__name__, self.name, self.parameter)

data = yaml.load("""
!MyTag
name: "aname"
parameter: 2
""")

print("dump: {}".format(yaml.dump(data)))

schema = yaml.load("""
type: !!python/object:__main__.MyTag
  type: map
  mapping:
    name:
      type: str
    parameter:
      type: int
""")

c = Core(source_data=data, schema_data=schema)
c.validate(raise_exception=True)

Output:

dump: !MyTag {name: aname, parameter: 2}

Traceback (most recent call last):
  File "example.py", line 34, in <module>
    c.validate(raise_exception=True)
  File "/usr/local/lib/python2.7/dist-packages/pykwalify/core.py", line 155, in validate
    self._start_validate(self.source)
  File "/usr/local/lib/python2.7/dist-packages/pykwalify/core.py", line 197, in _start_validate
    root_rule = Rule(schema=self.schema)
  File "/usr/local/lib/python2.7/dist-packages/pykwalify/rule.py", line 66, in __init__
    self.init(schema, "")
  File "/usr/local/lib/python2.7/dist-packages/pykwalify/rule.py", line 399, in init
    path=path,
pykwalify.errors.RuleError: <RuleError: error code 4: Key 'type' in schema rule is not a string type (found MyTag): Path: '/'>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions