Skip to content

Commit

Permalink
Fixes on SerDes (cdimascio#682)
Browse files Browse the repository at this point in the history
* Try catch serdes serialize and deserialize in order to avoid Internal Server Error and return BadRequest errors cdimascio#601

* Fix incorrect serDes example cdimascio#569
  • Loading branch information
pilerou authored Dec 21, 2021
1 parent 3f0de5d commit 56f778b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ To create custom serializers and/or deserializers, define:
e.g.
```javascript
serDes: [{
serDes: [
// installs dateTime serializer and deserializer
OpenApiValidator.serdes.dateTime,
// installs date serializer and deserializer
Expand All @@ -784,8 +784,8 @@ serDes: [{
format: 'mongo-objectid',
deserialize: (s) => new ObjectID(s),
serialize: (o) => o.toString(),
}
}],
},
],
```
The mongo serializers will trigger on the following schema:
Expand Down
14 changes: 12 additions & 2 deletions src/framework/ajv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ function createAjv(
];
return false;
}
obj[propName] = sch.deserialize(data);
try {
obj[propName] = sch.deserialize(data);
}
catch(e) {
return false;
}
}
return true;
};
Expand Down Expand Up @@ -99,7 +104,12 @@ function createAjv(
return function validate(data, path, obj, propName) {
if (typeof data === 'string') return true;
if (!!sch.serialize) {
obj[propName] = sch.serialize(data);
try {
obj[propName] = sch.serialize(data);
}
catch(e) {
return false;
}
}
return true;
};
Expand Down

0 comments on commit 56f778b

Please sign in to comment.