Skip to content

Commit f3ad9ad

Browse files
authored
Merge pull request #13 from openaddresses/auto-serialize
Automatic Object Serialization
2 parents 47a5458 + c8cb6be commit f3ad9ad

File tree

3 files changed

+2192
-630
lines changed

3 files changed

+2192
-630
lines changed

lib/middleware.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Param from './param.js';
33
import Ajv from 'ajv';
44
import addFormats from 'ajv-formats';
55
import { ValidationError } from 'express-json-validator-middleware';
6+
import Generic from '@openaddresses/batch-generic';
67

78
const ajv = addFormats(new Ajv({
89
allErrors: true,
@@ -99,6 +100,7 @@ export default class Middleware {
99100

100101
/**
101102
* Express middleware to validate the response body
103+
* and optionally serialize Generic object to JSON
102104
*
103105
* @param {Object} schema JSON Schema
104106
*
@@ -110,6 +112,10 @@ export default class Middleware {
110112
return function (req, res, next) {
111113
const json = res.json;
112114
res.json = function(obj) {
115+
if (obj instanceof Generic && typeof obj.serialize === 'function') {
116+
obj = obj.serialize();
117+
}
118+
113119
if (res.statusCode === null || res.statusCode === 200) {
114120
validate(obj);
115121
}

0 commit comments

Comments
 (0)