@@ -275,9 +275,38 @@ module.exports = function (MyModel) {
275275
276276##### function parameters
277277
278- - ` options ` All config options set for the serialization process. See below.
278+ - ` options ` All config options set for the serialization process.
279279- ` callback ` Callback to call with error or serialized records
280280
281+ ## Custom Deserialization
282+ For occasions where you need greater control over the deserialization process, you can implement a custom deserialization function for each model as needed. This function will be used instead of the regular deserialization process.
283+
284+ #### example
285+ ``` js
286+ module .exports = function (MyModel ) {
287+ MyModel .jsonApiDeserialize = function (options , callback ) {
288+ // either return an error
289+ var err = new Error (' Unable to deserialize record' );
290+ err .status = 500 ;
291+ cb (err)
292+
293+ // or
294+ // options.data is the raw data
295+ // options.result needs to be populated with deserialization result
296+ options .result = options .data .data .attributes ;
297+
298+ cb (null , options);
299+ }
300+ }
301+ ```
302+
303+ ##### function parameters
304+
305+ - ` options ` All config options set for the deserialization process.
306+ - ` callback ` Callback to call with error or serialized records
307+
308+ ## The options object
309+
281310###### ` options.type `
282311Resource type. Originally calculated from a models plural. Is used in the default
283312serialization process to set the type property for each model in a jsonapi response.
@@ -367,10 +396,80 @@ This is the attributes settings as defined in the `attributes` configuration opt
367396explained earlier. Use this in ` beforeJsonApiSerialize ` to make any model specific
368397adjustments before serialization.
369398
370- ## Serialization Hooks
371- For occasions when you don't want to fully implement serialization for a model manually but
372- you need to manipulate the serialization process, you can use the serialization
373- hooks ` beforeJsonApiSerialize ` and ` afterJsonApiSerialize ` .
399+ ###### ` options.data `
400+ The raw body data prior to deserialization from creates and updates. This can be
401+ manipulated prior to deserialization using ` beforeJsonApiDeserialize `
402+
403+ ###### ` options.result `
404+ The deserialized raw body data. This is used when saving
405+ models as part of a create or update operation. You can manipulate this prior to
406+ the save occuring in ` afterJsonApiDeserialize `
407+
408+ ## Serialization/Deserialization Hooks
409+ For occasions when you don't want to fully implement (de)serialization for a model manually but
410+ you need to manipulate the serialization/deserialization process, you can use the
411+ hooks ` beforeJsonApiSerialize ` , ` afterJsonApiSerialize ` , ` beforeJsonApiDeserialize ` and ` afterJsonApiDeserialize ` .
412+
413+ ### beforeJsonApiDeserialize
414+ In order to modify the deserialization process on a model by model basis, you can
415+ define a ` Model.beforeJsonApiDeserialize ` function as shown below. The function
416+ will be called with an options object and a callback which must be called with either
417+ an error as the first argument or the modified options object as the second
418+ parameter.
419+
420+ ** Examples of things you might want to use this feature for**
421+ - modifying ` options.data.data.attributes ` prior to their being deserialized into model properties that
422+ will be saved
423+ - modifying ` options.data.data.relationships ` prior to their being used to save relationship linkages
424+
425+ #### code example
426+ ``` js
427+ module .exports = function (MyModel ) {
428+ MyModel .beforeJsonApiDeserialize = function (options , callback ) {
429+ // either return an error
430+ var err = new Error (' Unwilling to deserialize record' );
431+ err .status = 500 ;
432+ callback (err)
433+
434+ // or return modified data
435+ options .data .data .attributes .title = ' modified title' ;
436+
437+ // returned options.data will be deserialized by either the default deserialization process
438+ // or by a custom deserialize function if one is present on the model.
439+ callback (null , options);
440+ }
441+ }
442+ ```
443+
444+ ### afterJsonApiDeserialize
445+ This function will be called with an options object and a callback which must be called with either
446+ an error as the first argument or the modified options object as the second parameter.
447+
448+ ** Examples of things you might want to use this feature for**
449+ - modifying ` options.result ` after their having being deserialized from ` options.data.data.attributes `
450+ - modifying ` options.data.data.relationships ` prior to their being used to save relationship linkages
451+
452+ #### code example
453+ ``` js
454+ module .exports = function (MyModel ) {
455+ MyModel .afterJsonApiDeserialize = function (options , callback ) {
456+ // either return an error
457+ var err = new Error (' something went wrong!' );
458+ err .status = 500 ;
459+ callback (err)
460+
461+ // or return modified data prior to model being saved with options.result
462+ options .result .title = ' modified title' ;
463+
464+ callback (null , options);
465+ }
466+ }
467+ ```
468+
469+ ##### function parameters
470+ - ` options ` All config options set for the deserialization process. See the "the options object"
471+ section above for info on what options properties are available for modification.
472+ - ` callback ` Callback to call with error or options object.
374473
375474### beforeJsonApiSerialize
376475In order to modify the serialization process on a model by model basis, you can
0 commit comments