Skip to content

Commit 3901eab

Browse files
committed
make it more restrictive
1 parent a003313 commit 3901eab

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

reproschema/models/model.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ class AdditionalProperty(Thing):
246246
description="Background image for drawing activities.",
247247
)
248248

249+
@model_validator(mode="after")
250+
def validate_only_background_image_extra(self):
251+
"""Validate that only backgroundImage is allowed as an extra field."""
252+
# Get any extra fields that weren't explicitly defined
253+
extra_fields = getattr(self, "__pydantic_extra__", {})
254+
255+
if extra_fields:
256+
# Since backgroundImage is now an explicit field, any extra fields are not allowed
257+
extra_field_names = list(extra_fields.keys())
258+
raise ValueError(
259+
f"Extra fields are not permitted in AdditionalProperty. "
260+
f"Only 'backgroundImage' is allowed as an additional field, "
261+
f"but found: {extra_field_names}"
262+
)
263+
264+
return self
265+
249266
id: Optional[str] = Field(
250267
None,
251268
description="A unique identifier for an entity. Must be either a CURIE shorthand for a URI or a complete URI.",

reproschema/models/tests/test_schema.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,33 @@ def test_background_image_always_allowed():
238238
)
239239

240240

241+
def test_extra_fields_rejected():
242+
"""Test that extra fields other than backgroundImage are rejected."""
243+
activity_dict = {
244+
"category": "Activity",
245+
"id": "activity_with_invalid_extra.jsonld",
246+
"prefLabel": {"en": "Activity with Invalid Extra Field"},
247+
"description": {"en": "An activity with an invalid extra field"},
248+
"schemaVersion": "1.0.0-rc4",
249+
"version": "0.0.1",
250+
"ui": {
251+
"inputType": "radio",
252+
"addProperties": [
253+
{
254+
"isAbout": "item1",
255+
"variableName": "test_item",
256+
"backgroundImage": "./images/test.png", # This is allowed
257+
"customTool": "special_pen", # This should be rejected
258+
}
259+
],
260+
},
261+
}
262+
263+
# This should fail because customTool is not allowed
264+
with pytest.raises(ValueError, match="Extra fields are not permitted"):
265+
Activity(**activity_dict)
266+
267+
241268
def test_activity_without_extra_fields_works():
242269
"""Test that activities without extra fields work normally regardless of input type."""
243270
activity_dict = {

0 commit comments

Comments
 (0)