I need to get a list of the required fields out of a JSON-Schema+Data.
Currently, we are using AJV to get error messages in our forms with JSON Schema and it is working great.
I need a way to get all the required fields (even if filled) in order to mark those fields with * as "required". required fields might change depending on the schema and data combinations.
Also tried hacking tv4 to extract the required fields without success.
Please help.
Example for such schema:
{
"type": "object",
"required": [
"checkbox"
],
"properties": {
"checkbox": {
"type": "boolean"
},
"textbox": {
"type": "string"
}
},
"oneOf": [
{
"required": [
"textbox"
],
"properties": {
"checkbox": {
"enum": [
true
]
}
}
},
{
"properties": {
"checkbox": {
"enum": [
false
]
}
}
}
],
"additionalProperties": false
}
Rereading your question the easiest way to do what you'd like would be to
sample 1
for(let prop in data) {
if(data.hasOwnProperty(prop) {
//set value to null, -1, or some other universally bad value
data[prop]...value = null;
}
}
sample 2
let result = tv4.validateMultiple(data, schema);
let required = result.errors;