Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
index=["b"],
start_position=Position(line_no=2, column_no=4, char_index=10),
end_position=Position(line_no=2, column_no=6, char_index=12),
)
]
text = "a: 123"
with pytest.raises(ValidationError) as exc_info:
validate_yaml(text, validator=Validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="The field 'b' is required.",
code="required",
index=["b"],
start_position=Position(line_no=1, column_no=1, char_index=0),
end_position=Position(line_no=1, column_no=6, char_index=5),
)
assert value == {"a": 123, "b": 456}
validator = Object(properties=Integer())
text = '{\n "a": "123",\n "b": "abc"}'
with pytest.raises(ValidationError) as exc_info:
validate_json(text, validator=validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="Must be a number.",
code="type",
index=["b"],
start_position=Position(line_no=3, column_no=10, char_index=27),
end_position=Position(line_no=3, column_no=14, char_index=31),
)
]
assert (
repr(exc.messages()[0])
== "Message(text='Must be a number.', code='type', index=['b'], start_position=Position(line_no=3, column_no=10, char_index=27), end_position=Position(line_no=3, column_no=14, char_index=31))"
)
class Validator(Schema):
a = Integer()
b = Integer()
text = '{\n "a": "123",\n "b": "abc"}'
with pytest.raises(ValidationError) as exc_info:
validate_json(text, validator=Validator)
exc = exc_info.value
assert exc.messages() == [
index=["b"],
start_position=Position(line_no=3, column_no=10, char_index=27),
end_position=Position(line_no=3, column_no=14, char_index=31),
)
]
text = '{"a": "123"}'
with pytest.raises(ValidationError) as exc_info:
validate_json(text, validator=Validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="The field 'b' is required.",
code="required",
index=["b"],
start_position=Position(line_no=1, column_no=1, char_index=0),
end_position=Position(line_no=1, column_no=12, char_index=11),
)
def _get_position(self, index: int) -> Position:
content = self._content[: index + 1]
lines = content.splitlines()
line_no = max(len(lines), 1)
column_no = 1 if not lines else max(len(lines[-1]), 1)
return Position(line_no, column_no, index)
def _get_position(content: str, index: int) -> Position:
return Position(
line_no=content.count("\n", 0, index) + 1,
column_no=index - content.rfind("\n", 0, index),
char_index=index,
)
def tokenize_yaml(content: typing.Union[str, bytes]) -> Token:
assert yaml is not None, "'pyyaml' must be installed."
if isinstance(content, bytes):
str_content = content.decode("utf-8", "ignore")
else:
str_content = content
if not str_content.strip():
# Handle the empty string case explicitly for clear error messaging.
position = Position(column_no=1, line_no=1, char_index=0)
raise ParseError(text="No content.", code="no_content", position=position)
class CustomSafeLoader(SafeLoader):
pass
def construct_mapping(loader: "yaml.Loader", node: "yaml.Node") -> DictToken:
start = node.start_mark.index
end = node.end_mark.index
mapping = loader.construct_mapping(node)
return DictToken(mapping, start, end - 1, content=str_content)
def construct_sequence(loader: "yaml.Loader", node: "yaml.Node") -> ListToken:
start = node.start_mark.index
end = node.end_mark.index
value = loader.construct_sequence(node)
return ListToken(value, start, end - 1, content=str_content)
def __eq__(self, other: typing.Any) -> bool:
return (
isinstance(other, Position)
and self.line_no == other.line_no
and self.column_no == other.column_no
and self.char_index == other.char_index
)
def tokenize_json(content: typing.Union[str, bytes]) -> Token:
if isinstance(content, bytes):
content = content.decode("utf-8", "ignore")
if not content.strip():
# Handle the empty string case explicitly for clear error messaging.
position = Position(column_no=1, line_no=1, char_index=0)
raise ParseError(text="No content.", code="no_content", position=position)
decoder = _TokenizingDecoder(content=content)
try:
return decoder.decode(content)
except JSONDecodeError as exc:
# Handle cases that result in a JSON parse error.
position = Position(column_no=exc.colno, line_no=exc.lineno, char_index=exc.pos)
raise ParseError(text=exc.msg + ".", code="parse_error", position=position)