Python trafaret.String() Examples
The following are 30
code examples of trafaret.String().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
trafaret
, or try the search function
.
Example #1
Source File: mongo_utils.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def text_filter(query, value, schema): string_columns = [s.name for s in schema.keys if isinstance(s.trafaret, t.String)] query_list = [] for column_name in string_columns: query_list.append(op(defaultdict(lambda: {}), column_name, "like", value)) query["$or"] = query_list return query # TODO: use functional style to create query # do not modify dict inside functions, modify dict on # same level
Example #2
Source File: ql.py From flask-graphql-example with MIT License | 6 votes |
def mutate(cls, _, info, __): logger.debug("agrs %s", info) post_schema = t.Dict({ 'title': t.String(min_length=2), 'user_id': t.String(min_length=2), 'content': t.String(min_length=2), t.Key('tags', optional=True): t.List(t.String, min_length=1), }) post_data = post_schema.check(info) user_id = post_data.pop('user_id') user = User.objects.get_or_404(id=user_id) post = Post(author=user, **post_data) post.save() return cls(post=construct(PostField, post))
Example #3
Source File: layout_utils.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def build_field(key, value, relations=None): extra = None name = key if isinstance(value, t.ToInt): v = "number" elif isinstance(value, (t.String, t.URL)): v = "string" elif isinstance(value, t.Email): v = "email" elif isinstance(value, t.ToFloat): v = "float" elif isinstance(value, t.Enum): v = "choice" elif isinstance(value, (t.Dict, t.List)): v = "json" elif isinstance(value, (t.Bool, t.StrBool)): v = "boolean" elif isinstance(value, DateTime): v = "datetime" else: v = "string" return name, v, extra
Example #4
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_list(self): res = extract_error(t.List(t.ToInt), 1) assert res == 'value is not a list' res = t.List(t.ToInt).check([1, 2, 3]) assert res == [1, 2, 3] res = t.List(t.String).check([u"foo", u"bar", u"spam"]) assert res == [u'foo', u'bar', u'spam'] res = extract_error(t.List(t.ToInt), [1, 2, 1 + 3j]) assert res == {2: 'value is not int'} res = t.List(t.ToInt, min_length=1).check([1, 2, 3]) assert res == [1, 2, 3] res = extract_error(t.List(t.ToInt, min_length=1), []) assert res == 'list length is less than 1' res = t.List(t.ToInt, max_length=2).check([1, 2]) assert res == [1, 2] res = extract_error(t.List(t.ToInt, max_length=2), [1, 2, 3]) assert res == 'list length is greater than 2' res = extract_error(t.List(t.ToInt), ["a"]) assert res == {0: "value can't be converted to int"}
Example #5
Source File: test_async.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_mapping(): trafaret = t.Mapping(t.String, t.ToInt & check_int) res = await (trafaret.async_check({'a': '5'})) assert res == {'a': 5} # bad key with pytest.raises(t.DataError) as res: await (trafaret.async_check({None: '5'})) assert res.value.as_dict() == {None: {"key": "value is not a string"}} # bad value with pytest.raises(t.DataError) as res: await (trafaret.async_check({'b': 'qwe'})) assert res.value.as_dict() == {'b': {"value": "value can't be converted to int"}} # is not a dict with pytest.raises(t.DataError) as res: await (trafaret.async_check(None)) assert res.value.as_dict() == "value is not a dict"
Example #6
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_base(self): trafaret = t.Dict(foo=t.ToInt, bar=t.String) trafaret.check({"foo": 1, "bar": u"spam"}) res = t.extract_error(trafaret, {"foo": 1, "bar": 2}) assert res == {'bar': 'value is not a string'} res = extract_error(trafaret, {"foo": 1}) assert res == {'bar': 'is required'} res = extract_error(trafaret, {"foo": 1, "bar": u"spam", "eggs": None}) assert res == {'eggs': 'eggs is not allowed key'} trafaret = trafaret.allow_extra("eggs") trafaret.check({"foo": 1, "bar": u"spam", "eggs": None}) trafaret.check({"foo": 1, "bar": u"spam"}) res = extract_error(trafaret, {"foo": 1, "bar": u"spam", "ham": 100}) assert res == {'ham': 'ham is not allowed key'} trafaret = trafaret.allow_extra("*") trafaret = trafaret.ignore_extra("a") trafaret.check({"foo": 1, "bar": u"spam", "ham": 100}) trafaret.check({"foo": 1, "bar": u"spam", "ham": 100, "baz": None}) res = extract_error(trafaret, {"foo": 1, "ham": 100, "baz": None}) assert res == {'bar': 'is required'}
Example #7
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_xor_key(self): trafaret = t.Dict(xor_key('name', 'nick', t.String())) res = trafaret({'name': u'Nickolay'}) assert res == {'name': u'Nickolay'} res = catch_error(trafaret, {'name': u'Nickolay', 'nick': u'Sveta'}) assert res.as_dict() == { 'name': 'correct only if nick is not defined', 'nick': 'correct only if name is not defined', } res = catch_error(trafaret, {}) assert res.as_dict() == { 'name': 'is required if nick is not defined', 'nick': 'is required if name is not defined', }
Example #8
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_string(self): res = t.String().check(u"foo") assert res == u'foo' res = extract_error(t.String(), u"") assert res == 'blank value is not allowed' res = t.String(allow_blank=True).check(u"") assert res == u'' res = extract_error(t.String(), 1) assert res == 'value is not a string' res = t.String(min_length=2, max_length=3).check(u'123') assert res == u'123' res = extract_error(t.String(min_length=2, max_length=6), u'1') assert res == 'String is shorter than 2 characters' res = extract_error(t.String(min_length=2, max_length=6), u'1234567') assert res == 'String is longer than 6 characters' with pytest.raises(AssertionError) as exc_info: t.String(min_length=2, max_length=6, allow_blank=True) assert exc_info.value.args[0] == 'Either allow_blank or min_length should be specified, not both' res = t.String(min_length=0, max_length=6, allow_blank=True).check(u'123') assert res == u'123'
Example #9
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_base3(self): trafaret = t.Dict({t.Key('bar', default=u'nyanya') >> 'baz': t.String}, foo=t.ToInt) res = trafaret.check({'foo': 4}) assert res == {'baz': u'nyanya', 'foo': 4} trafaret = trafaret.allow_extra('*') res = extract_error(trafaret, {'baz': u'spam', 'foo': 4}) assert res == {'baz': 'baz key was shadowed'} trafaret = trafaret.allow_extra('*', trafaret=t.String) res = extract_error(trafaret, {'baaz': 5, 'foo': 4}) assert res == {'baaz': 'value is not a string'} res = trafaret({'baaz': u'strstr', 'foo':4}) assert res == {'baaz': u'strstr', 'foo':4, 'baz': u'nyanya'} trafaret = trafaret.ignore_extra('fooz') res = trafaret.check({'foo': 4, 'fooz': 5}) assert res == {'baz': u'nyanya', 'foo': 4} trafaret = trafaret.ignore_extra('*') res = trafaret.check({'foo': 4, 'foor': 5}) assert res == {'baz': u'nyanya', 'foo': 4}
Example #10
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_repr(self): trafaret = t.Mapping(t.String, t.Int) assert repr(trafaret) == '<Mapping(<String> => <Int>)>'
Example #11
Source File: test_trafaret.py From pydantic with MIT License | 5 votes |
def __init__(self, allow_extra): self.schema = t.Dict({ 'id': t.Int(), 'client_name': t.String(max_length=255), 'sort_index': t.Float, # t.Key('client_email', optional=True): t.Or(t.Null | t.Email()), t.Key('client_phone', optional=True): t.Or(t.Null | t.String(max_length=255)), t.Key('location', optional=True): t.Or(t.Null | t.Dict({ 'latitude': t.Or(t.Float | t.Null), 'longitude': t.Or(t.Float | t.Null), })), t.Key('contractor', optional=True): t.Or(t.Null | t.Int(gt=0)), t.Key('upstream_http_referrer', optional=True): t.Or(t.Null | t.String(max_length=1023)), t.Key('grecaptcha_response'): t.String(min_length=20, max_length=1000), t.Key('last_updated', optional=True): t.Or(t.Null | t.String >> parse), t.Key('skills', default=[]): t.List(t.Dict({ 'subject': t.String, 'subject_id': t.Int, 'category': t.String, 'qual_level': t.String, 'qual_level_id': t.Int, t.Key('qual_level_ranking', default=0): t.Float, })), }) if allow_extra: self.schema.allow_extra('*')
Example #12
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_or(self): null_string = t.Or(t.String, t.Null) res = null_string.check(None) assert res is None res = null_string.check(u"test") assert res == u'test' res = extract_error(null_string, 1) assert res == {0: 'value is not a string', 1: 'value should be None'}
Example #13
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_operator(self): check = t.String | t.ToInt assert check(u'a') == u'a' assert check(5) == 5
Example #14
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_repr(self): res = t.String() assert repr(res) == '<String>' res = t.String(allow_blank=True) assert repr(res) == '<String(blank)>'
Example #15
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_repr(self): res = t.ToInt | t.String assert repr(res) == '<Or(<ToInt>, <String>)>' res = t.ToInt | t.String | t.Null assert repr(res) == '<Or(<Or(<ToInt>, <String>)>, <Null>)>'
Example #16
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_tuple(self): tup = t.Tuple(t.ToInt, t.ToInt, t.String) res = tup.check([3, 4, u'5']) assert res == (3, 4, u'5') res = extract_error(tup, [3, 4, 5]) assert res == {2: 'value is not a string'} res = extract_error(tup, 5) assert res == 'value must be convertable to tuple' res = extract_error(tup, [5]) assert res == 'value must contain 3 items'
Example #17
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_repr(self): tup = t.Tuple(t.ToInt, t.ToInt, t.String) assert repr(tup) == '<Tuple(<ToInt>, <ToInt>, <String>)>'
Example #18
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_keys_subset(self): cmp_pwds = lambda x: {'pwd': x['pwd'] if x.get('pwd') == x.get('pwd1') else t.DataError('Not equal', code='not_equal')} d = t.Dict({KeysSubset('pwd', 'pwd1'): cmp_pwds, 'key1': t.String}) res = d.check({'pwd': u'a', 'pwd1': u'a', 'key1': u'b'}).keys() assert list(sorted(res)) == [u'key1', u'pwd'] res = extract_error(d.check, {'pwd': u'a', 'pwd1': u'c', 'key1': u'b'}) assert res == {'pwd': 'Not equal'} res = extract_error(d.check, {'pwd': u'a', 'pwd1': None, 'key1': u'b'}) assert res == {'pwd': 'Not equal'} get_values = (lambda d, keys: [d[k] for k in keys if k in d]) join = (lambda d: {'name': u' '.join(get_values(d, ['name', 'last']))}) res = t.Dict({KeysSubset('name', 'last'): join}).check({'name': u'Adam', 'last': u'Smith'}) assert res == {'name': u'Adam Smith'} bad_res = lambda d: t.DataError({'error key': t.DataError(u'bad res', code='bad_res')}, code='bad_res') trafaret = t.Dict({KeysSubset('name', 'last'): bad_res}) res = extract_error(trafaret, {'name': u'Adam', 'last': u'Smith'}) res = {'error key': 'bad res'} bad_err = lambda d: t.DataError({'error_key': 'lala'}) trafaret = t.Dict({KeysSubset('name', 'last'): bad_err}) with pytest.raises(RuntimeError) as exc_info: trafaret({'name': u'Adam', 'last': u'Smith'}) assert exc_info.value.args[0] == 'Please use DataError instance'
Example #19
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_subdict_sample(self): def check_passwords_equal(data): if data['password'] != data['password_confirm']: return t.DataError('Passwords are not equal', code='are_no_equal') return data['password'] check_password = t.String() passwords_key = subdict( 'password', t.Key('password', trafaret=check_password), t.Key('password_confirm', trafaret=check_password), trafaret=check_passwords_equal, ) signup_trafaret = t.Dict( t.Key('email', trafaret=t.Email), passwords_key, ) res = signup_trafaret({'email': u'me@gmail.com', 'password': u'qwerty', 'password_confirm': u'qwerty'}) assert res == {'email': u'me@gmail.com', 'password': u'qwerty'} res = catch_error(signup_trafaret, {'email': u'me@gmail.com', 'password': u'qwerty', 'password_confirm': u'not qwerty'}) assert res.as_dict() == {'password': 'Passwords are not equal'} res = catch_error(signup_trafaret, {'email': u'me@gmail.com', 'password': u'qwerty'}) assert res.as_dict() == {'password_confirm': 'is required'}
Example #20
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_confirm_key(self): trafaret = t.Dict(confirm_key('password', 'password_confirm', t.String())) res = trafaret({'password': u'qwerty', 'password_confirm': u'qwerty'}) assert res == {'password': u'qwerty', 'password_confirm': u'qwerty'} res = catch_error(trafaret, {'password_confirm': u'qwerty'}) assert res.as_dict() == {'password': 'is required'} res = catch_error(trafaret, {'password': u'qwerty'}) assert res.as_dict() == {'password_confirm': 'is required'} res = catch_error(trafaret, {'password': u'qwerty', 'password_confirm': u'not qwerty'}) assert res.as_dict() == {'password_confirm': 'must be equal to password'}
Example #21
Source File: test_constructor.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_bad_key(self): # 123 can not be constructed to Key with pytest.raises(ValueError): construct({123: t.String})
Example #22
Source File: ql.py From flask-graphql-example with MIT License | 5 votes |
def mutate(cls, _, info, __): logger.debug("agrs %s", info) user_schema = t.Dict({ 'email': t.String(min_length=2), 'first_name': t.String(min_length=2), 'last_name': t.String(min_length=2), }) user_data = user_schema.check(info) user = User.objects.create(**user_data) user.save() return cls(user=construct(UserField, user))
Example #23
Source File: ql.py From flask-graphql-example with MIT License | 5 votes |
def mutate(cls, _, info, __): logger.debug("agrs %s", info) comment_schema = t.Dict({ 'name': t.String(min_length=2, max_length=30), 'post_id': t.String(min_length=2, max_length=30), 'content': t.String(min_length=2), }) comment_data = comment_schema.check(info) post_id = comment_data.pop('post_id') post = Post.objects.get_or_404(id=post_id) comment = Comment(**comment_data) post.comments.append(comment) post.save() return cls(post=construct(PostField, post), comment=construct(CommentField, comment))
Example #24
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_forward(self): node = t.Forward() res = extract_error(node, 'something') assert res == 'trafaret not set yet' node << t.Dict(name=t.String, children=t.List[node]) assert node.check({"name": u"foo", "children": []}) == {'children': [], 'name': u'foo'} res = extract_error(node, {"name": u"foo", "children": [1]}) assert res == {'children': {0: 'value is not a dict'}} res = node.check({"name": u"foo", "children": [{"name": u"bar", "children": []}]}) assert res == {'children': [{'children': [], 'name': u'bar'}], 'name': u'foo'} with pytest.raises(RuntimeError): # __rshift__ is not overridden node << t.Int()
Example #25
Source File: sa_utils.py From aiohttp_admin with Apache License 2.0 | 5 votes |
def build_trafaret(sa_type, **kwargs): if isinstance(sa_type, sa.sql.sqltypes.Enum): trafaret = t.Enum(*sa_type.enums, **kwargs) # check for Text should be before String elif isinstance(sa_type, sa.sql.sqltypes.Text): trafaret = t.String(**kwargs) elif isinstance(sa_type, sa.sql.sqltypes.String): trafaret = t.String(max_length=sa_type.length, **kwargs) elif isinstance(sa_type, sa.sql.sqltypes.Integer): trafaret = t.ToInt(**kwargs) elif isinstance(sa_type, sa.sql.sqltypes.Float): trafaret = t.ToFloat(**kwargs) elif isinstance(sa_type, sa.sql.sqltypes.DateTime): trafaret = DateTime(**kwargs) # RFC3339 elif isinstance(sa_type, sa.sql.sqltypes.Date): trafaret = DateTime(**kwargs) # RFC3339 elif isinstance(sa_type, sa.sql.sqltypes.Boolean): trafaret = t.ToBool(**kwargs) # Add PG related JSON and ARRAY elif isinstance(sa_type, postgresql.JSON): trafaret = AnyDict | t.List(AnyDict) # Add PG related JSON and ARRAY elif isinstance(sa_type, postgresql.ARRAY): item_trafaret = build_trafaret(sa_type.item_type) trafaret = t.List(item_trafaret) else: type_ = str(sa_type) msg = 'Validator for type {} not implemented'.format(type_) raise NotImplementedError(msg) return trafaret
Example #26
Source File: sa_utils.py From aiohttp_admin with Apache License 2.0 | 5 votes |
def text_filter(query, value, table): pairs = ((n, c) for n, c in table.c.items() if isinstance(c.type, sa.sql.sqltypes.String)) sub_queries = [] for name, column in pairs: do_compare = op("like", column) sub_queries.append(do_compare(column, value)) query = query.where(or_(*sub_queries)) return query # TODO: validate that value supplied in filter has same type as in table # TODO: use functional style to create query
Example #27
Source File: db_fixtures.py From aiohttp_admin with Apache License 2.0 | 5 votes |
def sa_table(): choices = ['a', 'b', 'c'] meta = sa.MetaData() post = sa.Table( 'test_post', meta, sa.Column('id', sa.Integer, nullable=False), sa.Column('title', sa.String(200), nullable=False), sa.Column('category', sa.String(200), nullable=True), sa.Column('body', sa.Text, nullable=False), sa.Column('views', sa.Integer, nullable=False), sa.Column('average_note', sa.Float, nullable=False), # sa.Column('pictures', postgresql.JSON, server_default='{}'), sa.Column('published_at', sa.DateTime, nullable=False), # sa.Column('tags', postgresql.ARRAY(sa.Integer), server_default='{}'), sa.Column('status', sa.Enum(*choices, name="enum_name", native_enum=False), server_default="a", nullable=False), sa.Column('visible', sa.Boolean, nullable=False), # Indexes # sa.PrimaryKeyConstraint('id', name='post_id_pkey')) return post
Example #28
Source File: test_async.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_dict_extra_and_ignore(): trafaret = t.Dict( t.Key('a', to_name='A', trafaret=t.String), allow_extra=['one_extra'], allow_extra_trafaret=t.String, ignore_extra=['one_ignore'], ) res = await (trafaret.async_check({'a': 's', 'one_extra': 's', 'one_ignore': 's'})) assert res == {'A': 's', 'one_extra': 's'} # extra key that is not declared in extra not in ignore with pytest.raises(t.DataError) as res: await trafaret.async_check({'a': 's', 'bad_extra': 's'}) assert res.value.as_dict() == {'bad_extra': 'bad_extra is not allowed key'} # extra key that was shadowed with pytest.raises(t.DataError) as res: await trafaret.async_check({'a': 's', 'A': 's'}) assert res.value.as_dict() == {'A': 'A key was shadowed'} # extra key with failed check with pytest.raises(t.DataError) as res: await trafaret.async_check({'a': 's', 'one_extra': 5}) assert res.value.as_dict() == {'one_extra': 'value is not a string'} # shadowing with allow_extra='*' trafaret = trafaret.allow_extra('*') with pytest.raises(t.DataError) as res: await trafaret.async_check({'a': 's', 'A': 's'}) assert res.value.as_dict() == {'A': 'A key was shadowed'}
Example #29
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_repr(self): trafaret = t.Dict(foo=t.ToInt, bar=t.String, allow_extra=['eggs']) assert repr(trafaret) == '<Dict(extras=(eggs) | <Key "bar" <String>>, <Key "foo" <ToInt>>)>' trafaret = trafaret.allow_extra('*') assert repr(trafaret) == '<Dict(any, extras=(eggs) | <Key "bar" <String>>, <Key "foo" <ToInt>>)>' trafaret = trafaret.ignore_extra("a") assert repr(trafaret) == '<Dict(any, ignore=(a), extras=(eggs) | <Key "bar" <String>>, <Key "foo" <ToInt>>)>'
Example #30
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_add_kwargs_ignore_any(self): first = t.Dict( t.Key('bip', trafaret=t.String()), ignore_extra='*' ) second = t.Dict( t.Key('bop', trafaret=t.Int()) ) third = first + second third.check({'bip': u'bam', 'bop': 17, 'matter': False}) assert third.ignore_any