GithubHelp home page GithubHelp logo

Comments (8)

jwalton avatar jwalton commented on July 22, 2024

I suspect this is an unexpected side effect of #53 @erykwarren. :(

from exegesis.

jwalton avatar jwalton commented on July 22, 2024

(Also, thanks for the detailed error report!)

from exegesis.

jwalton avatar jwalton commented on July 22, 2024

🎉 This issue has been resolved in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

from exegesis.

erykwarren avatar erykwarren commented on July 22, 2024

@jwalton Sorry for having introduced this bug :( . For what it is worth, swagger-tool did also parse the JSON to do the validation.

from exegesis.

jwalton avatar jwalton commented on July 22, 2024

from exegesis.

openreply-dleinhaeuser avatar openreply-dleinhaeuser commented on July 22, 2024

If the body is an object you could store it somewhere else before stringifying it and then validate that as an optimization.

from exegesis.

jwalton avatar jwalton commented on July 22, 2024

It's not quite that simple unfortunately. The thing that makes it complicated is that you can write a toJSON function on an object that will control how it is serialized:

const foo = {toJSON() {return "bar";}};
JSON.stringify({foo}); // returns '{"foo":"bar"}'

We're using ajv to do schema validation, and unfortunately it doesn't call into toJSON for us. So if you try:

const Ajv = require('ajv');
const ajv = new Ajv();
const validate = ajv.compile({
    type: 'object',
    properties: {
        myString: {type: "string"}
    }
});
validate({myString: 'bar'}); // passes
validate({myString: foo}); // fails

And, at first glance, you might think "Why would anyone care about this weird esoteric feature of JSON stringifiers?" But, if you use MongoDB (as a lot of node.js folks do), ObjectId is an object, with a toJSON() that converts it into a string.

So if there's an object with a toJSON(), we actually need to convert your response to JSON (or at least that object) and then parse it back before we can validate it. So we need to know ahead of time that you're not using any objects with toJSON(), or we need to traverse the response tree and check to see if you have any objects with toJSON(), or we need to give up and just do this for every response. :P

from exegesis.

openreply-dleinhaeuser avatar openreply-dleinhaeuser commented on July 22, 2024

You could do something like this (using lodash for brevity):

_.cloneDeepWith(body, (value, key) => {
  if ('toJSON' in value && _.isFunction(value.toJSON)) {
    return value.toJSON(key);
  }
  return undefined;
}

Question is of course, if this will be faster than serializing/deserializing and up to what size.
Maybe just not worth the hassle.

from exegesis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.