| 
 | 1 | +---  | 
 | 2 | +title: REST API input is validated by default in controllers  | 
 | 3 | +description: In Strapi 5, REST API input is validated by default in controllers, instead of accepting invalid data and sanitizing it silently.  | 
 | 4 | +sidebar_label: Default input validation  | 
 | 5 | +displayed_sidebar: devDocsMigrationV5Sidebar  | 
 | 6 | +tags:  | 
 | 7 | + - breaking changes  | 
 | 8 | + - controllers  | 
 | 9 | + - validation  | 
 | 10 | +---  | 
 | 11 | + | 
 | 12 | +import Intro from '/docs/snippets/breaking-change-page-intro.md'  | 
 | 13 | +import MigrationIntro from '/docs/snippets/breaking-change-page-migration-intro.md'  | 
 | 14 | +import YesPlugins from '/docs/snippets/breaking-change-affecting-plugins.md'  | 
 | 15 | +import NoCodemods from '/docs/snippets/breaking-change-not-handled-by-codemod.md'  | 
 | 16 | + | 
 | 17 | +# REST API input is validated by default in controllers  | 
 | 18 | + | 
 | 19 | +Sanitization means that the object is “cleaned” and returned.  | 
 | 20 | + | 
 | 21 | +Validation means an assertion is made that the data is already clean and throws an error if something is found that shouldn't be there.  | 
 | 22 | + | 
 | 23 | +Strapi methods exist both for [sanitization and validation in controllers](/dev-docs/backend-customization/controllers#sanitization-and-validation-in-controllers) and they can target input body data, query parameters, and output (only for sanitization).  | 
 | 24 | + | 
 | 25 | +In Strapi 5, REST API input is validated by default in controllers, instead of accepting invalid data and sanitizing it silently.  | 
 | 26 | + | 
 | 27 | +<Intro />  | 
 | 28 | + | 
 | 29 | +<YesPlugins />  | 
 | 30 | +<NoCodemods />  | 
 | 31 | + | 
 | 32 | +## Breaking change description  | 
 | 33 | + | 
 | 34 | +<SideBySideContainer>  | 
 | 35 | + | 
 | 36 | +<SideBySideColumn>  | 
 | 37 | + | 
 | 38 | +**In Strapi v4**  | 
 | 39 | + | 
 | 40 | +In v4, query parameters are validated, but input data (create and update body data) is only sanitized.  | 
 | 41 | + | 
 | 42 | +</SideBySideColumn>  | 
 | 43 | + | 
 | 44 | +<SideBySideColumn>  | 
 | 45 | + | 
 | 46 | +**In Strapi 5**  | 
 | 47 | + | 
 | 48 | +In v5, both query parameters and input data are validated.  | 
 | 49 | + | 
 | 50 | +</SideBySideColumn>  | 
 | 51 | + | 
 | 52 | +</SideBySideContainer>  | 
 | 53 | + | 
 | 54 | +## Migration  | 
 | 55 | + | 
 | 56 | +<MigrationIntro />  | 
 | 57 | + | 
 | 58 | +### Notes  | 
 | 59 | + | 
 | 60 | +* A `400 Bad Request` error will be thrown if the request has invalid values such as in in the following cases:  | 
 | 61 | + | 
 | 62 | + - relations the user do not have permission to create  | 
 | 63 | + - unrecognized values that are not present on a schema  | 
 | 64 | + - attempt to writing non-writable fields and internal timestamps like `createdAt` and `createdBy` fields  | 
 | 65 | + - usage of the `id` field (other than for connecting relations) to set or update the `id` of an object  | 
 | 66 | + | 
 | 67 | +### Manual procedure  | 
 | 68 | + | 
 | 69 | +Users should ensure that parameters and input data are valid to avoid `400` errors being thrown. Additional information can be found in the [sanitization and validation in controllers](/dev-docs/backend-customization/controllers#sanitization-and-validation-in-controllers) documentation.  | 
0 commit comments