Skip to content
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

WIP: schemas #66

Merged
merged 29 commits into from
Jan 3, 2018
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3bdb6aa
WIP: schemas
CJ-Wright Dec 12, 2017
4c6ce36
TST: add test for validator
CJ-Wright Dec 13, 2017
4c2e92d
ENH: working schemas with examples
CJ-Wright Dec 13, 2017
5c2dd0f
DOC: add docs
CJ-Wright Dec 13, 2017
c684b86
CI: create not install
CJ-Wright Dec 13, 2017
4a498b3
MTN: fix run and move the schemas out of folder
CJ-Wright Dec 13, 2017
e2ae650
ENH: validator
CJ-Wright Dec 14, 2017
f8f7930
WIP: finalize schema
CJ-Wright Dec 14, 2017
2969e52
STY: pep8
CJ-Wright Dec 17, 2017
e0f26bd
STY: pep8 import
CJ-Wright Dec 17, 2017
5881956
MTN: move schema things to schemas
CJ-Wright Dec 17, 2017
986d5fa
STY: newline
CJ-Wright Dec 17, 2017
4dc419c
FIX: don't allow unknown
CJ-Wright Dec 17, 2017
f7bcfb1
FIX: custom validator for description
CJ-Wright Dec 17, 2017
6068349
WIP: write docs from schema/exemplars
CJ-Wright Dec 18, 2017
263441b
WIP: auto schema doc
CJ-Wright Dec 18, 2017
d0a0ac7
FIX: proper internal dict format
CJ-Wright Dec 18, 2017
c026c1b
DOC: remove auto docs, add to their own auto folder, add auto folder …
CJ-Wright Dec 18, 2017
55d3619
FIX: comments from @scopatz
CJ-Wright Dec 18, 2017
1394df2
ENH: move top docs to schemas module
CJ-Wright Dec 18, 2017
61ffe55
STY: pep8 line width
CJ-Wright Dec 18, 2017
9c3c8d6
STY: pep8 newline
CJ-Wright Dec 18, 2017
79b75a9
ENH: alph. yamls and added top docs to schema
CJ-Wright Dec 18, 2017
6222199
STY: pep8 line width
CJ-Wright Dec 18, 2017
4420cb5
FIX: comments from @scopatz
CJ-Wright Jan 2, 2018
b7d3565
FIX: fix app, move tests
CJ-Wright Jan 2, 2018
1491d88
FIX: move error to raise
CJ-Wright Jan 2, 2018
a990ed9
news
CJ-Wright Jan 2, 2018
5df3883
elaborate on news
CJ-Wright Jan 3, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TST: add test for validator
CJ-Wright committed Dec 13, 2017
commit 4c6ce368ff4221596aac95d24cdba7142a64c5e0
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -20,11 +20,14 @@ install:
- conda update conda

# Install dependencies
- conda install python
- conda install -n test --file requirements/run.txt
- source activate test
- python setup.py install

script:
- set -e
- conda install --file requirements/test.txt
- pytest -vvs .
- # Command to build your docs
- pip install doctr
- conda install --file requirements/doc.txt
3 changes: 2 additions & 1 deletion regolith/schemas/schemas.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,8 @@
'description': 'optional, URL to the call for proposals',
'type': str},
'currency': {'description': "typically '$' or 'USD'", 'type': str},
'end_day': {'description': 'end day of teh grant, optional', 'type': str},
'end_day': {'description': 'end day of teh grant, optional',
'type': (str, int)},
'end_month"': {'description': 'end month of the grant', 'type': str},
'end_year': {'description': 'end year of the grant', 'type': int},
'funder': {'description': 'the agency funding the work', 'type': str},
31 changes: 31 additions & 0 deletions regolith/tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from regolith.validators import validate_schema
from regolith.schemas.schemas import schemas
import json
from io import StringIO


def test_grant_validation():
raw_json = """{"_id": "SymPy-1.1",
"amount": 3000.0,
"begin_day": 1,
"begin_month": "May",
"begin_year": 2017,
"call_for_proposals": "https://groups.google.com/d/msg/numfocus/wPjhdm8NJiA/S8JL1_NZDQAJ",
"end_day": 31,
"end_month": "December",
"end_year": 2017,
"funder": "NumFOCUS",
"narrative": "https://docs.google.com/document/d/1nZxqoL-Ucni_aXLWmXtRDd3IWqW0mZBO65CEvDrsXZM/edit?usp=sharing",
"program": "Small Development Grants",
"team": [{"institution": "University of South Carolina",
"name": "Anthony Scopatz",
"position": "PI"},
{"institution": "University of South Carolina",
"name": "Aaron Meurer",
"position": "researcher"}
],
"title": "SymPy 1.1 Release Support"}"""
record = json.loads(raw_json)
validate_schema(record, schemas['grants'])


33 changes: 13 additions & 20 deletions regolith/validators.py
Original file line number Diff line number Diff line change
@@ -116,36 +116,29 @@ def ensure_email(email):
}


def validate_schema(record, schema):
def validate_schema(record, schema, key=None):
if isinstance(record, dict):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check collections.abc.Mapping, not dict. Same goes for below.

total_keys = set(record.keys())
total_keys.update(set(schema.keys()))
total_keys = set(schema.keys())
remove_keys = ['required', 'type', 'description']
for k in remove_keys:
if k in total_keys:
total_keys.remove(k)
total_keys.update(set(record.keys()))

for k in total_keys:
if k not in schema:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it an error if we get a key that we don't expect?

if k not in record and schema[k].get('required', False):
raise ValueError('{} is required'.format(k))
elif k in record and k in schema:
print(k)
validate_schema(record[k], schema[k])
validate_schema(record[k], schema[k], k)
elif isinstance(record, collections.Iterable) and not isinstance(record,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really include set and generators? Or is this supposed to be collections.abc.Sequence?

str):
for r in record:
validate_schema(r, schema)
validate_schema(r, schema, key)
else:
if not isinstance(record, schema['type']):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an elif, rather than an if inside of an else.

raise ValueError('Schema expected type: {}, '
'got type: {}'.format(type(record),
schema['type']))


dict_schema = {'key':
{'type': str, 'required': True}
}
dict_schema2 = {'key':
{'key1': {'type': str, 'required': True, 'description':
'key one schema'},
'key2': {'type': [int, float], 'required': False},
'key3': [{'type': [int, float], 'required': False}]
}
}
'got type: {} in '
'{{{}:{}}}'.format(type(record),
schema['type'], key, record))
1 change: 1 addition & 0 deletions requirements/run.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
python
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a valid requirement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took inspiration for this from conda forge recipes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is crazy if it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually include python as a run requirement in conda recipes, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is true. I wonder about whether this affects pip, though maybe we don't care 🤷‍♂️

ruamel.yaml
3 changes: 2 additions & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ruamel.yaml
ruamel.yaml
pytest