Home / Function/ payloadMethod() — fastify Function Reference

payloadMethod() — fastify Function Reference

Architecture documentation for the payloadMethod() function in input-validation.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  a866ccb4_e1fb_8b5b_3ad7_9dd2187491bd["payloadMethod()"]
  76f22d02_b2e2_1332_1404_348a87789f6a["input-validation.js"]
  a866ccb4_e1fb_8b5b_3ad7_9dd2187491bd -->|defined in| 76f22d02_b2e2_1332_1404_348a87789f6a
  style a866ccb4_e1fb_8b5b_3ad7_9dd2187491bd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

test/input-validation.js lines 8–335

module.exports.payloadMethod = function (method, t) {
  const test = t.test
  const fastify = require('..')()
  const upMethod = method.toUpperCase()
  const loMethod = method.toLowerCase()

  const opts = {
    schema: {
      body: {
        type: 'object',
        properties: {
          hello: {
            type: 'integer'
          }
        }
      }
    }
  }

  const ajv = new Ajv({ coerceTypes: true, removeAdditional: true })
  const optsWithCustomValidator = {
    schema: {
      body: {
        type: 'object',
        properties: {
          hello: {
            type: 'integer'
          }
        },
        additionalProperties: false
      }
    },
    validatorCompiler: function ({ schema, method, url, httpPart }) {
      return ajv.compile(schema)
    }
  }

  const optsWithJoiValidator = {
    schema: {
      body: Joi.object().keys({
        hello: Joi.string().required()
      }).required()
    },
    validatorCompiler: function ({ schema, method, url, httpPart }) {
      return schema.validate.bind(schema)
    }
  }

  const yupOptions = {
    strict: true, // don't coerce
    abortEarly: false, // return all errors
    stripUnknown: true, // remove additional properties
    recursive: true
  }

  const optsWithYupValidator = {
    schema: {
      body: yup.object().shape({
        hello: yup.string().required()
      }).required()
    },
    validatorCompiler: function ({ schema, method, url, httpPart }) {
      return data => {
        try {
          const result = schema.validateSync(data, yupOptions)
          return { value: result }
        } catch (e) {
          return { error: [e] }
        }
      }
    }
  }

  test(`${upMethod} can be created`, t => {
    t.plan(1)
    try {
      fastify[loMethod]('/', opts, function (req, reply) {
        reply.send(req.body)
      })
      fastify[loMethod]('/custom', optsWithCustomValidator, function (req, reply) {
        reply.send(req.body)

Domain

Subdomains

Frequently Asked Questions

What does payloadMethod() do?
payloadMethod() is a function in the fastify codebase, defined in test/input-validation.js.
Where is payloadMethod() defined?
payloadMethod() is defined in test/input-validation.js at line 8.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free