GeorgeGeorgitsis
GeorgeGeorgitsis

Reputation: 1262

JSON schema validator in lambda python

In my AWS API endpoint i'm going to receive some JSON data and i need to validate them against a schema in my lambda function using python.

The data look a bit like this:

{
  "metadata": [
    {
      "fixedkey1": 1,
      "fixedkey2": 2
    }
  ],
  "UNIX_TIMESTAMP": [
    {
      "ONE_OUT_OF_10_PREDEFINED_VALUE": "21.5"
    },
    {
      "ONE_OUT_OF_10_PREDEFINED_VALUE": "5"
    }
  ],
  "ANOTHER_UNIX_TIMESTAMP": [
    {
      "ONE_OUT_OF_10_PREDEFINED_VALUE": "10"
    }
  ]
}

My problem is, how can i define the UNIX timestamps in the schema (it can be a lot of them in the object)?

Also how can i check that the ONE_OUT_OF_10_PREDEFINED_VALUE key is in a predefined list (for example one of TE,RI,KH etc.?

Upvotes: 1

Views: 1385

Answers (2)

Ether
Ether

Reputation: 53996

You can verify timestamps with the format keyword (just make sure that your particular implementation supports format validation and has it enabled) -- {"format": "datetime"}. You can check that a value is in a range of explicit possibilities with enum.

For property names specifically, use the propertyNames keyword (which validates the property string itself):

{
  "type": "object",
  "propertyNames": { "format": "datetime" },
  "additionalProperties": {
    .. schema for validating the value of the properties ...
  }
}

or

{
  "type": "object",
  "propertyNames": { "enum": [ "TE", "RI", "KH" ] },
  "additionalProperties": {
    .. schema for validating the value of the properties ...
  }
}

Upvotes: 0

Minek Po1
Minek Po1

Reputation: 162

  1. I would use regex.

RobertL provides this regex pattern for an Unix Unix time stamp.

  1. Use the in python key word.

Upvotes: 1

Related Questions