Search code examples
microservicessoftware-designsystem-design

How to Sync Front end and back end validation


I have a project using micro services, front end communicate with back end with multiple Rest API calls.

Both side (Front or Back) have validation. Is there a good way to sync back end and front end validation together.

For example, if there's way to create an validation file (JSON Yaml etc.), which front end and back end could read and understand it perfectly. Only managing validation file, then I can control validation on both end. Instead, to change validation, I need notify both front and back end, it is not efficient.

Please leave your notes if any thoughts across you.


Solution

  • Depending on the specific requirements, something like Protocol Buffers could be used which has many options for validation.

    If both endpoints are servers and a browser is not involved, then one option is Consumer Driven Contracts, e.g.: https://spring.io/projects/spring-cloud-contract

    If the client is a browser then you could write the validation rules in a programming language that can compile to run both on the server side (e.g. JVM) and in the browser (i.e. JavaScript), such as: https://ktor.io/