Skip to content

Commit

Permalink
chore: validate JSON files with a schema
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Sep 19, 2020
1 parent b3683c4 commit a3169d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/nitpick/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def plugin_class() -> Type["NitpickPlugin"]:


@hookspec
def schema_class(file_name: str, tags: Set[str]) -> Type[Schema]: # pylint: disable=unused-argument
def schema_class(file_name: str, tags: Set[str]) -> Optional[Type[Schema]]: # pylint: disable=unused-argument
"""You should return your schema class here if it handles this file name or tags."""
# FIXME: return tuple in all plugins? Tuple[Type["NitpickPlugin"], Optional[Type[Schema]]

Expand Down
7 changes: 7 additions & 0 deletions src/nitpick/plugins/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from typing import Optional, Set, Type

from marshmallow import Schema
from sortedcontainers import SortedDict

from nitpick import fields
Expand Down Expand Up @@ -91,6 +92,12 @@ def plugin_class() -> Type["NitpickPlugin"]:
return JSONPlugin


@hookimpl
def schema_class(file_name: str, tags: Set[str]) -> Optional[Type[Schema]]: # pylint: disable=unused-argument
"""You should return your schema class here if it handles this file name or tags."""
return JSONFileSchema if "json" in tags else None


@hookimpl
def handle_config_file(config: JsonDict, path_from_root: str, tags: Set[str]) -> Optional["NitpickPlugin"]:
"""Handle JSON files."""
Expand Down
30 changes: 16 additions & 14 deletions src/nitpick/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from collections import OrderedDict
from pathlib import Path
from typing import Dict, List, Optional, Set, Type
from typing import Any, Dict, List, Optional, Set, Type
from urllib.parse import urlparse, urlunparse

import click
Expand Down Expand Up @@ -43,6 +43,7 @@ def __init__(self) -> None:

self._dynamic_schema_class = BaseStyleSchema # type: type
self.rebuild_dynamic_schema()
self.style_errors = {} # type: Dict[str, Any]

@staticmethod
def get_default_style_url():
Expand Down Expand Up @@ -79,28 +80,23 @@ def validate_style(self, style_file_name: str, original_data: JsonDict, is_merge
style_file_name, "Invalid config:", flatten_marshmallow_errors(style_errors)
)

@staticmethod
def validate_schema(
self,
path_from_root: str,
schema: Type[Schema],
style_file_name: str,
original_data: JsonDict,
is_merged_style: bool,
):
"""Validate the schema for the file."""
has_schema = schema is not BaseStyleSchema
data_to_validate = original_data if has_schema else {path_from_root: None}
style_errors = schema().validate(data_to_validate)
if style_errors and has_schema:
style_errors = {path_from_root: style_errors}
local_errors = schema().validate(data_to_validate)
if local_errors and has_schema:
local_errors = {path_from_root: local_errors}

if style_errors:
Deprecation.jsonfile_section(style_errors, is_merged_style)

if style_errors:
NitpickApp.current().add_style_error(
style_file_name, "Invalid config:", flatten_marshmallow_errors(style_errors)
)
if local_errors:
Deprecation.jsonfile_section(local_errors, is_merged_style)
self.style_errors.update(local_errors)

# FIXME: remove flake8/pylint hints
def include_multiple_styles(self, chosen_styles: StrOrList) -> None: # pylint: disable=too-many-locals # noqa: C901
Expand All @@ -125,6 +121,7 @@ def include_multiple_styles(self, chosen_styles: StrOrList) -> None: # pylint:
except ValueError:
display_name = style_uri

self.style_errors = {}
for key, value_dict in toml_dict.items():
from nitpick.config import FileNameCleaner # pylint: disable=import-outside-toplevel

Expand All @@ -138,7 +135,12 @@ def include_multiple_styles(self, chosen_styles: StrOrList) -> None: # pylint:
# otherwise, raise style error "unknown file"
pass
for schema in schemas:
self.validate_schema(cleaner.path_from_root, schema, display_name, value_dict, False)
self.validate_schema(cleaner.path_from_root, schema, value_dict, False)

if self.style_errors:
NitpickApp.current().add_style_error(
display_name, "Invalid config:", flatten_marshmallow_errors(self.style_errors)
)
self._all_styles.add(toml_dict)

sub_styles = search_dict(NITPICK_STYLES_INCLUDE_JMEX, toml_dict, []) # type: StrOrList
Expand Down

0 comments on commit a3169d9

Please sign in to comment.