Skip to content

Generated duration strings are not valid about 30% of the time #837

Open
@hxlnt

Description

@hxlnt

Generated duration strings are often not valid.

To reproduce the issue:

Using https://json-schema-faker.js.org/ with the following schema, some results generated are valid and others are not.

{
  "properties": {
    "duration": {
      "type": "string",
      "format": "duration"
    }
  },
  "required": [
    "duration"
  ],
  "additionalProperties": false
}

Clicking "Generate" 10 times yielded the following results--7 valid durations and 3 non-valid durations:

{
  "duration": "P78187143M"
}
{
  "duration": "P35882228342MT"
}
{
  "duration": "PW"
}
{
  "duration": "P05Y6D"
}
{
  "duration": "P15267M03035361D"
}
{
  "duration": "P26Y411240M"
}
{
  "duration": "P41Y7252451M9DT2590M"
}
{
  "duration": "P"
}
{
  "duration": "P69Y251M188D"
}
{
  "duration": "PT26540H"
}

Here is a comparison of the Regex used in

duration: '^P(?!$)((\\d+Y)?(\\d+M)?(\\d+D)?(T(?=\\d)(\\d+H)?(\\d+M)?(\\d+S)?)?|(\\d+W)?)$',
against these results. As you can see, duration strings ending in T are not valid as well as strings without any duration values (e.g., P and PW)

Screenshot 2025-01-15 at 10 58 44 AM

I discovered this issue via Prism, where API response validation checks are failing against generated data. I initially logged stoplightio/prism#2650 against Prism, but it looks like this is actually a bug on json-schema-faker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions