question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serialize in request validation and authorize object when toJSON() matches to the format

See original GitHub issue

Is your feature request related to a problem? Please describe. I would like to do 2 things :

  • serialize String data to Object when I receive it from request
  • deserialize object when i want to return the response and validate my response

I have two kinds of objects in my mind :

  • Date objects
  • Mongodb ObjectID

Describe the solution you’d like It could be great to allow developers to add some adapters that could be considered by express-openapi-validator on request and on response. For example, i could declare in my openapi components section :

  components: 
    schemas: 
      ObjectId:
        type: "string"
        pattern: "^[0-9a-fA-F]{24}$"
      Date:
        type: "string"
        format: "date"
      DateTime:
        type: "string"
        format: "date-time"

then i could configure my OpenAPIValidator with those adapters :

OpenApiValidator({
  apiSpec: './test/resources/openapi.yaml',
  validateRequests: true, // (default)
  validateResponses: true, // false by default
  formatAdapters : {
     '#/components/schemas/ObjectId' : {
        serialize : o => new ObjectID(o)
        deserialize : o => o.toJSON()
    },
    '#/components/schemas/Date' : {
        serialize : o => new Date(o)
        deserialize : o => o.toISOString().slice(0,10);
    },
    '#/components/schemas/DateTime' : {
        serialize : o => new Date(o)
        deserialize : o => o.toISOString();
    } 
  }
})

Describe alternatives you’ve considered I already tried to hack the formats.js by adding new formats and a “validate” description I also used in the past (2014-2015), a library, “tdegrunt/jsonschema” ,in which I added a hook to do this. I manage to do this but I need to parse twice the JSON body : once with jsonschema then once with express-openapi-validator.

Additional context I saw some issues related to this issue even if they are not explained the same way. I think this solution would be adaptable :

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pileroucommented, Dec 18, 2020

In my pull request, I tested it. It also worked for request parameters.

0reactions
cdimasciocommented, Dec 26, 2020

v4.10.0 resolves this issue for date-time and date formats. a custom serializer mechanism is still required for custom types e.g. MongoDb ObjectId

This issue must remain open as a solution is still needed for custom serializers to handle cases like ObjectId

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Schema Serializer and Deserializer
This document describes how to use JSON Schema with the Apache Kafka® Java client and console tools. Both the JSON Schema serializer and...
Read more >
Validation-and-Serialization - Fastify
The serializerCompiler is a function that returns a function that must return a string from an input object. When you define a response...
Read more >
How to Read Json Response Body using Rest Assured?
Using these methods we can get an Object of type io.restassured.response.ResponseBody. This class represents the Body of a received Response.
Read more >
Basic JSON APIs with Route-to-code in ASP.NET Core
Write JSON response · Adds an HTTP GET API endpoint with /hello/{name:alpha} as the route template. · When the route is matched, the...
Read more >
Object validation and conversion with Marshmallow in Python
To serialize objects, the snippet uses the dump method of the BookMarkSchema class, which returns a formatted JSON object. To validate that this ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found