-
Notifications
You must be signed in to change notification settings - Fork 72
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
WIP: schemas #66
Changes from 1 commit
3bdb6aa
4c6ce36
4c2e92d
5c2dd0f
c684b86
4a498b3
e2ae650
f8f7930
2969e52
e0f26bd
5881956
986d5fa
4dc419c
f7bcfb1
6068349
263441b
d0a0ac7
c026c1b
55d3619
1394df2
61ffe55
9c3c8d6
79b75a9
6222199
4420cb5
b7d3565
1491d88
a990ed9
5df3883
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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']) | ||
|
||
|
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): | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
str): | ||
for r in record: | ||
validate_schema(r, schema) | ||
validate_schema(r, schema, key) | ||
else: | ||
if not isinstance(record, schema['type']): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really a valid requirement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took inspiration for this from conda forge recipes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is crazy if it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually include python as a run requirement in conda recipes, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
ruamel.yaml | ||
ruamel.yaml | ||
pytest |
There was a problem hiding this comment.
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.