Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIS-8339] FLOAT should also accept integer #43

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

3.5.0
-----
- [feature] add `NUMBER` that accepts float and integer


3.4.1
-----
- Fix `dsv` decorator to work with `DSVError`.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ validate_data_spec(multirow_data, SingleSpec, multirow=True) # return True
### FLOAT
`float_field = Checker([FLOAT])` or `Checker([float])`

### NUMBER
`number_field = Checker([NUMBER])`

### STR
`str_field = Checker([STR])` or `Checker([str])`

Expand Down
2 changes: 1 addition & 1 deletion data_spec_validator/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.4.1'
__version__ = '3.5.0'
2 changes: 2 additions & 0 deletions data_spec_validator/spec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
LIST,
LIST_OF,
NONE,
NUMBER,
ONE_OF,
REGEX,
SELF,
Expand Down Expand Up @@ -66,6 +67,7 @@
"LIST",
"LIST_OF",
"NONE",
"NUMBER",
"ONE_OF",
"REGEX",
"SELF",
Expand Down
3 changes: 3 additions & 0 deletions data_spec_validator/spec/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
LIST,
LIST_OF,
NONE,
NUMBER,
ONE_OF,
RAW_CHECK_TYPE,
REGEX,
Expand Down Expand Up @@ -68,6 +69,7 @@ def _get_default_check_2_validator_map() -> Dict[str, BaseValidator]:
ListOfValidator,
ListValidator,
NoneValidator,
NumberValidator,
OneOfValidator,
RegexValidator,
SpecValidator,
Expand All @@ -79,6 +81,7 @@ def _get_default_check_2_validator_map() -> Dict[str, BaseValidator]:
return {
INT: IntValidator(),
FLOAT: FloatValidator(),
NUMBER: NumberValidator(),
STR: StrValidator(),
DIGIT_STR: DigitStrValidator(),
BOOL: BoolValidator(),
Expand Down
1 change: 1 addition & 0 deletions data_spec_validator/spec/defines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
NONE = 'none'
INT = 'int'
FLOAT = 'float'
NUMBER = 'number'
DIGIT_STR = 'digit_str' # URL params cannot distinguish from strings and numbers
STR = 'str'
BOOL = 'bool'
Expand Down
11 changes: 11 additions & 0 deletions data_spec_validator/spec/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
LIST,
LIST_OF,
NONE,
NUMBER,
ONE_OF,
REGEX,
SPEC,
Expand Down Expand Up @@ -216,6 +217,16 @@ def validate(value, extra, data):
return ok, info


class NumberValidator(BaseValidator):
name = NUMBER

@staticmethod
def validate(value, extra, data):
ok = type(value) is float or type(value) is int
info = '' if ok else TypeError(f'{repr(value)} is not a number')
return ok, info


class StrValidator(BaseValidator):
name = STR

Expand Down
14 changes: 14 additions & 0 deletions test/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
LIST,
LIST_OF,
NONE,
NUMBER,
ONE_OF,
REGEX,
SELF,
Expand Down Expand Up @@ -67,6 +68,19 @@ class FloatSpec:
nok_data = dict(float_field=3)
assert is_something_error(TypeError, validate_data_spec, nok_data, FloatSpec)

def test_number(self):
class NumberSpec:
number_field = Checker([NUMBER])

ok_data = dict(number_field=3.0)
assert validate_data_spec(ok_data, NumberSpec)

ok_data = dict(number_field=3)
assert validate_data_spec(ok_data, NumberSpec)

nok_data = dict(number_field='3.0')
assert is_something_error(TypeError, validate_data_spec, nok_data, NumberSpec)

def test_str(self):
class StrSpec:
str_field = Checker([STR])
Expand Down
Loading