Giter Club home page Giter Club logo

Comments (8)

mcollina avatar mcollina commented on May 18, 2024

You can get a JSON schema tutorial at https://json-schema.org/.

from fast-json-stringify.

previousdeveloper avatar previousdeveloper commented on May 18, 2024

I have generated but not working.

title: 'Listing Response Schema',
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'string'
}
}
}

from fast-json-stringify.

mcollina avatar mcollina commented on May 18, 2024

There are several examles in https://github.com/fastify/fast-json-stringify/blob/master/test/array.test.js

from fast-json-stringify.

previousdeveloper avatar previousdeveloper commented on May 18, 2024

Yes i found them, but these examples are object array json, my example is different array object not supported ?

from fast-json-stringify.

mcollina avatar mcollina commented on May 18, 2024

Would you mind sending a PR with a failing unit test? Thanks.

(https://github.com/fastify/fast-json-stringify/blob/master/bench.js#L41 runs a benchmark for a very similar case)

from fast-json-stringify.

CodingXD avatar CodingXD commented on May 18, 2024

@mcollina I trying to pass an array of objects, but I'm only getting an empty array.
This is how the body schema looks like

body: {
        type: "object",
        properties: {
          additionalImages: {
            type: "array",
            items: {
              type: "object",
              properties: {
                url: { type: "string" },
                name: { type: "string" },
              },
            },
          },
        },

from fast-json-stringify.

Eomm avatar Eomm commented on May 18, 2024

Could you add a Minimal, Reproducible Example?

Without it, we are unable to help you.

from fast-json-stringify.

CodingXD avatar CodingXD commented on May 18, 2024

Sure @Eomm , here it is

fastify.route({
    method: "POST",
    url: "/create",
    schema: {
      body: {
        type: "object",
        properties: {
          roomName: { type: "string", minLength: 1 },
          featuredImage: {
            type: "object",
            properties: {
              url: { type: "string" },
              name: { type: "string" },
            },
          },
          additionalImages: {
            type: "array",
            items: {
              type: "object",
              properties: {
                url: { type: "string" },
                name: { type: "string" },
              },
            },
          },
        },
        additionalProperties: false,
        required: [
          "roomName", "featuredImage",
        ],
      },
      response: {
        201: {
          type: "object",
          properties: {
            success: { type: "boolean" },
          },
        },
      },
    },
    handler: async function (request, reply) {
            client = await fastify.pg.connect();
            const {
              roomName,
              featuredImage,
              additionalImages,
            }: any = request.body;
            
            console.log(roomName); // Super deluxe room
            console.log(featuredImage); // {url: "https://.............", name: "file.jpg"}
            console.log(additionalImages); // []

            // rest of irrelevant code
            return reply.code(201).send({ success: true });
    },
  });

In the client, I passed values like this

const additionalImages= [{ url: "https://images.lib.com/.......", name: "hits1.jpg" }, { url: "https://images.lib.com/.......", name: "hits2.jpg" }, { url: "https://images.lib.com/.......", name: "hits3.jpg" }];
const featuredImage = { url: "https://images.lib.com/.......", name: "beats.jpg" };
const response = await fetch(
        `${process.env.NEXT_PUBLIC_BASE_URL}/rooms/create`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            room_name: roomName,
            additionalImages,
            featuredImage,
          }),
        }
      );
      const result = await response.json();

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.