railtoolkit / schema

This repo collects the descriptions of the structure and the validation constraints of tools in the railtoolkit in JSON schemas. It is, therefore, an alternative to RailML.
ISC License
1 stars 0 forks source link

mileage as key for running path #21

Open kaat0 opened 1 month ago

kaat0 commented 1 month ago

@gwehrle thought about a slighty different approach to have the position as the key for the following attributes like this:

    characteristic_sections:
       0.0:
          speed:      1
          resistance: 0.00
       1.0:
          speed:      2
          resistance: 0.00

Originally posted by @gwehrle in https://github.com/railtoolkit/schema/issues/19#issuecomment-2222383255

kaat0 commented 1 month ago

Mileage as key needs to be a string and an object for a valid JSONschema (see json schema: object). The "uniqueItems": true specification can only be used for arrays, not objects.

I came up with the following Schema to make mileage as key possible:

"characteristic_sections": {
  "type": "object",
  "patternProperties": {
    "([+-]?[0-9]{0,3}?[_]?[0-9]{0,3}?[_]?[0-9]{0,3}[.][0-9]{0,3})": {
      "type": "object",
      "properties": {
        "speed": {
          "description": "speed in kilometers per hour",
          "type": "number",
          "exclusiveMinimum": 0
        },
        "resistance": {
          "description": "resistance in permil",
          "type": "number"
        }
      }
    }
  }
},

The regex states that at least a decimal point is needed and the underscore _ can be used as digit separator for 3 digits.

Now the YAML file can look like:

schema: https://railtoolkit.org/schema/running-path.json
schema_version: "2024.07"
paths:
  - id: example
    description: "Example"
    characteristic_sections:
      0_000.0:  # mileage in meter
        speed:      160  # speed limit in km/h,
        resistance: 0.00 # resistance in permil
      1_000.0:
        resistance: 1.00
      4_0000.0x:
        resistance: -3.00
      567098:

Unfortunately, this is a valid file with npm run validate:running-path doc/running-path.example.yaml and it should not be valid according to the regex!

kaat0 commented 1 month ago

see mileage-as-key branch