Skip to content

Commit

Permalink
Fix regex metacharacters in enums
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior authored and shinnar committed Jun 5, 2024
1 parent 088e561 commit 8e65354
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jsonsubschema/_canonicalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jsonschema
import numbers
import math
import re
import sys

import jsonsubschema._constants as definitions
Expand Down Expand Up @@ -272,7 +273,7 @@ def rewrite_enum(d):
ret = None

if t == "string":
pattern = "|".join(map(lambda x: "^"+str(x)+"$", enum))
pattern = "|".join(map(lambda x: "^"+str(re.escape(x))+"$", enum))
ret = {"type": "string", "pattern": pattern}

if t == "integer":
Expand Down
9 changes: 9 additions & 0 deletions test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ def test_enum_uninhabited4(self):
with self.subTest('LHS > RHS'):
self.assertTrue(isSubschema(s2, s1))

def test_enum_regex_string(self):
s1 = {'enum': ['^*']}
s2 = {'enum': ['^^']}

with self.subTest('LHS < RHS'):
self.assertFalse(isSubschema(s1, s2))
with self.subTest('LHS > RHS'):
self.assertFalse(isSubschema(s2, s1))


class TestEnumNotSupported(unittest.TestCase):

Expand Down

0 comments on commit 8e65354

Please sign in to comment.