-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import json | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.core.validators import BaseValidator | ||
from django.utils.translation import gettext as _ | ||
|
||
from py_mini_racer import JSArray, JSObject, MiniRacer | ||
|
||
|
||
class JsValidator(BaseValidator): | ||
|
||
@property | ||
def code(self): | ||
return self.limit_value | ||
|
||
def __call__(self, value): | ||
ctx = MiniRacer() | ||
|
||
pickled = json.dumps(value or "") | ||
base = f"var value = {pickled};" | ||
ctx.eval(base) | ||
ret = ctx.eval(self.code) | ||
|
||
# try: | ||
# ret = jsonpickle.decode(result) | ||
# except (JSONDecodeError, TypeError): | ||
# ret = result | ||
|
||
if isinstance(ret, JSArray): | ||
raise ValidationError(list(ret)) | ||
|
||
if isinstance(ret, JSObject): | ||
errors = {s: k for s, k in ret.items()} | ||
raise ValidationError(errors) | ||
|
||
if isinstance(ret, str): | ||
raise ValidationError(_(ret)) | ||
elif isinstance(ret, bool) and not ret: | ||
raise ValidationError(_("Please insert a valid value")) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from django.core.exceptions import ValidationError | ||
|
||
import pytest | ||
|
||
from hope_flex_fields.validators import JsValidator | ||
|
||
|
||
def test_jsvalidator(): | ||
code = "result=value*2" | ||
v = JsValidator(code) | ||
assert v(22) | ||
|
||
|
||
# | ||
# def test_jsvalidator_error(): | ||
# code = 'value=2' | ||
# v = JsValidator(code) | ||
# with pytest.raises(ValueError) as e: | ||
# v(22) | ||
# assert e.value.messages == [' Validator code must returns something'] | ||
|
||
|
||
def test_jsvalidator_fail(): | ||
code = "result=value==2" | ||
v = JsValidator(code) | ||
with pytest.raises(ValidationError) as e: | ||
v(22) | ||
assert e.value.messages == ["Please insert a valid value"] | ||
|
||
|
||
def test_jsvalidator_return_dict(): | ||
code = 'result={"value": "error"}' | ||
v = JsValidator(code) | ||
with pytest.raises(ValidationError) as e: | ||
v(22) | ||
assert e.value.message_dict == {"value": ["error"]} | ||
|
||
|
||
def test_jsvalidator_return_tuple(): | ||
code = 'result=["1","2","3"]' | ||
v = JsValidator(code) | ||
with pytest.raises(ValidationError) as e: | ||
v(22) | ||
assert e.value.messages == ["1", "2", "3"] | ||
|
||
|
||
def test_jsvalidator_fail_custom_message(): | ||
code = """ | ||
if (value <2){ | ||
result = "Provided number must be less than or equal to 2!"; | ||
}else if (value > 5){ | ||
result = "Provided number must be greater than or equal to 5!"; | ||
} | ||
""" | ||
v = JsValidator(code) | ||
with pytest.raises(ValidationError) as e: | ||
v(22) | ||
assert e.value.messages == ["Provided number must be greater than or equal to 5!"] | ||
|
||
|
||
def test_jsvalidator_return_error(): | ||
code = "result={}" | ||
v = JsValidator(code) | ||
with pytest.raises(ValidationError) as e: | ||
v(22) | ||
assert e.value.message_dict == {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters