Skip to content

Commit 01e5fe6

Browse files
committed
tutorial completish
1 parent 7aa3272 commit 01e5fe6

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

docs/tutorial/data.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,75 @@ which with help with intellisense and code completion.
175175
Validation
176176
==========
177177

178+
A somewhat experimental feature of the data package is data validation.
179+
The base data class defines two unimplemented attributes :attr:`compas.data.Data.JSONSCHEMA` and :attr:`compas.data.Data.JSONSCHEMA`.
180+
The former is meant to define the name of the json schema in the ``schema`` folder of :mod:`compas.data`,
181+
and the latter a Python schema using :mod:`schema.Schema`.
182+
183+
If a deriving class implements those attributes, data sources can be validated against the two schemas to verify compatibility
184+
of the available data with the object type.
185+
186+
::
187+
188+
>>> from compas.data import validate_data
189+
>>> from compas.geometry import Frame
190+
>>> data = {'point': [0.0, 0.0, 0.0], 'xaxis': [1.0, 0.0, 0.0], 'zaxis': [0.0, 0.0, 1.0]}
191+
>>> validate_data(data, Frame)
192+
Validation against the JSON schema of this object failed.
193+
Traceback (most recent call last):
194+
...
195+
196+
jsonschema.exceptions.ValidationError: 'yaxis' is a required property
197+
198+
Failed validating 'required' in schema:
199+
{'$compas': '1.7.1',
200+
'$id': 'frame.json',
201+
'$schema': 'http://json-schema.org/draft-07/schema#',
202+
'properties': {'point': {'$ref': 'compas.json#/definitions/point'},
203+
'xaxis': {'$ref': 'compas.json#/definitions/vector'},
204+
'yaxis': {'$ref': 'compas.json#/definitions/vector'}},
205+
'required': ['point', 'xaxis', 'yaxis'],
206+
'type': 'object'}
207+
208+
On instance:
209+
{'point': [0.0, 0.0, 0.0],
210+
'xaxis': [1.0, 0.0, 0.0],
211+
'zaxis': [0.0, 0.0, 1.0]}
212+
178213

179214
Custom Objects
180215
==============
181216

217+
To add a new object class that implements the data interface, only a few attributes have to be implemented.
218+
219+
.. code-block:: python
220+
221+
class MyObject(Data):
222+
223+
def __init__(self, a, b, **kwargs):
224+
super(MyObject, self).__init__(**kwargs)
225+
self.a = a
226+
self.b = b
227+
228+
@property
229+
def data(self):
230+
"""dict : The data dictionary that represents the data of the object."""
231+
return {'a': self.a, 'b': self.b}
232+
233+
@data.setter
234+
def data(self, data):
235+
self.a = data['a']
236+
self.b = data['b']
237+
238+
@classmethod
239+
def from_data(cls, data):
240+
return cls(data['a'], data['b'])
241+
182242
183243
GH Components
184244
=============
185245

246+
*Coming soon...*
186247

187248
Inherticance Diagrams
188249
=====================

src/compas/data/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
is_int3
4444
is_float3
4545
is_float4x4
46+
validate_data
4647
4748
4849
Exceptions
@@ -64,6 +65,7 @@
6465
from .validators import is_int3
6566
from .validators import is_float3
6667
from .validators import is_float4x4
68+
from .validators import validate_data
6769
from .encoders import DataEncoder
6870
from .encoders import DataDecoder
6971
from .data import Data
@@ -84,5 +86,6 @@
8486
'json_load',
8587
'json_loads',
8688
'json_dump',
87-
'json_dumps'
89+
'json_dumps',
90+
'validate_data'
8891
]

0 commit comments

Comments
 (0)