def validate_yaml_schema(data, schema): """Check that the data respects the schema Will raise `SchemaError` if the schema is not respected. """ schema = Rx.make_schema(schema) if isinstance(data, DictOfDict): data = dict(data) try: schema.validate(data) except Rx.SchemaMismatch as exception: raise errors.SchemaError(rx_exception=exception)
def validate_parser_argument(raw_schema): """Check that the parser argument respects the schema Will raise `ContentError` if the schema is not respected. """ schema = Rx.make_schema(yaml.load(raw_schema)) def wrap(parse): """Wrap the parse function""" def wrapped(keyword, argument, config): """Check the argument schema before calling the plugin parser""" try: schema.validate(argument) except Rx.SchemaMismatch as exception: msg = 'Invalid syntax:\n---\n{}---\n{}'.format( yaml.dump({keyword: argument}, default_flow_style=False), str(exception) ) raise ContentError(keyword, msg) return parse(keyword, argument=argument, config=config) return wrapped return wrap