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.

Schema validation

See original GitHub issue

Hello there! I’m trying to get fastify-multipart form and fastify/ajv schema validation to work together in a restfull api. But I still get an error :

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "body should be object",
    "validation": [
        {
            "keyword": "type",
            "dataPath": "",
            "schemaPath": "#/type",
            "params": {
                "type": "object"
            },
            "message": "should be object"
        }
    ],
    "code": "BadRequest"
}

I’ve extented AJV with a fileType

ajv.addKeyword("isFileType", {
        compile: (schema, parent, it) => {
            // Change the schema type, as this is post validation it doesn't appear to error.
            (parent as any).type = "file";
            delete (parent as any).isFileType;
            return () => true;
        },
    });

Here is my schema

{
    "consumes": ["multipart/form-data"],
    "body": {
      "type": "object",
      "properties": {
       "urbanFile": {
          "isFileType": true,
          "type": "object"
        },
        "authorizationType": {
          "type": "string",
          "enum": [
            "buildingPermit",
            "planningPermit",
            "demolitionPermit"
          ]
        },
}

and here is my handler function

const mutliPart = request.multipart(
      function fileHandler(field, file, filename, encoding, mimetype) {
        console.log("file is in " + field + " and is named " + filename);
      },
      function done(error) {
        if (error) {
          throw error;
        }
      }
    );

    mutliPart.on('field', function (key, value) {
      console.log('form-data', key, value)
    })
    reply.status(201).send("Victory");

Is this a bug ? or am I doing something wrong? any help on this would be much much appreciated.

Cheers, Olivier

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
oliviertocommented, Dec 18, 2019

@chenchuraviteja what is your validation schema ?

1reaction
chenchuravitejacommented, Dec 19, 2019

validation schema

schema: { tags: [{ name: ‘Category’ }], description: ‘Post category data’, consumes: [ “multipart/form-data” ], body: { type: ‘object’, properties: { name: { type: ‘string’ }, thumg_url: { isFileType: true, type: ‘object’ }, img_url: { isFileType: true, type: ‘object’ }, status: { type: ‘number’, enum: [0, 1], default: 1 } }, required: [‘name’, ‘thumg_url’, ‘img_url’] }, response: { 201: { type: ‘object’, properties: categoryProperties } } }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schema Markup Validator
Schema.org. menu. Documentation · Schemas · About. public. NEW TEST. languages ... help_outline. play_arrow. VALIDATE.
Read more >
Schema Validation — MongoDB Manual
Schema validation lets you create validation rules for your fields, such as allowed data types and value ranges. MongoDB uses a flexible schema...
Read more >
Schema Markup Testing Tool | Google Search Central
Schema Markup Validator. Validate all Schema.org-based structured data that's embedded in web pages, without Google feature specific warnings. Go to the Schema ......
Read more >
A Vocabulary for Structural Validation of JSON - JSON Schema
JSON Schema validation asserts constraints on the structure of instance data. An instance location that satisfies all asserted constraints is then annotated ...
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 >

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