Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hxlnt opened this issue Jan 15, 2025 · 1 comment
Open

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

hxlnt opened this issue Jan 15, 2025 · 1 comment

Comments

@hxlnt
Copy link

hxlnt commented Jan 15, 2025

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.

@pateketrueke
Copy link
Member

Good catch, I think we should improve the regex used or the generation behavior, I'll see what to do asap, thank you!

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

No branches or pull requests

2 participants