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

schema-strict: add more validation keywords #184

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
Changelog
=========

1.0.5 (YYYY-MM-DD)
------------------

Changed
~~~~~~~

- :ref:`schema-strict`: Remove "integer" type from ID fields. Add validation rules:

- "format": "email" if the key is "email"
- "minimum": 0 to "quantity", "durationInDays" and "numberOfTenderers fields
- "required": ["id", "name"] to "Organization" and "OrganizationReference"
- "required": ["id"] to "Amendment" and "RelatedProcess"

1.0.4 (2022-02-10)
------------------

Expand Down
10 changes: 9 additions & 1 deletion docs/cli/schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ Optional arguments:
schema-strict
-------------

Adds "minItems" and "uniqueItems" if an array, "minProperties" if an object and "minLength" if a string and "enum", "format" and "pattern" are not set.
Adds:
* "minItems" and "uniqueItems" if an array, adds "minProperties" if an object
* "minLength" if a string and if "enum", "format" and "pattern" aren't set
* "format": "email" if the key is "email"
* "minimum": 0 to "quantity", "durationInDays" and "numberOfTenderers fields
* "required": ["id", "name"] to "Organization" and "OrganizationReference"
* "required": ["id"] to "Amendment" and "RelatedProcess"

Removes "integer" from "type" for ID fields.

Optional arguments:

Expand Down
29 changes: 26 additions & 3 deletions ocdskit/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,15 @@ def _deprecated(value):

def add_validation_properties(schema, unique_items=True, coordinates=False):
"""
Adds "minItems" and "uniqueItems" if an array, adds "minProperties" if an object, and adds "minLength" if a string
and if "enum", "format" and "pattern" aren't set.
Adds:
* "minItems" and "uniqueItems" if an array, adds "minProperties" if an object
* "minLength" if a string and if "enum", "format" and "pattern" aren't set
* "format": "email" if the key is "email"
* "minimum": 0 to "quantity", "durationInDays" and "numberOfTenderers fields
* "required": ["id", "name"] to "Organization" and "OrganizationReference"
* "required": ["id"] to "Amendment" and "RelatedProcess"

Removes "integer" from "type" for ID fields.

:param dict schema: a JSON schema
:param bool unique_items: whether to add "uniqueItems" properties to array fields
Expand Down Expand Up @@ -234,5 +241,21 @@ def add_validation_properties(schema, unique_items=True, coordinates=False):
if 'object' in schema['type']:
schema.setdefault('minProperties', 1)

for value in schema.values():
for key, value in schema.items():
if key == 'email':
value['format'] = 'email'
elif key in ['quantity', 'durationInDays', 'numberOfTenderers']:
value['minimum'] = 0
elif key in ['Organization', 'OrganizationReference']:
value['required'] = ['id', 'name']
value['properties']['name']['type'] = "string"
value['properties']['id']['type'] = "string"
elif key in ['Amendment', 'RelatedProcess']:
value['required'] = ['id']
value['properties']['id']['type'] = "string"
elif key == 'id':
if 'type' in value:
if 'integer' in value['type']:
value['type'].remove('integer')
Comment on lines +247 to +259
Copy link
Member

Choose a reason for hiding this comment

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

OCDS Kit (despite the name) has tools that are reusable by other JSON Schema standards (like BODS). We should put this logic in the standard repository. Since this leaves only email as key-based logic, we can move it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good - I've done that in open-contracting/standard#1480 so I'll close this PR.


add_validation_properties(value, unique_items=unique_items, coordinates=coordinates)