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

Add "anyOf" to optional fields validation #87

Merged
merged 1 commit into from
Aug 29, 2023
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
26 changes: 15 additions & 11 deletions adtl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pathlib import Path
from functools import lru_cache
from typing import Any, Dict, Iterable, List, Optional, Union, Callable
from more_itertools import unique_everseen

import pint
import tomli
Expand Down Expand Up @@ -431,17 +432,20 @@ def make_fields_optional(
return schema
_schema = copy.deepcopy(schema)
_schema["required"] = sorted(set(schema["required"]) - set(optional_fields))
if "oneOf" in _schema:
if any("required" in _schema["oneOf"][x] for x in range(len(_schema["oneOf"]))):
for x in range(len(_schema["oneOf"])):
_schema["oneOf"][x]["required"] = list(
set(_schema["oneOf"][x]["required"]) - set(optional_fields or [])
)
if all(
all(bool(v) is False for v in _schema["oneOf"][x].values())
for x in range(len(_schema["oneOf"]))
):
_schema.pop("oneOf")
for opt in ["oneOf", "anyOf"]:
if opt in _schema:
if any("required" in _schema[opt][x] for x in range(len(_schema[opt]))):
for x in range(len(_schema[opt])):
_schema[opt][x]["required"] = list(
set(_schema[opt][x]["required"]) - set(optional_fields or [])
)
if all(
all(bool(v) is False for v in _schema[opt][x].values())
for x in range(len(_schema[opt]))
):
_schema.pop(opt)
else:
_schema[opt] = list(unique_everseen(_schema[opt]))
return _schema


Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ dependencies = [
"requests>=2.0.0",
"fastjsonschema==2.16.*",
"tqdm",
"python-dateutil"
"python-dateutil",
"more_itertools"
]

[project.optional-dependencies]
Expand Down
14 changes: 14 additions & 0 deletions tests/schemas/epoch-oneOf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
]
}
],
"anyOf": [
{
"required": [
"sex",
"epoch"
]
},
{
"required": [
"sex_at_birth",
"epoch"
]
}
],
"properties": {
"epoch": {
"description": "Epoch of dataset",
Expand Down
13 changes: 13 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,19 @@ def test_make_fields_optional():
]
assert "oneOf" not in parser.make_fields_optional(schema, ["sex", "sex_at_birth"])

assert schema["anyOf"] == [
{"required": ["sex", "epoch"]},
{"required": ["sex_at_birth", "epoch"]},
]

assert parser.make_fields_optional(schema, ["epoch"])["anyOf"] == [
{"required": ["sex"]},
{"required": ["sex_at_birth"]},
]
assert parser.make_fields_optional(schema, ["sex", "sex_at_birth"])["anyOf"] == [
{"required": ["epoch"]}
]


def test_reference_expansion():
ps_noref = parser.Parser(TEST_PARSERS_PATH / "groupBy.json")
Expand Down
Loading