Reputation: 21155
I have a special enum case in my code and need to validate against it:
{
"status": 10
}
Let's use this imaginary list of valid values:
var valid = [10, 20, 23, 27];
How can I alter my JSON Schema to validate one of these values?
{
type: 'object',
required: ['status'],
properties: {
status: { type: number },
}
}
Upvotes: 10
Views: 8355
Reputation: 1246
If I understand you correctly, I think you'll have to loop through all the values as Javascript does not have a thing like enums.
var validValues = [ 10, 20, 23, 27 ];
var statusType = json.properties.status.type;
/* This function call will return a boolean that tells you wether the value in your json is valid or not.*/
isValid( statusType );
function isValid( statusType )
{
for( var i = 0; i < validValues.length; i++ )
if( statusType === validValues[i] )
return true;
return false;
}
I have simplified the example a bit, but you'll get my drift.
Upvotes: -1
Reputation: 13635
You just define the status
property as an enum
:
{
"type" : "object",
"required" : ["status"],
"properties" : {
"status" : {
"type" : "number",
"enum" : [10, 20, 23, 27]
}
}
}
Upvotes: 17