def test_dict_forbidden_keys(): with raises(SchemaForbiddenKeyError): Schema({Forbidden("b"): object}).validate({"b": "bye"}) with raises(SchemaWrongKeyError): Schema({Forbidden("b"): int}).validate({"b": "bye"}) assert Schema({Forbidden("b"): int, Optional("b"): object}).validate({"b": "bye"}) == {"b": "bye"} with raises(SchemaForbiddenKeyError): Schema({Forbidden("b"): object, Optional("b"): object}).validate({"b": "bye"})
def test_dict_forbidden_keys(): with raises(SchemaForbiddenKeyError): Schema({Forbidden('b'): object}).validate({'b': 'bye'}) with raises(SchemaWrongKeyError): Schema({Forbidden('b'): int}).validate({'b': 'bye'}) assert (Schema({Forbidden('b'): int, Optional('b'): object}).validate({'b': 'bye'}) == {'b': 'bye'}) with raises(SchemaForbiddenKeyError): Schema({Forbidden('b'): object, Optional('b'): object}).validate({'b': 'bye'})
def test_json_schema_dict_forbidden(): s = Schema({ Forbidden('test'): 1, Forbidden('test'): 2, Optional('test'): 3, Optional('test'): 4, }) assert s.json_schema() == { 'type': 'object', 'properties': { 'test': { 'allOf': [{'enum': [3, 4]}], 'not': {'enum': [1, 2]} } }, 'additionalProperties': False, }
def test_json_schema_forbidden_key_ignored(): s = Schema({Forbidden("forbidden"): str, "test": str}) assert s.json_schema("my-id") == { "$schema": "http://json-schema.org/draft-07/schema#", "id": "my-id", "properties": {"test": {"type": "string"}}, "required": ["test"], "additionalProperties": False, "type": "object", }
def test_hook_handle(): class Hook1(Hook): priority = 0 def handle(self, *kargs): return None s = Schema({ Hook1('test'): 'test', Optional('test'): str, }) assert s.validate({'test': 'test'}) == {'test': 'test'} assert s.validate({'test': 'other'}) == {'test': 'other'} s = Schema({ Hook1('test'): 'test', Forbidden('test'): 'test', 'test': str, }) with SE: s.validate({'test', 'test'}) assert s.validate({'test': 'other'}) == {'test': 'other'} class Hook2(Hook): priority = 0 def handle(self, *kargs): return False s = Schema({ Hook2('test'): 'test', Forbidden('test'): 'test', }) assert s.validate({'test': 'test'}) == {} class Hook3(Hook): priority = 0 def handle(self, *kargs): return True s = Schema({ Hook3('test'): 'test', Forbidden('test'): 'test', }) assert s.validate({'test': 'test'}) == {'test': 'test'}
class AppConfig(Base): """ Globally available singleton-accessible application configuration. """ _filename = "app" _schema = Schema( { # configuration entries for plugins "plugin": Schema({ Optional("source_dirs", default=list): list[str], }, name="Plugin Config", ignore_extra_keys=True), # configuration entries for database connection "database": Schema({ "connection_string": str, Optional(object): object, }, name="Database Config", ignore_extra_keys=True), # configuration entries for Tor proxy "control_server": Schema( { Optional("url_public", default="http://127.0.0.1"): str, Optional("access_log_format", default='%a "%r" %s %b %Tf "%{Referer}i" "%{User-Agent}i"'): str, Optional("api"): Schema({ Optional("url_prefix"): str, }, name="Control Server - API Config"), Optional("docs"): Schema( { Forbidden("title"): str, Forbidden("version"): str, Forbidden("securityDefinitions"): str, "swagger_path": str, Optional("static_path"): str, Optional("url"): str, Optional(object): object, }, name="Control Server - Swagger Docs Config", ignore_extra_keys=True), Optional("auth"): Schema( { "secret_or_pub_key": str, Optional("algorithms", default=["HS512", "HS384", "HS256"]): list, Optional("whitelist", default=[r"/public/*"]): list, Optional("issuer", default="TorScraper"): str, Optional("audience"): str, Optional(object): object, }, name="Control Server - Swagger Docs Config", ignore_extra_keys=True), "site": Schema( { "host": str, "port": int, Optional(object): object, }, name="Control Server - Site Config", ignore_extra_keys=True) }, name="Control Server Config"), # configuration entries for Tor proxy "proxy": Schema( { "use_proxy": bool, Optional("start_process", default=True): bool, Optional("connect_control", default=True): bool, Optional("host", default="127.0.0.1"): str, Optional("port_proxy", default=9050): int, Optional("port_control", default=9051): int, Optional("protocol", default="socks5"): str, Optional("password"): str, }, name="Proxy Config"), # configuration entries for Tor proxy "redis": Schema( { "address": str, Optional("db"): int, Optional("password"): str, Optional("encoding"): str, Optional("timeout"): int, Optional("minsize"): str, Optional("maxsize"): str, Optional(object): object, }, name="Redis Config", ignore_extra_keys=True), }, name="Torscraper App Configuration")
""" The validation schema for the models """ from schema import And, Forbidden, Regex, Schema, Optional USER_SCHEMA_VALIDATOR = Schema({ Forbidden("guid"): And(str, len), Forbidden("created_at"): And(str, len), Forbidden("updated_at"): And(str, len), Optional("first_name"): And(str, len), Optional("last_name"): And(str, len), Optional("email"): Regex(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"), "phone": Regex(r"[6789]{1}[0-9]{9}"), Optional("addresses"): list, "authentication": object, }) USER_UPDATE_VALIDATOR = Schema({ Optional("guid"): And(str, len), Forbidden("created_at"): And(str, len),