API Filters and Validation

API Filters and Validation



Is it best to do all validation in filters (i.e. ActionFilterAttribute) before the main work begins?



In my application I have to validate the data in a URL, such as date validation, is it a number, is the incoming URL from an allowed device and Base64 authorization. From there the data is upserted to do a DB. It currently works but I do all the validation in the end point, but I would like to improve things.



In summary, should I use filters to do the upfront validation, then move to the main endpoint and complete the insert of data. What are the main advantages and disadvantages?



I have been doing some research on filters, but I have not found anything that says explicitly to use them for full validation. It seems that using filters would be the best way to do validation.




1 Answer
1



You could use filters for validations not specific to any component, or some validation that must be done before any work, that way you can reuse those filters in multiple actions.



For data validation related to a specific functionality, it's better to let the appropriate action to handle it, or else you would end with a bunch of filters.



Put simply:



Use filters to: validate incoming URL from an allowed device and Base64 authorization.



Use actions to: validate parameters (i.e, date, numbers, etc)






Thanks Daniel, that makes perfect sense...

– The OrangeGoblin
Sep 8 '18 at 4:06



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP code is not being executed, instead code shows on the page

Administrative divisions of China

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼