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

Support for custom tags? #154

Open
yamokosk opened this issue Jul 31, 2019 · 3 comments
Open

Support for custom tags? #154

yamokosk opened this issue Jul 31, 2019 · 3 comments

Comments

@yamokosk
Copy link

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: '/'>
@Grokzen
Copy link
Owner

Grokzen commented Aug 1, 2019

Custom tags is not supported natively and i do not have any immediate plans for supporting them right now.

@Grokzen
Copy link
Owner

Grokzen commented Oct 19, 2019

@yamokosk Can you supply a working script that correctly loads the custom tag you made and where it would build up a data object that acctually work on the python side so i can then implement this in the code base?

@Grokzen
Copy link
Owner

Grokzen commented Apr 17, 2022

@yamokosk So i was thinking, in theory you should be able to add in any tag by modifying the following variable https://github.com/Grokzen/pykwalify/blob/master/pykwalify/compat.py#L10 that is the ruamel.yaml object and you should be able to add in any tags you want same way as here https://github.com/Grokzen/pykwalify/blob/master/pykwalify/core.py#L68 and if you do this before you construct your Core object it should be added to your parser when loading your schema/data files. It might be some issues with the ordering of imports and adding the tag, but it should be possible to make that work i guess.

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