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

Some validation QoL tweaks #948

Merged
merged 3 commits into from
Sep 28, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions optimade/models/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import math
from functools import reduce
from enum import IntEnum, Enum
from sys import float_info
from typing import List, Optional, Union

from pydantic import BaseModel, validator, root_validator, conlist
Expand All @@ -22,7 +21,7 @@
)
from optimade.server.warnings import MissingExpectedField

EXTENDED_CHEMICAL_SYMBOLS = CHEMICAL_SYMBOLS + EXTRA_SYMBOLS
EXTENDED_CHEMICAL_SYMBOLS = set(CHEMICAL_SYMBOLS + EXTRA_SYMBOLS)
ml-evs marked this conversation as resolved.
Show resolved Hide resolved


__all__ = (
Expand All @@ -36,7 +35,8 @@
)


EPS = float_info.epsilon
# Use machine epsilon for single point floating precision
ml-evs marked this conversation as resolved.
Show resolved Hide resolved
EPS = 2 ** -23


Vector3D = conlist(float, min_items=3, max_items=3)
Expand Down Expand Up @@ -145,8 +145,10 @@ class Species(BaseModel):

@validator("chemical_symbols", each_item=True)
def validate_chemical_symbols(cls, v):
if not (v in EXTENDED_CHEMICAL_SYMBOLS):
raise ValueError(f"{v} MUST be in {EXTENDED_CHEMICAL_SYMBOLS}")
if v not in EXTENDED_CHEMICAL_SYMBOLS:
raise ValueError(
f'{v!r} MUST be an element symbol, e.g., "C", "He", or a special symbol from {EXTRA_SYMBOLS}.'
)
return v

@validator("concentration", "mass")
Expand Down Expand Up @@ -944,7 +946,7 @@ def ratios_must_sum_to_one(cls, v):

if abs(sum(v) - 1) > EPS:
raise ValueError(
f"elements_ratios MUST sum to 1 within floating point accuracy. It sums to: {sum(v)}"
f"elements_ratios MUST sum to 1 within (at least single precision) floating point accuracy. It sums to: {sum(v)}"
)
return v

Expand Down
14 changes: 9 additions & 5 deletions optimade/validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,14 +1436,18 @@ def _get_endpoint(
if isinstance(expected_status_code, int):
expected_status_code = [expected_status_code]

if response.status_code not in expected_status_code:
message = f"Request to '{request_str}' returned HTTP code {response.status_code} and not the allowed {expected_status_code}."
message += "\nAdditional details:"
message = f"received expected response: {response}."

if response.status_code != 200:
message = f"Request to '{request_str}' returned HTTP status code {response.status_code}."
message += "\nAdditional details from implementation:"
try:
for error in response.json().get("errors", []):
message += f'\n {error.get("title", "N/A")}: {error.get("detail", "N/A")} ({error.get("source", {}).get("pointer", "N/A")})'
except json.JSONDecodeError:
message += f"\n Could not parse response as JSON. Content type was {response.headers.get('content-type')!r}."
raise ResponseError(message)

return response, f"received expected response: {response}."
if response.status_code not in expected_status_code:
raise ResponseError(message)

return response, message