0

this is my route

app.patch(
  `/${root}/item/:itemId`,
  {
    schema: item_schema,
  },
  items // controller
);

and this is my schema file

import schema from "fluent-json-schema";
const body = schema
  .object()
  .prop(
    "itemTitle",
    schema.string().required()
  );

const response = schema
  .object()
  .prop(
    "payload",
    schema
      .object()
      .prop("itemTitle",schema.string().required())
  );

export item_schema = {
  body,
  response,
};

problem is with the response saying

"msg":"Failed building the serialization schema for PATCH: /v4/item/:itemId, due to error schema is invalid: data.properties should be object"

i tried making response vanilla and it worked

  const response = {
    response: {
      type: "object",
      properties: {
        payload: {
          type: "object",
          properties: {
            itemTitle: { type: "string" },
          },
          required:["itemTitle"]
        },
      },
    },
  };

and now I'm trying to do it with fluent schema but still getting the error

ponez
  • 1
  • 3
  • The schemas themselves look fine. Try using an empty object `{}` which is a valid schema, and see if you get the same problem. If you do, this is an XY problem. Sorry I can't be more help, I'm not familiar with fastify. – Relequestual Feb 03 '21 at 14:29
  • Ah sorry I think I miss-read, you said it worked OK with using the non-fluent approach. Have you tried outputting your fluent constructed schema to inspect the resulting JSON? – Relequestual Feb 03 '21 at 14:40
  • i tried your suggestion it works fine! i did something like this ```ts const response = {} ``` it's working but not working – ponez Feb 03 '21 at 14:42
  • how can i do that? by console logging ? – ponez Feb 03 '21 at 14:43
  • Yeah, as per docs: https://github.com/fastify/fluent-json-schema#usage `console.log(JSON.stringify(body.valueOf(), undefined, 2))` (swapped schema for body in your example) – Relequestual Feb 03 '21 at 14:44
  • ok i did it and here's the result ```ts { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "payload": { "type": "object", "properties": { "deactivation_reason": { "type": "string", "maxLength": 255, "minLength": 10 } } } } ``` it looks a lil messy in comments sorry – ponez Feb 03 '21 at 14:46
  • Schema looks fine to me. Unless anyone can provide additional insight into your use of fastify, I'd raise an issue on their github repo. – Relequestual Feb 03 '21 at 14:51

1 Answers1

0

response wrapper was missing :

const response = {
  response: schema
    .object()
    .prop(
      "payload",
      schema
        .object()
        .prop(
          "deactivation_reason",
          schema.string().maxLength(255).minLength(10)
        )
    ),
};
ponez
  • 1
  • 3