Grokzen / pykwalify

Python YAML/JSON schema validation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issue while using "unique: true" for the sequence data type

MinalRajendra opened this issue · comments

Environment

  • Python version: 3.5.2
  • PyKwalify version: 1.8.0

Steps to Reproduce

  1. create input YML file as below
    test.yml

mydata:
name: 123
age: ABCD
hobbies: [cooking, painting, cooking]

  1. create YML schema as below

type: map
mapping:
 mydata:
  type: map
  mapping:
    name:
      type: str
      range:
       min: 1
       max: 10
    age:
       type: int
       range:
          min: 21
          max: 100
    hobbies:
       type: seq
       sequence:
       -  type: str
          unique: true
          required: true
  1. Write Python test case for validation as below
    validate_yml.py
#! /usr/bin/python3

@author: Minal Deshmukh

import pykwalify
from pykwalify.core import Core, SchemaError
from pykwalify.errors import RuleError

yaml_file = "./test.yml"
schema_file = "./schema.yml"
yaml_schema = Core(source_file=yaml_file, schema_files=[schema_file])
try:
    pykwalify.init_logging(0)
    yaml_schema.validate(raise_exception=True)
except (SchemaError, RuleError):
    for error in yaml_schema.errors:
        print("error -->",error)
        print ("Data Type of error is -->",type(error))
  1. Execute test case as below
    python3 validate_yml.py

OUTPUT

error --> Value 'ABCD' is not of type 'int'. Path: '/mydata/age'
Data Type of error is --> <class 'pykwalify.errors.SchemaError.SchemaErrorEntry'>
error --> Value '123' is not of type 'str'. Path: '/mydata/name'
Data Type of error is --> <class 'pykwalify.errors.SchemaError.SchemaErrorEntry'>
error --> Value 'cooking' is not unique. Previous path: '/mydata/hobbies/0'. Path: '/mydata/hobbies/2'
Data Type of error is --> <class 'str'>

Schema

type: map
mapping:
 mydata:
  type: map
  mapping:
    name:
      type: str
      range:
       min: 1
       max: 10
    age:
       type: int
       range:
          min: 21
          max: 100
    hobbies:
       type: seq
       sequence:
       -  type: str
          unique: true
          required: true

Data

mydata:
    name: 123
    age: ABCD
    hobbies: [cooking, painting, cooking]

Expected Behavior

Expected data type of the error below is 'pykwalify.errors.SchemaError.SchemaErrorEntry'

error --> Value 'cooking' is not unique. Previous path: '/mydata/hobbies/0'. Path: '/mydata/hobbies/2'
Data Type of error is --> <class 'str'>

Observed Behavior

Data type is different than the other error data types
error --> Value 'cooking' is not unique. Previous path: '/mydata/hobbies/0'. Path: '/mydata/hobbies/2'
Data Type of error is --> <class 'str'>

Mkay, good catch. I think the error stems from 2 different places

https://github.com/Grokzen/pykwalify/blob/master/pykwalify/core.py#L449 is where the errors is stored during parsing to later be added to the errors list. But when the iterator runs over this temporary lits here https://github.com/Grokzen/pykwalify/blob/master/pykwalify/core.py#L468 we run iteration over a dict and i guess you get out the keys only and in this case that would be the __repr__ part of the key and not the exception object itself.

Mkay, good catch. I think the error stems from 2 different places

https://github.com/Grokzen/pykwalify/blob/master/pykwalify/core.py#L449 is where the errors is stored during parsing to later be added to the errors list. But when the iterator runs over this temporary lits here https://github.com/Grokzen/pykwalify/blob/master/pykwalify/core.py#L468 we run iteration over a dict and i guess you get out the keys only and in this case that would be the __repr__ part of the key and not the exception object itself.

Thank you for looking into it & your immediate response.
When can we expect fix for this issue?

@MinalRajendra I can't give any estimate as i can't give this project to much of my free time right now. If you want a fix, the absolute fastest way is for you to test to modify the parts above i mentioned and submit a PR. Otherwise, maybe in Dec, most likley in Jan at this point