简体   繁体   中英

Auto-generating frontend and backend validation for Angular/Java

I hope my question is not too broad.

Our application has an Angular frontend and a Java backend and we need both frontend and backened validation. At the moment, we manually code validators in Typescript and Java that basically do the same thing. Since that is not only tedious but also error-prone, we would like to define once (maybe in a JSON file) which input fields have which constraints and ideally use a piece of software that auto-generates Java and Typescript code based on that definition.

My question is: are there any open-source/commercial software solutions that take care of this? It seems like a common enough problem, but I haven't come across a proper solution yet. JSON schema seemed promising and since we're using swagger anyway, I'd hoped that swagger-codegen would automatically take the supported JSON-schema keywords as constraints into account, but I haven't figured out how it works. When I generate Angular code, constraints such as 'minimum' and 'maximum' are completely ignored, and when I generate Java SpringBoot code, they only show up as annotations.

This is exactly what JSON Schema is designed for. Open API (swagger) modifies JSON Schema for it's own purposes, which doesn't include validation.

JSON Schema has validator implementations in most major languages allowing you to use the same schema to do the same validation on the front end and the back end. http://json-schema.org/implementations.html

Unfortunately, you likely won't be able to use the same schemas you use for your Open API service definition. I'm not aware of any validator implementations that support Open API's flavor of JSON Schema. I understand that Open API is trying to close the gap between their definition and JSON Schema, but a gap still exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM