Reputation: 514
So I have two Json files where I am validating them and If one file has extra key, I would like to throw an error. Right now with my below code, it does not give me any error, it takes the extra key with it, though I get an error if a key is missing but not if I have an extra key.
My piece of code is as follows:
async Task<JsonSchema> GetDataSchemaAsync( string format )
{
if( string.IsNullOrWhiteSpace( format ) )
throw new ArgumentException( "Table data format is invalid" );
var schemaUri = new Uri( _schemaBaseUri, $"{format}.json");
using var client = new WebClient();
var json = client.DownloadString( schemaUri );
return await JsonSchema.FromJsonAsync( json );
}
var schema = await GetDataSchemaAsync( format );
var data = JsonConvert.DeserializeObject<JObject>(line);
var errors = validator.Validate(data, schema);
My format file has the schema with keys mentioned and my line is my json format with values and an extra key.
Please advise.
Upvotes: 0
Views: 344
Reputation: 17382
JSON Schema supports the additionalProperties: boolean
setting. The default value for this setting is true
so additional properties will be allowed during validation. Docs
Set
"additionalProperties": false
in your schema and the validator will throw an error if the dataobject has any properties not defined in the schema.
Upvotes: 1