Skip to content

Commit 76b4e2a

Browse files
authored
Merge pull request #106 from p1c2u/feature/readers-and-readme-update
Readers and README update
2 parents 6133d94 + 4e93f2b commit 76b4e2a

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

README.rst

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Installation
3333

3434
$ pip install openapi-spec-validator
3535

36+
Alternatively you can download the code and install from the repository:
37+
38+
.. code-block:: bash
39+
40+
$ pip install -e git+https://github.com/p1c2u/openapi-spec-validator.git#egg=openapi_spec_validator
41+
42+
3643
Usage
3744
#####
3845

@@ -43,25 +50,25 @@ Straight forward way:
4350

4451
.. code:: bash
4552
46-
$ openapi-spec-validator some.yaml
53+
$ openapi-spec-validator openapi.yaml
4754
4855
pipes way:
4956

5057
.. code:: bash
5158
52-
$ cat some.yaml | openapi-spec-validator -
59+
$ cat openapi.yaml | openapi-spec-validator -
5360
5461
docker way:
5562

5663
.. code:: bash
5764
58-
$ docker run -v path/to/some.yaml:/some.yaml --rm p1c2u/openapi-spec-validator /some.yaml
65+
$ docker run -v path/to/openapi.yaml:/openapi.yaml --rm p1c2u/openapi-spec-validator /openapi.yaml
5966
6067
or more pythonic way:
6168

6269
.. code:: bash
6370
64-
$ python -m openapi_spec_validator some.yaml
71+
$ python -m openapi_spec_validator openapi.yaml
6572
6673
Examples
6774
********
@@ -70,34 +77,39 @@ Validate spec:
7077

7178
.. code:: python
7279
73-
7480
from openapi_spec_validator import validate_spec
81+
from openapi_spec_validator.readers import read_from_filename
82+
83+
spec_dict, spec_url = read_from_filename('openapi.yaml')
7584
85+
# If no exception is raised by validate_spec(), the spec is valid.
7686
validate_spec(spec_dict)
7787
78-
Add ``spec_url`` to validate spec with relative files:
88+
validate_spec({})
7989
80-
.. code:: python
90+
Traceback (most recent call last):
91+
...
92+
OpenAPIValidationError: 'openapi' is a required property
8193
94+
Add ``spec_url`` to validate spec with relative files:
8295

83-
from openapi_spec_validator import validate_spec
96+
.. code:: python
8497
8598
validate_spec(spec_dict, spec_url='file:///path/to/spec/openapi.yaml')
8699
87100
You can also validate spec from url:
88101

89102
.. code:: python
90103
91-
92104
from openapi_spec_validator import validate_spec_url
93105
106+
# If no exception is raised by validate_spec_url(), the spec is valid.
94107
validate_spec_url('http://example.com/openapi.json')
95108
96109
If you want to iterate through validation errors:
97110

98111
.. code:: python
99112
100-
101113
from openapi_spec_validator import openapi_v3_spec_validator
102114
103115
errors_iterator = openapi_v3_spec_validator.iter_errors(spec)

openapi_spec_validator/__main__.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import logging
22
import argparse
3-
import os
4-
try:
5-
import pathlib
6-
except ImportError:
7-
import pathlib2 as pathlib
83
import sys
94

105
from openapi_spec_validator import (
11-
openapi_v2_spec_validator, openapi_v3_spec_validator, all_urls_handler,
12-
file_object_handler,
6+
openapi_v2_spec_validator, openapi_v3_spec_validator,
137
)
148
from openapi_spec_validator.exceptions import ValidationError
9+
from openapi_spec_validator.readers import read_from_stdin, read_from_filename
1510

1611
logger = logging.getLogger(__name__)
1712
logging.basicConfig(
@@ -20,19 +15,6 @@
2015
)
2116

2217

23-
def read_from_stdin(filename):
24-
return file_object_handler(sys.stdin), ''
25-
26-
27-
def read_from_filename(filename):
28-
if not os.path.isfile(filename):
29-
raise SystemError("No such file {0}".format(filename))
30-
31-
filename = os.path.abspath(filename)
32-
uri = pathlib.Path(filename).as_uri()
33-
return all_urls_handler(uri), uri
34-
35-
3618
def main(args=None):
3719
parser = argparse.ArgumentParser()
3820
parser.add_argument('filename', help="Absolute or relative path to file")

openapi_spec_validator/readers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
try:
3+
import pathlib
4+
except ImportError:
5+
import pathlib2 as pathlib
6+
import sys
7+
8+
from openapi_spec_validator import all_urls_handler, file_object_handler
9+
10+
11+
def read_from_stdin(filename):
12+
return file_object_handler(sys.stdin), ''
13+
14+
15+
def read_from_filename(filename):
16+
if not os.path.isfile(filename):
17+
raise IOError("No such file: {0}".format(filename))
18+
19+
filename = os.path.abspath(filename)
20+
uri = pathlib.Path(filename).as_uri()
21+
return all_urls_handler(uri), uri

0 commit comments

Comments
 (0)