Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: validate source objects in poetry #5678

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from tomlkit.toml_document import TOMLDocument

from poetry.config.config import Config
from poetry.json import validate_object
from poetry.packages.locker import Locker
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager
Expand Down Expand Up @@ -300,3 +301,13 @@ def create_pyproject_from_package(
)

return cast(TOMLDocument, pyproject)

@classmethod
def validate(
cls, config: dict[str, Any], strict: bool = False
) -> dict[str, list[str]]:
results = super().validate(config, strict)

results["errors"].extend(validate_object(config))

return results
27 changes: 19 additions & 8 deletions src/poetry/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import json
import os

from pathlib import Path
from typing import Any

import jsonschema

from poetry.core.json import SCHEMA_DIR as CORE_SCHEMA_DIR


SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")

Expand All @@ -16,14 +19,9 @@ class ValidationError(ValueError):
pass


def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]:
schema_file = os.path.join(SCHEMA_DIR, f"{schema_name}.json")

if not os.path.exists(schema_file):
raise ValueError(f"Schema {schema_name} does not exist.")

with open(schema_file, encoding="utf-8") as f:
schema = json.loads(f.read())
def validate_object(obj: dict[str, Any]) -> list[str]:
schema_file = Path(SCHEMA_DIR, "poetry.json")
schema = json.loads(schema_file.read_text(encoding="utf-8"))

validator = jsonschema.Draft7Validator(schema)
validation_errors = sorted(
Expand All @@ -41,4 +39,17 @@ def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]:

errors.append(message)

core_schema = json.loads(
Path(CORE_SCHEMA_DIR, "poetry-schema.json").read_text(encoding="utf-8")
)

if core_schema["additionalProperties"]:
# TODO: make this un-conditional once core update to >1.1.0b2
properties = {*schema["properties"].keys(), *core_schema["properties"].keys()}
additional_properties = set(obj.keys()) - properties
for key in additional_properties:
errors.append(
f"Additional properties are not allowed ('{key}' was unexpected)"
)

return errors
Loading