Skip to content

Commit c8a8177

Browse files
committed
Fix VlanRange json schema regex
1 parent 34d0863 commit c8a8177

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

nwastdlib/vlans.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pydantic.json_schema import JsonSchemaValue
2626
from pydantic_core import CoreSchema, SchemaSerializer, core_schema
2727

28+
VLAN_RANGE_JSON_SCHEMA_REGEX = r"^(?:([1-9]|[1-9]\d{1,2}|[1-4]\d{3})(?:-(?:[1-9]|[1-9]\d{1,2}|[1-4]\d{3}))?)$"
29+
2830

2931
def to_ranges(i: Iterable[int]) -> Iterable[range]:
3032
"""Convert a sorted iterable of ints to an iterable of range objects.
@@ -277,7 +279,7 @@ def __get_pydantic_json_schema__(cls, core_schema_: CoreSchema, handler: GetJson
277279
schema_override = {
278280
"type": "string",
279281
"format": "vlan",
280-
"pattern": "^([1-4][0-9]{0,3}(-[1-4][0-9]{0,3})?,?)+$",
282+
"pattern": VLAN_RANGE_JSON_SCHEMA_REGEX,
281283
"examples": ["345", "20-23,45,50-100"],
282284
}
283285
return json_schema_resolved | schema_override

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ test = [
6060
]
6161
dev = [
6262
"bumpversion",
63+
"jsonschema",
6364
"pre-commit",
6465
"types-redis"
6566
]

tests/test_vlans.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import jsonschema
12
import pytest
23
from pydantic import BaseModel, ConfigDict, ValidationError
34

4-
from nwastdlib.vlans import VlanRanges
5+
from nwastdlib.vlans import VLAN_RANGE_JSON_SCHEMA_REGEX, VlanRanges
56

67

78
def test_vlan_ranges_instantiation():
@@ -235,7 +236,7 @@ class MyModel(BaseModel):
235236
"title": "Vlanranges",
236237
"type": "string",
237238
"format": "vlan",
238-
"pattern": "^([1-4][0-9]{0,3}(-[1-4][0-9]{0,3})?,?)+$",
239+
"pattern": VLAN_RANGE_JSON_SCHEMA_REGEX,
239240
"examples": ["345", "20-23,45,50-100"],
240241
},
241242
},
@@ -293,3 +294,37 @@ def get_dummy_model():
293294

294295
response = fastapi_test_client.get("/dynamic_model")
295296
assert response.json() == {"name": "DummyModel", "vlanrange": "10"}
297+
298+
299+
@pytest.fixture(scope="module")
300+
def vr_schema():
301+
class MyModel(BaseModel):
302+
vr: VlanRanges
303+
304+
return MyModel.model_json_schema()
305+
306+
307+
@pytest.mark.parametrize("vlan", [1, 9, 10, 49, 50, 99, 100, 101, 499, 500, 999, 1000, 3999, 4000, 4999])
308+
def test_vlan_allowed_by_schema(vlan, vr_schema):
309+
"""Test vlans which should be allowed by the JSON Schema."""
310+
assert jsonschema.validate({"vr": str(vlan)}, vr_schema) is None
311+
312+
313+
@pytest.mark.parametrize("vlan", [-5000, -50, -1, 0, 5000, 5001, 9999, 10000, 14999])
314+
def test_vlan_rejected_by_schema(vlan, vr_schema):
315+
"""Test vlans which should be rejected by the JSON Schema."""
316+
with pytest.raises(jsonschema.ValidationError):
317+
jsonschema.validate({"vr": str(vlan)}, vr_schema)
318+
319+
320+
@pytest.mark.parametrize("vlanrange", ["1-1", "9-9", "49-59", "99-300", "999-1001", "3999-4999"])
321+
def test_vlan_ranges_allowed_by_schema(vlanrange, vr_schema):
322+
"""Test vlan ranges which should be allowed by the JSON Schema."""
323+
assert jsonschema.validate({"vr": vlanrange}, vr_schema) is None
324+
325+
326+
@pytest.mark.parametrize("vlanrange", ["-1", "-10-0", "0-1", "4999-5000", "5000-5002"])
327+
def test_vlan_ranges_rejected_by_schema(vlanrange, vr_schema):
328+
"""Test vlan ranges which should be rejected by the JSON Schema."""
329+
with pytest.raises(jsonschema.ValidationError):
330+
jsonschema.validate({"vr": vlanrange}, vr_schema)

0 commit comments

Comments
 (0)