Skip to content

Commit 2655257

Browse files
authored
Adding schema validation (#387)
* Waiting for backup restore * Adding schema parameter validation * Updating docs * Updating schema validation * Updating error message * Fixing formatter * Bumping version number
1 parent 91faf70 commit 2655257

7 files changed

Lines changed: 24 additions & 9 deletions

File tree

arango/collection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ def configure(
400400
if sync is not None:
401401
data["waitForSync"] = sync
402402
if schema is not None:
403+
if not isinstance(schema, dict) or len(schema) == 0:
404+
raise ValueError("schema parameter must be a non-empty dict")
403405
data["schema"] = schema
404406
if replication_factor is not None:
405407
data["replicationFactor"] = replication_factor

arango/database.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,7 @@ def create_collection(
16681668
:return: Standard collection API wrapper.
16691669
:rtype: arango.collection.StandardCollection
16701670
:raise arango.exceptions.CollectionCreateError: If create fails.
1671+
:raise ValueError: If parameters are invalid.
16711672
"""
16721673
key_options: Json = {"type": key_generator, "allowUserKeys": user_keys}
16731674
if key_generator == "autoincrement":
@@ -1698,6 +1699,8 @@ def create_collection(
16981699
if write_concern is not None:
16991700
data["writeConcern"] = write_concern
17001701
if schema is not None:
1702+
if not isinstance(schema, dict) or len(schema) == 0:
1703+
raise ValueError("schema parameter must be a non-empty dict")
17011704
data["schema"] = schema
17021705
if computedValues is not None:
17031706
data["computedValues"] = computedValues

arango/formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ def format_backup_dbserver(body: Json) -> Json:
11121112
:return: Formatted body.
11131113
:rtype: dict
11141114
"""
1115-
return {"status": body["Status"]}
1115+
return {"status": body.get("Status")}
11161116

11171117

11181118
def format_backup_transfer(body: Json) -> Json:

arango/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "8.2.6"
1+
__version__ = "8.3.0"

docs/schema.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Schema Validation
33

44
ArangoDB supports document validation using JSON schemas. You can use this
55
feature by providing a schema during collection creation using the ``schema``
6-
parameter.
6+
parameter. It must not be an empty ``dict```.
77

88
**Example:**
99

@@ -37,6 +37,3 @@ parameter.
3737

3838
# Modify the schema.
3939
employees.configure(schema=my_schema)
40-
41-
# Remove the schema.
42-
employees.configure(schema={})

tests/test_backup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
import pytest
24
from packaging import version
35

@@ -110,6 +112,9 @@ def test_backup_management(sys_db, bad_db, cluster, skip_tests, db_version):
110112
result = sys_db.backup.restore(backup_id_foo)
111113
assert isinstance(result, dict)
112114

115+
# Wait for restore to complete
116+
time.sleep(10)
117+
113118
# Test restore backup with bad database.
114119
with assert_raises(BackupRestoreError) as err:
115120
bad_db.backup.restore(backup_id_foo)

tests/test_collection.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ def test_collection_misc_methods(col, bad_col, cluster):
6060
}
6161
]
6262

63-
properties = col.configure(
64-
sync=not prev_sync, schema={}, computed_values=computed_values
65-
)
63+
with pytest.raises(ValueError):
64+
# schema must not be empty
65+
properties = col.configure(
66+
sync=not prev_sync, schema={}, computed_values=computed_values
67+
)
68+
69+
properties = col.configure(sync=not prev_sync, computed_values=computed_values)
6670

6771
assert properties["name"] == col.name
6872
assert properties["system"] is False
@@ -202,6 +206,10 @@ def test_collection_management(db, bad_db, cluster):
202206

203207
col_name = generate_col_name()
204208

209+
with pytest.raises(ValueError):
210+
# schema must not be empty
211+
db.create_collection(name=col_name, schema={})
212+
205213
col = db.create_collection(
206214
name=col_name,
207215
sync=True,

0 commit comments

Comments
 (0)