55

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?

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54

1 Answers1

113

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
}
BBB
  • 615
  • 3
  • 12
  • 26
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73