Esempio n. 1
0
def validate(path, format, encoding, verbose):
    options = {
        'schema': {
            'path': path,
            'format': format,
            'encoding': encoding
        }
    }
    config = _load_config(options, verbose=verbose)

    path = config['schema']['path']
    format = config['schema']['format']
    encoding = config['schema']['encoding']

    with open(path, 'rb') as schema_file:
        content = schema_file.read()

    try:
        apistar.validate(content, format=format, encoding=encoding)
    except (ParseError, ValidationError) as exc:
        _echo_error(exc, content, verbose=verbose)
        sys.exit(1)

    success_summary = {
        'json': 'Valid JSON',
        'yaml': 'Valid YAML',
        'config': 'Valid APIStar config.',
        'jsonschema': 'Valid JSONSchema document.',
        'openapi': 'Valid OpenAPI schema.',
        'swagger': 'Valid Swagger schema.',
    }[format]
    click.echo(click.style('✓ ', fg='green') + success_summary)
Esempio n. 2
0
def test_openapi(filename):
    with open(filename, 'rb') as input_file:
        content = input_file.read()

    path, extension = os.path.splitext(filename)
    encoding = {".json": "json", ".yaml": "yaml"}[extension]
    apistar.validate(content, format='openapi', encoding=encoding)
Esempio n. 3
0
def _load_config(options, verbose=False):
    if not os.path.exists("apistar.yml"):
        # If the config file is not used, then --path is required.
        if options["schema"]["path"] is None:
            raise click.UsageError('Missing option "--path".')
        config = options

    else:
        with open("apistar.yml", "rb") as config_file:
            content = config_file.read()

        try:
            config = apistar.validate(content, format="config", encoding="yaml")
        except (ParseError, ValidationError) as exc:
            click.echo('Errors in configuration file "apistar.yml":')
            _echo_error(exc, content, verbose=verbose)
            sys.exit(1)

        # Anything passed in 'options' should override the config file.
        # Ensure all values are populated with 'None' if unset.
        for section in options.keys():
            config.setdefault(section, {})
            for key, value in options[section].items():
                config[section].setdefault(key, None)
                if value is not None:
                    config[section][key] = value

    path = config["schema"]["path"]
    if not os.path.exists(path):
        raise click.UsageError('Schema file "%s" not found.' % path)

    if config["schema"]["encoding"] is None:
        config["schema"]["encoding"] = _encoding_from_filename(path)

    return config
Esempio n. 4
0
def validate(path, format, encoding, verbose):
    options = {
        "schema": {
            "path": path,
            "format": format,
            "encoding": encoding
        }
    }
    config = _load_config(options, verbose=verbose)

    path = config["schema"]["path"]
    format = config["schema"]["format"]
    encoding = config["schema"]["encoding"]

    with open(path, "rb") as schema_file:
        content = schema_file.read()

    try:
        apistar.validate(content, format=format, encoding=encoding)
    except (typesystem.ParseError, typesystem.ValidationError) as exc:
        if isinstance(exc, typesystem.ParseError):
            summary = {
                "json": "Invalid JSON.",
                "yaml": "Invalid YAML.",
                None: "Parse error.",
            }[encoding]
        else:
            summary = {
                "config": "Invalid APIStar config.",
                "jsonschema": "Invalid JSONSchema document.",
                "openapi": "Invalid OpenAPI schema.",
                "swagger": "Invalid Swagger schema.",
                None: "Invalid schema.",
            }[format]
        _echo_error(exc, content, summary=summary, verbose=verbose)
        sys.exit(1)

    success_summary = {
        "json": "Valid JSON",
        "yaml": "Valid YAML",
        "config": "Valid APIStar config.",
        "jsonschema": "Valid JSONSchema document.",
        "openapi": "Valid OpenAPI schema.",
        "swagger": "Valid Swagger schema.",
    }[format]
    click.echo(click.style("✓ ", fg="green") + success_summary)
Esempio n. 5
0
def test_openapi(filename):
    with open(filename, "rb") as input_file:
        content = input_file.read()

    path, extension = os.path.splitext(filename)
    encoding = {".json": "json", ".yaml": "yaml"}[extension]
    document = apistar.validate(content, format="openapi", encoding=encoding)
    if document.url is not None:
        for link_info in document.walk_links():
            assert document.url in link_info.link.url
Esempio n. 6
0
 def __init__(self,
              schema,
              format=None,
              encoding=None,
              auth=None,
              decoders=None,
              encoders=None,
              headers=None,
              session=None,
              allow_cookies=True):
     self.document = apistar.validate(schema,
                                      format=format,
                                      encoding=encoding)
     self.transport = self.init_transport(auth, decoders, encoders, headers,
                                          session, allow_cookies)
Esempio n. 7
0
def spec_errors(spec):
    try:
        apistar.validate(spec, format='swagger')
        return []
    except apistar.validators.ValidationError as error:
        return error.args[0]