Json Schema validation: do not allow fields other than those declared in schema

JsonValidationJsonschema

Json Problem Overview


Suppose that I have schema like

fname: string
lname: string
age: string

None of them are required. User can send me any of those attributes above but nothing else that is not declared. They can pass me fname, lname and age or all. But if they pass me all and additional property like middle_name the message should be rejected.

How would I define a schema like this?

Json Solutions


Solution 1 - Json

You can create a json-schema and use the option:

additionalProperties = false

That way you only allow the attributes defined in properties. In your case:

{
	"properties": {
		"fname": {"type": "string"},
		"lname": {"type": "string"},
		"age": {"type": "string"}
	},
	"additionalProperties": false
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTuan Anh TranView Question on Stackoverflow
Solution 1 - JsonjruizarangurenView Answer on Stackoverflow