This commit is contained in:
Reid 'arrdem' McKenzie 2021-05-31 12:28:46 -06:00
commit 03b37675b5
32 changed files with 193 additions and 169 deletions
projects/yamlschema
setup.py
src/python/yamlschema
test/python

View file

@ -1,5 +1,6 @@
from setuptools import setup
setup(
name="arrdem.yamlschema",
# Package metadata

View file

@ -2,11 +2,11 @@
JSONSchema linting for YAML documents.
"""
import logging
import typing as t
from enum import Enum
from io import StringIO
import logging
import re
import typing as t
import yaml
from yaml.nodes import MappingNode, Node, ScalarNode, SequenceNode
@ -58,9 +58,13 @@ class YamlLinter(object):
schema = self._schema
for e in path:
if not e:
raise ValueError(f"Unable to dereference {ref}; contains empty segment!")
raise ValueError(
f"Unable to dereference {ref}; contains empty segment!"
)
if not (schema := schema.get(e)):
raise ValueError(f"Unable to dereference {ref}; references missing sub-document!")
raise ValueError(
f"Unable to dereference {ref}; references missing sub-document!"
)
return schema
@ -175,7 +179,10 @@ class YamlLinter(object):
else:
yield LintRecord(
LintLevel.MISSMATCH, node, schema, f"Expected an integer, got a {node.tag}"
LintLevel.MISSMATCH,
node,
schema,
f"Expected an integer, got a {node.tag}",
)
def lint_number(self, schema, node: Node) -> t.Iterable[LintRecord]:
@ -185,7 +192,10 @@ class YamlLinter(object):
else:
yield LintRecord(
LintLevel.MISSMATCH, node, schema, f"Expected an integer, got a {node.tag}"
LintLevel.MISSMATCH,
node,
schema,
f"Expected an integer, got a {node.tag}",
)
def _lint_num_range(self, schema, node: Node, value) -> t.Iterable[LintRecord]:

View file

@ -2,9 +2,8 @@
Tests covering the YAML linter.
"""
from yamlschema import lint_buffer
import pytest
from yamlschema import lint_buffer
@pytest.mark.parametrize(
@ -100,20 +99,31 @@ def test_lint_document_fails(msg, schema, obj):
assert list(lint_buffer(schema, obj)), msg
@pytest.mark.parametrize("msg, schema, obj", [
("Basic usage of $ref",
{"$ref": "#/definitions/Foo",
"definitions": {
"Foo": {"type": "string"},
}},
"---\nfoo"),
("Use of nested references",
{"$ref": "#/definitions/Foos",
"definitions": {
"Foos": {"type": "array", "items": {"$ref": "#/definitions/Foo"}},
"Foo": {"type": "string"},
}},
"---\n- foo\n- bar\n- baz"),
])
@pytest.mark.parametrize(
"msg, schema, obj",
[
(
"Basic usage of $ref",
{
"$ref": "#/definitions/Foo",
"definitions": {
"Foo": {"type": "string"},
},
},
"---\nfoo",
),
(
"Use of nested references",
{
"$ref": "#/definitions/Foos",
"definitions": {
"Foos": {"type": "array", "items": {"$ref": "#/definitions/Foo"}},
"Foo": {"type": "string"},
},
},
"---\n- foo\n- bar\n- baz",
),
],
)
def test_ref_references(msg, schema, obj):
assert not list(lint_buffer(schema, obj)), msg