如何同步前端和后端验证 [英] How to Sync Front end and back end validation

查看:84
本文介绍了如何同步前端和后端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用微服务的项目,前端与后端通过多个 Rest API 调用进行通信.

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.

例如,如果有办法创建一个验证文件(JSON Yaml 等),前端和后端可以完美地读取和理解它.只管理验证文件,然后我可以控制两端的验证.相反,要更改验证,我需要通知前端和后端,效率不高.

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.

推荐答案

根据具体要求,可以使用诸如 Protocol Buffers 之类的东西,它有很多验证选项.

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

如果两个端点都是服务器并且不涉及浏览器,那么一种选择是消费者驱动的合同,例如:https://spring.io/projects/spring-cloud-contract

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

如果客户端是浏览器,那么您可以用一种编程语言编写验证规则,该语言可以编译为在服务器端(例如 JVM)和浏览器(例如 JavaScript)中运行,例如:https://ktor.io/

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/

这篇关于如何同步前端和后端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆