Skip to content

plugin.api.validate: refactor, turn into package #4514

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

Merged
merged 9 commits into from
May 8, 2022
Next Next commit
plugin.api.validate: refactor all + any
Turn into SchemaContainer subclasses
  • Loading branch information
bastimeyer committed May 7, 2022
commit 57289f47740949f81cf584ff7e80537a2b2d2f90
34 changes: 20 additions & 14 deletions src/streamlink/plugin/api/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,27 @@ def _is_re_match(value):
return _all(_hasattr(value, a) for a in _re_match_attr)


class any(tuple):
"""At least one of the schemas must be valid."""
def __new__(cls, *args):
return super().__new__(cls, args)
class SchemaContainer:
def __init__(self, schema):
self.schema = schema


class all(tuple):
"""All schemas must be valid."""
def __new__(cls, *args):
return super().__new__(cls, args)
class any(SchemaContainer):
"""
Collection of schemas where at least one schema must be valid.
"""

def __init__(self, *schemas):
super().__init__(schemas)

class SchemaContainer:
def __init__(self, schema):
self.schema = schema

class all(SchemaContainer):
"""
Collection of schemas where every schema must be valid.
"""

def __init__(self, *schemas):
super().__init__(schemas)


class transform:
Expand Down Expand Up @@ -364,7 +370,7 @@ def validate(schema, value):
@validate.register(any)
def validate_any(schema, value):
errors = []
for subschema in schema:
for subschema in schema.schema:
try:
return validate(subschema, value)
except ValueError as err:
Expand All @@ -375,8 +381,8 @@ def validate_any(schema, value):


@validate.register(all)
def validate_all(schemas, value):
for schema in schemas:
def validate_all(schema, value):
for schema in schema.schema:
value = validate(schema, value)

return value
Expand Down