I have a form that's being submitted from my front end to my back end, and the back end is responding. I'd like to ensure that they both send JSON in a format that the other side will interpret correctly.

From this question, I figured out that JSON schemas are the way to do that.

However, that's not quite what I want. JSON schema are a way of validating that the schema is correct.

For an example of what I've tried so far, in one of my earlier sites, I was using a Python Flask backend with a React frontend. I was only passing one value in the JSON so I was just using JSON in the form {'key': number} where the key named 'key' was the only key in the JSON. I stored the text 'key' as a constant both in Python and in Javascript. Ideally, both would look it up from the same file.

That is, I want my program to be able to look up the values in the schema and update. If the name of one key changes to something else, the rest of my program should update accordingly. Just using schemas doesn't work because they only validate; they don't update my program.

How do people usually make their back end and front end update to accommodate new schemas without changing all the constants in the code by hand?

As a fake example, it would be nice if the python schema program allowed me to give unchanging names to the properties. For example I have a 'price' property described as "the cost of the object". I then give a constant name like "PRICE" to this property. It's then referenceable in my code as Schema.PRICE. This means that if I get a new spec from the store owner that says "We decided that what used to be 'price' should now be called 'cost' because it's how much it costs our customers to pay for it," all I have to do is go to the schema and change 'price' to 'cost'. But in my code, everything remains Schema.PRICE which will now just evaluate to 'cost' when run. This allows the property names within my schema to be flexible while the code remains effortless to maintain.

So, how is this sort of flexibility usually accomplished?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.