@@ -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
179214Custom 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
187248Inherticance Diagrams
188249=====================
0 commit comments