Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

[JSON Schema] Express Time resource as custom type 'time' #23

Closed
coderbyheart opened this issue Jan 19, 2023 · 4 comments
Closed

[JSON Schema] Express Time resource as custom type 'time' #23

coderbyheart opened this issue Jan 19, 2023 · 4 comments
Labels

Comments

@coderbyheart
Copy link
Member

coderbyheart commented Jan 19, 2023

Time Unix Time. A signed integer
representing the number of seconds since
Jan 1st, 1970 in the UTC time zone.
Represented as an ASCII
integer.

For example, 1476186613
seconds since Jan 01 1970,
which represents Tuesday,
11-Oct-16 11:50:13 UTC,
are represented as the
ASCII string
"1476186613", which has
10 characters/bytes.

Same representation as Integer.

Page 96 http://www.openmobilealliance.org/release/lightweightm2m/V1_0-20170208-A/OMA-TS-LightweightM2M-V1_0-20170208-A.pdf

Example

add type: time, user-defined keywords are supported in AJV: https://ajv.js.org/keywords.html

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "$id": "https://github.com/NordicSemiconductor/lwm2m-types-js/blob/saga/LwM2MDocument.schema.json",
  "title": "LwM2M JSON Schema",
  "description": "JSON schema for expressing LwM2M resources as JSON",
  "properties": {
    "6": {
      "properties": {
        "5": {
          "type": "time",
          "minimum": 1000000000,
          "title": "Timestamp",
          "description": "The timestamp of when the location measurement was performed. Unix Time. A signed integer representing the number of seconds since Jan 1 st, 1970 in the UTC time zone."
        },
      },
    },
  },
}
@coderbyheart coderbyheart changed the title [JSON Schema] Express Time resource as custom type 'time [JSON Schema] Express Time resource as custom type 'time' Jan 19, 2023
@MLopezJ
Copy link
Contributor

MLopezJ commented Jan 20, 2023

How to represent date in Json Schema?

Using the keyword type as string and the format keyword as date-time

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "$id": "https://github.com/NordicSemiconductor/lwm2m-types-js/blob/saga/LwM2MDocument.schema.json",
  "title": "LwM2M JSON Schema",
  "description": "JSON schema for expressing LwM2M resources as JSON",
  "properties": {
    "6": {
      "properties": {
        "5": {
          "type": "string",
          "format": "date-time",
          "title": "Timestamp",
        },
      },
    },
  },
}

More info

What is the expected value according to LwM2M XML definition?

2 examples:

Object 6

<Item ID="5">
  <Name>Timestamp</Name>
  <Operations>R</Operations>
  <MultipleInstances>Single</MultipleInstances>
  <Mandatory>Mandatory</Mandatory>
  <Type>Time</Type>
  <RangeEnumeration></RangeEnumeration>
  <Units></Units>
  <Description><![CDATA[The timestamp of when the location measurement was performed.]]></Description>
</Item>

Object 3303

<Item ID="5518">
  <Name>Timestamp</Name>
  <Operations>R</Operations>
  <MultipleInstances>Single</MultipleInstances>
  <Mandatory>Optional</Mandatory>
  <Type>Time</Type>
  <RangeEnumeration></RangeEnumeration>
  <Units></Units>
  <Description>The timestamp of when the measurement was performed.</Description>
</Item>

What is the LwM2M definition of Time type ?

Unix Time. A signed integer representing the number of seconds since Jan 1st, 1970 in the UTC time zone.

LwM2M documentation pag 96

How does the provided value look like in the shadow?

Object 3303 prop 5518: Temperature

{
  "Timestamp": "2022-10-07T13:33:22Z"
}

Object 6, prop 5: Location

{
  "Timestamp": "1970-01-01T00:00:00Z",
}

Problem

The expected format according to the standard is UNIX TIME but provided value in shadow follows ISO 8601 date-time

@MLopezJ
Copy link
Contributor

MLopezJ commented Jan 20, 2023

How to represent UNIX TIME in Json Schema ?

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "$id": "https://github.com/NordicSemiconductor/lwm2m-types-js/blob/saga/LwM2MDocument.schema.json",
  "title": "LwM2M JSON Schema",
  "description": "JSON schema for expressing LwM2M resources as JSON",
  "properties": {
    "6": {
      "properties": {
        "5": {
          "type": "integer",
          "minimum": -864e12,
          "maximum": 864e12,
          "title": "Timestamp",
        },
      },
    },
  },
}

stackoverflow

Because UNIX TIME definition says: it is the number of seconds between a particular date and the Unix Epoch unixtimestamp

Other alternative could be add format keyword as date-time in object definition

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "$id": "https://github.com/NordicSemiconductor/lwm2m-types-js/blob/saga/LwM2MDocument.schema.json",
  "title": "LwM2M JSON Schema",
  "description": "JSON schema for expressing LwM2M resources as JSON",
  "properties": {
    "6": {
      "properties": {
        "5": {
          "type": "integer",
          "minimum": -864e12,
          "maximum": 864e12,
          "title": "Timestamp",
          "format": "date-time",
        },
      },
    },
  },
}

However, this is not a real solution because "format": "date-time" only work on string type and LwM2M described value as integer:

here is described another alternative using ajv, however that does not work neither because the expected value is UNIX TIME format and not ISO 8601 format.

@github-actions
Copy link

🎉 This issue has been resolved in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

coderbyheart added a commit that referenced this issue Jan 20, 2023
@coderbyheart
Copy link
Member Author

@MLopezJ

How does the provided value look like in the shadow?

This is not relevant for this project.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants