Skip to content

Commit

Permalink
Fixed #17
Browse files Browse the repository at this point in the history
  • Loading branch information
noamgat committed Nov 6, 2023
1 parent 66e2a79 commit e5825b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lmformatenforcer/jsonschemaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def get_allowed_characters(self) -> str:
)
required_keys = self.schema_object.required or []
can_end = set(self.existing_keys).issuperset(required_keys)
can_parse_key = self.is_dictionary or set(possible_keys).issuperset(
can_parse_key = self.is_dictionary or set(possible_keys).difference(
self.existing_keys
)

Expand Down
6 changes: 5 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from lmformatenforcer.exceptions import LMFormatEnforcerException


class CharacterNotAllowedException(LMFormatEnforcerException):
pass


def assert_parser_with_string(string: str, parser: CharacterLevelParser, expect_success: bool):
for idx, character in enumerate(string):
try:
if character in parser.get_allowed_characters():
parser = parser.add_character(character)
else:
if expect_success:
raise ValueError(f"Parser does not allow '{character}' at index {idx}")
raise CharacterNotAllowedException(f"Parser does not allow '{character}' at index {idx}")
else:
return # Success
except LMFormatEnforcerException:
Expand Down
13 changes: 12 additions & 1 deletion tests/test_jsonschemaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from lmformatenforcer.exceptions import LMFormatEnforcerException

from .common import assert_parser_with_string
from .common import assert_parser_with_string, CharacterNotAllowedException


def _test_json_schema_parsing_with_string(string: str, schema_dict: dict, expect_success: bool):
Expand Down Expand Up @@ -210,4 +210,15 @@ def test_string_escaping():
# Unicode digit outside of hex range
test_string = f'{{"num":1,"message":"hello {BACKSLASH}uf9fP world"}}'
_test_json_schema_parsing_with_string(test_string, SampleModel.schema(), False)


def test_comma_after_all_object_keys_fails():
class SomeSchema(BaseModel):
key: str

test_string = '{"key": "val",'
with pytest.raises(CharacterNotAllowedException):
_test_json_schema_parsing_with_string(test_string, SomeSchema.schema(), True)



0 comments on commit e5825b8

Please sign in to comment.