Skip to content

Commit e3f586d

Browse files
committed
start of more useful data validator
1 parent b5a6d12 commit e3f586d

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/compas/data/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@
4040
is_int3
4141
is_float3
4242
is_float4x4
43-
validate_data
4443
4544
"""
4645
from __future__ import absolute_import
4746

4847
from .validators import is_int3
4948
from .validators import is_float3
5049
from .validators import is_float4x4
51-
from .validators import validate_data
5250
from .encoders import DataEncoder
5351
from .encoders import DataDecoder
5452
from .data import Data
@@ -62,7 +60,6 @@
6260
'is_int3',
6361
'is_float3',
6462
'is_float4x4',
65-
'validate_data',
6663
'json_load',
6764
'json_loads',
6865
'json_dump',

src/compas/data/data.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def __setstate__(self, state):
221221
self.data = state['data']
222222

223223
def validate_data(self):
224-
"""Validate the data of this object against its data schema (`self.DATASCHEMA`).
224+
"""Validate the object's data against its data schema (`self.DATASCHEMA`).
225225
226226
Returns
227227
-------
@@ -235,20 +235,19 @@ def validate_data(self):
235235
return self.DATASCHEMA.validate(self.data)
236236

237237
def validate_json(self):
238-
"""Validate the data loaded from a JSON representation of the data of this object against its data schema (`self.DATASCHEMA`).
238+
"""Validate the object's data against its json schema (`self.JSONSCHEMA`).
239239
240240
Returns
241241
-------
242-
None
242+
str
243+
The validated JSON representation of the data.
243244
244245
Raises
245246
------
246247
SchemaError
247248
"""
248249
import jsonschema
249-
jsondata = json.dumps(self.data, cls=DataEncoder)
250-
data = json.loads(jsondata, cls=DataDecoder)
251-
jsonschema.validate(data, schema=self.JSONSCHEMA)
252-
self.data = data
253-
self.DATASCHEMA.validate(self.data)
254-
return jsondata
250+
jsonstring = json.dumps(self.data, cls=DataEncoder)
251+
jsondata = json.loads(jsonstring, cls=DataDecoder)
252+
jsonschema.validate(jsondata, schema=self.JSONSCHEMA)
253+
return jsonstring

src/compas/data/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ def validate_data(data, cls):
4141
SchemaError
4242
"""
4343
import jsonschema
44-
jsonschema.validate(data, schema=cls.get_JSONSCHEMA())
44+
jsonschema.validate(data, schema=cls.JSONSCHEMA)
4545
return cls.DATASCHEMA.validate(data)

0 commit comments

Comments
 (0)