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.

Body schema validation

See original GitHub issue

Hi!

Thanks for the amazing project, it’s helping me a lot!

I’m having an issue on controllers when I try to insert schema validation, like this:

@POST('/', { schema })
  public async create(
    request: FastifyRequest<{ Body: CreateTransactionSchema }>,
    response: FastifyReply,
  ): Promise<FastifyReply> {
...

And I have a schema like this:

export const schema = {
  schema: {
    body: {
      type: 'object',
      requiredKeys: ['title', 'value', 'category', 'realized_date'],
      properties: {
        title: { type: 'string' },
        value: { type: 'number' },
        category: { type: 'string' },
        realized_date: { type: 'string' },
      },
    },
  },
};

When I try to run this method on postman, for example, it simply bypass my schema validation. Do you have any suggestion what I have to do to handle schema validation on the body of requests?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
camisetagscommented, Dec 13, 2020

Thanks @L2jLiga and @unematiii!

Both changes worked for me. But following fastify’s docs, the wright config for avj is:

const app = fastify({
    ajv: {
      customOptions: {
        removeAdditional: true,
        useDefaults: true,
        coerceTypes: false, // Here's the change to Fastify's default options
        nullable: true,
      },
    },
  });

With this on configs and required set on schema, ererything worked perfectly for me. 😄 Thanks again!

1reaction
unematiiicommented, Dec 13, 2020

Shouldn’t it be required instead of requiredKeys? See docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a JSON schema to validate a response body - Learnetto
The validate() method compiles the JSON schema with Ajv, and then returns a middleware function which will be run every time a request...
Read more >
JSON Schema Validator - Newtonsoft
View source code. An online, interactive JSON Schema validator. Supports JSON Schema Draft 3, Draft 4, Draft 6, Draft 7 and Draft 2019-09....
Read more >
Schema Validation - express-validator
Schemas are a special, object-based way of defining validations or sanitizations on requests. At the root-level, you specify field paths as keys, ...
Read more >
JSON Schema | The home of JSON Schema
JSON Schema is a declarative language that allows you to annotate and validate JSON documents. JSON Schema enables the confident and reliable use...
Read more >
Schema Validation - API Shield - Cloudflare Docs
Schema Validation allows you to check if incoming traffic complies with a previously supplied API schema. When you provide an API schema, API ......
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