Grokzen / pykwalify

Python YAML/JSON schema validation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validation against multiple types

jdeluyck opened this issue · comments

Is there a way to allow matching against any number of types? I have a yaml schema where it's allowed either to supply a number, or a string.

Logically this would result in something like

   key:
      - type: str
        required: true
      - type: number
        required: true

You can do this when you are validating a list object. It accepts multiple items/paths for validation.

type: seq
sequence:
  - type: str
    required: true
  - type: int
    required: true

The original kwalify syntax allowed for only one item in the sequence list but it was easily extended to allow for multiple with a few new keywords to controll the requirements flow of how many needed to be allowed for OK validation.

This do not work for a dict. Each key is only allowed to be matched against one specific rule validation by default kwalify standards. However, you can get around this by squeezing in some different regex keywords to get it to validate against multiple scenarios. But you have no flow controll in that case and i think all paths needs to be valid or you get an error.

There has been a suggestion of implementing a keyword taken from JsonSchema any.of to help mitigate this issue and to allow for an arbitrary build of the schema for the user. However, i do not really know right now where i want to take pykwalify and the syntax. It was originally created to allow for a modern runtime of the old kwalify syntax, but i do not know if i want to continue to evolve the syntax further and if i do, in what direction that would give any additional value over existing solutions like JsonSchmea that already is good enough and is used alot.

I do believe pykwalify has a good reason to exist - if even only the fact that it's a lot easier to define a schema in yaml than it is in json, and that I don't have to first run a conversion (and hope it succeeds) to json before being able to use jsonschema.

Yes that is true, but it should be fairly easy to make a wrapper tool that gives the ability to provide a yaml file and the tool converts it to json.