Skip to content

Commit

Permalink
Merge pull request #63 from guyskk/feat-validator-nstr
Browse files Browse the repository at this point in the history
add nstr validator
  • Loading branch information
guyskk authored Dec 13, 2023
2 parents 85af14f + 8940760 commit bad13ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/validr/_validator_c.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,7 @@ def float_validator(compiler, min=-sys.float_info.max, max=sys.float_info.max,
return validate


@validator(accept=(str, object), output=str)
def str_validator(compiler, int minlen=0, int maxlen=1024 * 1024,
def _str_validator(compiler, int minlen=0, int maxlen=1024 * 1024,
bint strip=False, bint escape=False, str match=None,
bint accept_object=False):
"""Validate string
Expand Down Expand Up @@ -904,6 +903,10 @@ def str_validator(compiler, int minlen=0, int maxlen=1024 * 1024,
return validate


str_validator = validator(accept=(str, object), output=str)(_str_validator)
nstr_validator = validator(accept=object, output=object)(_str_validator)


@validator(accept=bytes, output=bytes)
def bytes_validator(compiler, int minlen=0, int maxlen=-1):
"""Validate bytes
Expand Down Expand Up @@ -1225,6 +1228,7 @@ builtin_validators = {
'bool': bool_validator,
'float': float_validator,
'str': str_validator,
'nstr': nstr_validator,
'bytes': bytes_validator,
'date': date_validator,
'time': time_validator,
Expand Down
28 changes: 28 additions & 0 deletions tests/validators/test_nstr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from validr import T

from . import case


@case({
T.nstr: [
('中文', '中文'),
('123', '123'),
(0, '0'),
('', ''),
[None, b'', b'abc', '中文'.encode('utf-8')]
],
T.nstr.default('中文'): [
(None, '中文'),
('', ''),
('abc', 'abc')
],
T.nstr.optional: [
('中文', '中文'),
(0, '0'),
('', ''),
(None, None),
[b'', b'abc']
],
})
def test_nstr():
pass

0 comments on commit bad13ca

Please sign in to comment.