-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate the merged style file schema
Still not validating each file checker class see #69
- Loading branch information
1 parent
b48e0a4
commit 1e31d0a
Showing
12 changed files
with
149 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
"""Nitpick exceptions.""" | ||
from pathlib import Path | ||
|
||
|
||
class StyleError(Exception): | ||
"""An error in a style file.""" | ||
|
||
def __init__(self, style_file_name: Path, *args: object) -> None: | ||
def __init__(self, style_file_name: str, *args: object) -> None: | ||
self.style_file_name = style_file_name | ||
super().__init__(*args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""Marshmallow schemas.""" | ||
from typing import Dict | ||
|
||
from marshmallow import Schema, fields | ||
from marshmallow_polyfield import PolyField | ||
from sortedcontainers import SortedDict | ||
|
||
from nitpick.generic import flatten | ||
from nitpick.validators import TrimmedLength | ||
|
||
|
||
class NotEmptyString(fields.String): | ||
"""A string field that must not be empty even after trimmed.""" | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(validate=TrimmedLength(min=1), **kwargs) | ||
|
||
|
||
def flatten_marshmallow_errors(errors: Dict) -> str: | ||
"""Flatten Marshmallow errors to a string.""" | ||
formatted = [] | ||
for field, data in SortedDict(flatten(errors)).items(): | ||
if isinstance(data, list): | ||
messages_per_field = ["{}: {}".format(field, ", ".join(data))] | ||
elif isinstance(data, dict): | ||
messages_per_field = [ | ||
"{}[{}]: {}".format(field, index, ", ".join(messages)) for index, messages in data.items() | ||
] | ||
else: | ||
# This should never happen; if it does, let's just convert to a string | ||
messages_per_field = [str(errors)] | ||
formatted.append("\n".join(messages_per_field)) | ||
return "\n".join(formatted) | ||
|
||
|
||
def string_or_list_field(object_dict, parent_object_dict): # pylint: disable=unused-argument | ||
"""Detect if the field is a string or a list.""" | ||
if isinstance(object_dict, list): | ||
return fields.List(NotEmptyString(required=True, allow_none=False)) | ||
return NotEmptyString() | ||
|
||
|
||
def boolean_or_dict_field(object_dict, parent_object_dict): # pylint: disable=unused-argument | ||
"""Detect if the field is a boolean or a dict.""" | ||
if isinstance(object_dict, dict): | ||
return fields.Dict | ||
return fields.Bool | ||
|
||
|
||
class ToolNitpickSchema(Schema): | ||
"""Validation schema for the ``[tool.nitpick]`` section on ``pyproject.toml``.""" | ||
|
||
style = PolyField(deserialization_schema_selector=string_or_list_field) | ||
|
||
|
||
class NitpickStylesSchema(Schema): | ||
"""Validation schema for the ``[nitpick.styles]`` section on the style file.""" | ||
|
||
include = PolyField(deserialization_schema_selector=string_or_list_field) | ||
|
||
|
||
class NitpickJsonFileSchema(Schema): | ||
"""Validation schema for the ``[nitpick.JsonFile]`` section on the style file.""" | ||
|
||
file_names = fields.List(fields.String) | ||
|
||
|
||
class NitpickSchema(Schema): | ||
"""Validation schema for the ``[nitpick]`` section on the style file.""" | ||
|
||
minimum_version = NotEmptyString() | ||
styles = fields.Nested(NitpickStylesSchema) | ||
files = fields.Dict(fields.String, PolyField(deserialization_schema_selector=boolean_or_dict_field)) | ||
JsonFile = fields.Nested(NitpickJsonFileSchema) | ||
|
||
|
||
class MergedStyleSchema(Schema): | ||
"""Validation schema for the merged style file.""" | ||
|
||
nitpick = fields.Nested(NitpickSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.