Giter Club home page Giter Club logo

Comments (6)

mcollina avatar mcollina commented on August 16, 2024

Thanks for reporting! Would you mind to share an example of what it is not working for you?

from fast-json-stringify.

geraintguan avatar geraintguan commented on August 16, 2024

Sure! Here's an exmaple:

const ajv = require("ajv")();
const server = require("fastify")();

ajv.setFormat("test", /[a-z]{24}/);

server.setSchemaCompiler(schema => ajv.compile(schema));

server.post("/", {
  schema: {
    response: {
      "200": {
        type: "object";
        properties: {
          test: {
            type: "string",
            format: "test"
          }
        }
      }
    }
  }
}, async (req, res) => ({}));

server.listen();

Results in an error similar to:
screen shot 2018-09-14 at 09 48 22

I realise that it would have made more sense to open this on the main Fastify repository first, apologies! But as I was reading the code for Fastify I (think) I tracked it down to the fact that it uses this package to compile the response schemas.

from fast-json-stringify.

mcollina avatar mcollina commented on August 16, 2024

The problem is that this module is not used to validate the responses before sending them. It is used to render them. As such, it does not know anything about your format, it would in fact be totally ignored even if you pass it through. We use AJV to validate that your schema is valid JSON schema.

Passing in a custom AJV would remove the error, but it will just make that ignored.

from fast-json-stringify.

geraintguan avatar geraintguan commented on August 16, 2024

That makes sense, and I don't want any validation, I'd just like to be able to use a schema that I use in both the request and response (because they share the same data) but have the custom format ignored when it is used in the response.

I've realised that there is an alternative to passing in a custom Ajv instance but instead use the option unknownFormats: "ignore" on the internal Ajv instance of this package, which would allow custom formats in the response schema but ignore them.

from fast-json-stringify.

mcollina avatar mcollina commented on August 16, 2024

I've realised that there is an alternative to passing in a custom Ajv instance but instead use the option unknownFormats: "ignore" instead which would allow custom formats in the response schema but ignore them.

I think we should add this to fast-json-stringify ajv config.

from fast-json-stringify.

geraintguan avatar geraintguan commented on August 16, 2024

Yes that's what I was thinking. 👍

I'll open a PR. Thanks for the quick responses. 😄

from fast-json-stringify.

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.