Skip to content

Commit 3744cdd

Browse files
pileroucdimascio
andauthored
Patch on serdes test and allow to use generated AJV out of Express usage (#684)
* Try catch serdes serialize and deserialize in order to avoid Internal Server Error and return BadRequest errors #601 * Fix incorrect serDes example #569 * fix the unit test and change message to a more human friendly description of the error #601 * Allow to get the generated request AJV object in order to use it out of an OpenAPI and express usage (websocket...) #683 * Add documentation for OpenApiValidator.ajv function initialization usage #683 * ResponseValidator's Ajv can be useful too. So we return an object that contains both request ajv and response ajv : ```javascript ajvs = { req : 'Ajv object' res : 'Ajv object' } ``` #683 * fix the unit test and change message to a more human friendly description of the error #601 * Allow to get the generated request AJV object in order to use it out of an OpenAPI and express usage (websocket...) #683 * Add documentation for OpenApiValidator.ajv function initialization usage #683 * ResponseValidator's Ajv can be useful too. So we return an object that contains both request ajv and response ajv : ```javascript ajvs = { req : 'Ajv object' res : 'Ajv object' } ``` #683 * Revert commits in order to push only bug fixes #601 * Revert "ResponseValidator's Ajv can be useful too." This reverts commit 677cacf. * Revert "Add documentation for OpenApiValidator.ajv function initialization usage" This reverts commit a727f2d. * Revert "Allow to get the generated request AJV object in order to use it out of an OpenAPI and express usage (websocket...)" This reverts commit ad3e785. * Revert "ResponseValidator's Ajv can be useful too. So we return an object that contains both request ajv and response ajv : ```javascript ajvs = { req : 'Ajv object' res : 'Ajv object' } ``` #683" This reverts commit 8fc7226 * Revert "Add documentation for OpenApiValidator.ajv function initialization usage" This reverts commit ecb8424. * Revert "Allow to get the generated request AJV object in order to use it out of an OpenAPI and express usage (websocket...)" This reverts commit 52429c5. Co-authored-by: Carmine DiMascio <cdimasci@amazon.com>
1 parent 56f778b commit 3744cdd

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/framework/ajv/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ function createAjv(
5959
obj[propName] = sch.deserialize(data);
6060
}
6161
catch(e) {
62+
(<ajv.ValidateFunction>validate).errors = [
63+
{
64+
keyword: 'serdes',
65+
schemaPath: data,
66+
dataPath: path,
67+
message: `format is invalid`,
68+
params: { 'x-eov-serdes': propName },
69+
},
70+
];
6271
return false;
6372
}
6473
}
@@ -108,6 +117,15 @@ function createAjv(
108117
obj[propName] = sch.serialize(data);
109118
}
110119
catch(e) {
120+
(<ajv.ValidateFunction>validate).errors = [
121+
{
122+
keyword: 'serdes',
123+
schemaPath: data,
124+
dataPath: path,
125+
message: `format is invalid`,
126+
params: { 'x-eov-serdes': propName },
127+
},
128+
];
111129
return false;
112130
}
113131
}

test/serdes.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ describe('serdes serialize response components only', () => {
307307
.expect(500)
308308
.then((r) => {
309309
console.log(r);
310-
expect(r.body.message).to.equal('d.toISOString is not a function');
310+
expect(r.body.message).to.equal('.response.creationDate format is invalid');
311311
}));
312312

313313
/*
314314
FIXME Manage format validation after serialize ? I can serialize using a working serialize method but that respond a bad format
315315
it('should throw error 500 on an object that serialize to a bad string format', async () =>
316-
316+
317317
request(app)
318318
.get(`${app.basePath}/users/5fdefd13a6640bb5fb5fa925`)
319319
.query({baddateresponse : 'functionBadFormat'})
@@ -322,7 +322,7 @@ describe('serdes serialize response components only', () => {
322322
console.log(r.body);
323323
expect(r.body.message).to.equal('Something saying that date is not date-time format');
324324
}));
325-
325+
326326
*/
327327

328328
});

0 commit comments

Comments
 (0)