Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 0b7778b

Browse files
committed
Added update validator property with documentation
1 parent 3c809fd commit 0b7778b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [NEW] Added ``rewrites`` accessor property for URL rewriting.
99
- [NEW] Added ``st_indexes`` accessor property for Cloudant Geospatial indexes.
1010
- [NEW] Added support for DesignDocument ``_info`` and ``_search_info`` endpoints.
11+
- [NEW] Added ``validate_doc_update`` accessor property for update validators.
1112
- [NEW] Added support for a custom ``requests.HTTPAdapter`` to be configured using an optional ``adapter`` arg e.g.
1213
``Cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME, adapter=Replay429Adapter())``.
1314
- [IMPROVED] Made the 429 response code backoff optional and configurable. To enable the backoff add

src/cloudant/design_document.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ def __init__(self, database, document_id=None):
4848
for prop in self._nested_object_names:
4949
self.setdefault(prop, dict())
5050

51+
@property
52+
def validate_doc_update(self):
53+
"""
54+
Provides an accessor property to the update validators dictionary in
55+
the locally cached DesignDocument. Update validators evaluate whether a
56+
document should be written to disk when insertions and updates are attempted.
57+
58+
Update validator example:
59+
60+
.. code-block:: python
61+
62+
# Add the update validator to ``validate_doc_update`` and save the design document
63+
ddoc = DesignDocument(self.db, '_design/ddoc001')
64+
ddoc['validate_doc_update'] = (
65+
'function(newDoc, oldDoc, userCtx, secObj) { '
66+
'if (newDoc.address === undefined) { '
67+
'throw({forbidden: \'Document must have an address.\'}); }}')
68+
ddoc.save()
69+
70+
For more details, see the `Update Validators documentation
71+
<https://docs.cloudant.com/design_documents.html#update-validators>`_.
72+
73+
:returns: Dictionary containing update validator functions
74+
"""
75+
return self.get('validate_doc_update')
76+
5177
@property
5278
def updates(self):
5379
"""

tests/unit/design_document_tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
module docstring.
2222
2323
"""
24+
import json
2425
import os
2526
import unittest
2627

@@ -1658,5 +1659,26 @@ def test_get_show_function(self):
16581659
'html += \'</ol></body></html>\'; return html; }); }'
16591660
)
16601661

1662+
def test_update_validator(self):
1663+
"""
1664+
Test that update validator requires an address key for a new document.
1665+
"""
1666+
ddoc = DesignDocument(self.db, '_design/ddoc001')
1667+
ddoc['validate_doc_update'] = (
1668+
'function(newDoc, oldDoc, userCtx, secObj) { '
1669+
'if (newDoc.address === undefined) { '
1670+
'throw({forbidden: \'Document must have an address.\'}); }}')
1671+
ddoc.save()
1672+
headers = {'Content-Type': 'application/json'}
1673+
resp = self.client.r_session.post(
1674+
self.db.database_url,
1675+
headers=headers,
1676+
data=json.dumps({'_id': 'test001'})
1677+
)
1678+
self.assertEqual(
1679+
resp.json(),
1680+
{'reason': 'Document must have an address.', 'error': 'forbidden'}
1681+
)
1682+
16611683
if __name__ == '__main__':
16621684
unittest.main()

0 commit comments

Comments
 (0)