node.jsjsonjsonschemafastify

How to validate all properties of a schema using fluent-json-schema?


I want to be able to validate all the properties in a schema, but I'm getting an error which only contains one missing property not all of them. I have the following schema:

const headersSchema = S.object()
  .prop('platform', S.string()).required()
  .prop('device-data', S.string()).required()
  .prop('device-id', S.string()).required()
  .prop('consumer-id', S.string()).required()
  .prop('version', S.string()).required()

This is the error I'm getting:

[
  {
    "instancePath": "",
    "schemaPath": "#/required",
    "keyword": "required",
    "params": {
      "missingProperty": "device-data"
    },
    "message": "must have required property 'device-data'"
  }
]

But I'm not sending two properties on the headers: device-data and device-id. I want to get an error that those 2 properties are missing that I am not submitting.

This is my route:

server.post(
  '/headers',
  {
    schema: {
      headers: headersSchema
    }
  },
  () => {}
)

Solution

  • It is an ajv setup, you must configure it because it may cause a DOS attack:

    const fastify = require('fastify')({
      ajv: {
        customOptions: {
          allErrors: true
        }
      }
    })
    

    Regarding the DOS attack:

    Sources: