satish
satish

Reputation: 125

JSON Schema validation for typos in JSON

How to validate the schema properties for typos, when the property is not required value.

Ex JSON Schema:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  }
}

If the following is the JSON how can we catch the typo for "age" field which has typo as "aged".

{
  "firstName": "John",
  "lastName": "Doe",
  "aged": 21
}

Upvotes: 0

Views: 284

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24479

If you add "additionalProperties": false, any properties not declared in properties will be considered an error. In more complex cases, you might need "unevaluatedProperties": false instead, but that's not necessary in this case. The other option is to be explicit about what you fields you allow with "propertyNames": { "enum": ["firstName", "lastName", "aged"] }.

Upvotes: 2

Related Questions