Skip to content

[Bug]: Custom Controller Sanitization Documentation referencing V4 way #2659

@tomh4

Description

@tomh4

Link to the documentation page or resource

https://docs.strapi.io/cms/backend-customization/controllers#sanitization-and-validation-in-controllers

Describe the bug

In the documentation it still says to use the old way of sanitizing content in custom controllers, even though this is no longer working.

import { sanitize, validate } from '@strapi/utils';

export default {
  async findCustom(ctx) {
    const contentType = strapi.contentType('api::test.test');

    await validate.contentAPI.query(ctx.query, contentType, { auth: ctx.state.auth });
    const sanitizedQueryParams = await sanitize.contentAPI.query(ctx.query, contentType, { auth: ctx.state.auth });

    const documents = await strapi.documents(contentType.uid).findMany(sanitizedQueryParams);

    return await sanitize.contentAPI.output(documents, contentType, { auth: ctx.state.auth });
  }
}

Additional context

In the migration docs it is outline to use the new way:
https://docs.strapi.io/cms/migration/v4-to-v5/breaking-changes/strapi-utils-refactored#additional-notes

// Strapi v4
import { validate, sanitize } from '@strapi/utils';

validate.contentAPI.xxx();
sanitize.contentAPI.xxx();

// Strapi 5
// Those methods require the strapi app context
strapi.contentAPI.sanitize.xxx();
strapi.contentAPI.validate.xxx();

Suggested improvements or fixes

Add the new way to the docs so they are correct again

Related issue(s)/PR(s)

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions