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
Prev Previous commit
Next Next commit
plugin.api.validate: refactor Schema class
Inherit from the all-schema directly to avoid having an unnecessary
singledispatch type definition.
  • Loading branch information
bastimeyer committed May 7, 2022
commit d1d805755c76d60f4afe3dc5c0abcf2a3026db0a
16 changes: 5 additions & 11 deletions src/streamlink/plugin/api/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,13 @@ def validate_unions(schema, value):
return validate_union(schema.schema, value)


class Schema:
"""Wraps a validator schema into a object."""

def __init__(self, *schemas):
self.schema = all(*schemas)
class Schema(all):
"""
Wrapper class for :class:`all` with a validate method which raises :class:`PluginError` by default on error.
"""

def validate(self, value, name="result", exception=PluginError):
try:
return validate(self.schema, value)
return validate(self, value)
except ValueError as err:
raise exception("Unable to validate {0}: {1}".format(name, err))


@validate.register(Schema)
def validate_schema(schema, value):
return schema.validate(value, exception=ValueError)