Grokzen / pykwalify

Python YAML/JSON schema validation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing of structure (sequence is not a list, xx is not a map)

jdeluyck opened this issue · comments

I'm having some issues getting the following yaml structure parsed:

defaults: &defaults
    quota: 10

projects: &projects
    - proj1:
          <<: *defaults
          owner: owner1
          ownername: "Foo"
          status: Created
          expiration: 2018/03/25
    - proj2:
          <<: *defaults
          owner: owner2
          ownername: "Bar"
          status: Created
          expiration: 2017/11/11

I've created the following schema:

type: map
allowempty: True
mapping:
  projects:
    type: seq
    sequence:
      type: map
      mapping:
        owner:
          type: str
          required: True
        ownername:
          type: str
          required: True
        status:
          type: str
          required: True
        expiration:
          type: timestamp
          required: True
        quota:
          type: str
          required: True

but this results in

pykwalify.errors.RuleError: <RuleError: error code 4: Sequence keyword is not a list: Path: '/mapping/projects'>

Second attempt, remapping to a map:

type: map
allowempty: True
mapping:
  projects:
    type: map
    mapping:
      regex;(.*):
        type: map
        mapping:
          owner:
            type: str
            required: True
          ownername:
            type: str
            required: True
          status:
            type: str
            required: True
          expiration:
            type: timestamp
            required: True
          quota:
            type: str
            required: True

Alas:

 ERROR - validation.invalid
 ERROR -  --- All found errors ---
 ERROR - [u"Value '[{'proj1': {'status': 'Created', 'owner': 'owner1', 'ownername': 'Foo', 'expiration': '2018/03/25', 'quota': 10}}, {'proj2': {'status': 'Created', 'owner': 'owner2', 'ownername': 'Bar', 'expiration': '2017/11/11', 'quota': 10}}]' is not a dict. Value path: '/projects'"]

pykwalify.errors.SchemaError: <SchemaError: error code 2: Schema validation failed:
 - Value '[{'proj1': {'status': 'Created', 'owner': 'owner1', 'ownername': 'Foo', 'expiration': '2018/03/25', 'quota': 10}}, {'proj2': {'status': 'Created', 'owner': 'owner2', 'ownername': 'Bar', 'expiration': '2017/11/11', 'quota': 10}}]' is not a dict. Value path: '/projects'.: Path: '/'>

so, um, what am I doing wrong?

Items inside a sequence must be a list, just like the error message says.

type: map
allowempty: True
mapping:
  projects:
    type: seq
    sequence:
      - type: map
        mapping:
          owner:
            type: str
            required: True
          ownername:
            type: str
            required: True
          status:
            type: str
            required: True
          expiration:
            type: timestamp
            required: True
          quota:
            type: str
            required: True

The second validation error is becuase of your data is wrong formated for that schema.

projects: &projects
    - proj1:
          <<: *defaults

You basically have a dict on the root level, with a key projects and the value for that key is a list. Your data basically translates to this json representation

{
  "projects": [
    {
      "proj1": {...}
    }
  ]
}

I just went back to check the documentation, and found that tiny detail (needs to be a list). Stupid ;)