Repository: cdimascio/express-openapi-validator
Pull request #684 (2021-12-24 09:55:42)

Patch on serdes test and allow to use generated AJV out of Express usage
It fixes a bug in serdes unit test by sending a more human readable message
AND
it could allow to use the generated Ajv behind OpenApiValidator out of an express usage.
It could be great, for example, to validate websocket request... 

cdimascio: @pilerou, can you separate the bug fix from the new feature
i'd like to get the bug fix in immediately, but need more time to consider the feature request.

pilerou: Hi @cdimascio I did the revert in my fork on origin/master linked to this PR.
There's only fixes now on my master branch.
I created a branch for the new feature #683 : https://github.com/pilerou/express-openapi-validator/tree/initAjv
Do you want me to create another separated Pull request or do you want to have a look in my branch ?
cdimascio: @pilerou please create another PR. makes the merge and diff simpler to review

Patch:
diff --git a/src/framework/ajv/index.ts b/src/framework/ajv/index.ts
index bd5a6f7..ca879d4 100644
--- a/src/framework/ajv/index.ts
+++ b/src/framework/ajv/index.ts
@@ -59,6 +59,15 @@ function createAjv(
                   obj[propName] = sch.deserialize(data);
                 }
                 catch(e) {
+                  (<ajv.ValidateFunction>validate).errors = [
+                    {
+                      keyword: 'serdes',
+                      schemaPath: data,
+                      dataPath: path,
+                      message: `format is invalid`,
+                      params: { 'x-eov-serdes': propName },
+                    },
+                  ];
                   return false;
                 }
               }
@@ -108,6 +117,15 @@ function createAjv(
                   obj[propName] = sch.serialize(data);
                 }
                 catch(e) {
+                  (<ajv.ValidateFunction>validate).errors = [
+                    {
+                      keyword: 'serdes',
+                      schemaPath: data,
+                      dataPath: path,
+                      message: `format is invalid`,
+                      params: { 'x-eov-serdes': propName },
+                    },
+                  ];
                   return false;
                 }
               }


Test patch:
diff --git a/test/serdes.spec.ts b/test/serdes.spec.ts
index 3f822f8..0260326 100644
--- a/test/serdes.spec.ts
+++ b/test/serdes.spec.ts
@@ -307,13 +307,13 @@ describe('serdes serialize response components only', () => {
       .expect(500)
       .then((r) => {
         console.log(r);
-        expect(r.body.message).to.equal('d.toISOString is not a function');
+        expect(r.body.message).to.equal('.response.creationDate format is invalid');
       }));
 
   /*
   FIXME Manage format validation after serialize ? I can serialize using a working serialize method but that respond a bad format
   it('should throw error 500 on an object that serialize to a bad string format', async () =>
- 
+
     request(app)
       .get(`${app.basePath}/users/5fdefd13a6640bb5fb5fa925`)
       .query({baddateresponse : 'functionBadFormat'})
@@ -322,7 +322,7 @@ describe('serdes serialize response components only', () => {
         console.log(r.body);
         expect(r.body.message).to.equal('Something saying that date is not date-time format');
       }));
- 
+
    */
 
 });