A
A
Andrey Sokolov2017-12-29 17:12:18
JavaScript
Andrey Sokolov, 2017-12-29 17:12:18

Scheme for JSON. How to validate and replace incorrect properties?

Hello.
Suggest a JS library.
It is necessary to generalize the data received from the API.
For example
, there is a certain scheme (conditional):

const Scheme = {
"name":  string,
"tags": array,
"stocks": object,
"someprop": string
}

I get from the API:
[{
"name": "qwerty",
"tags": undefined,
"stocks": [1]
}]

As a result, I need a similar result:
[{
"name": "qwerty",
"tags": [],
"stocks": {},
"someprop": ''
}]

And another question - are such solutions practiced, which I described, or can this problem be solved somehow differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2017-12-29
@Nipheris

Schema JSON validation - practised . As for replacements and "auto-repair" - I have not seen this, maybe you need to customize the verification process according to the scheme, when you need to do something with an invalid element (throw away and put a stub according to the scheme).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question