elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.6k stars 226 forks source link

Validation Error: "startTime" Property Fails to Match Expected "time" Format #899

Open K4leri opened 3 weeks ago

K4leri commented 3 weeks ago

What version of Elysia is running?

"elysia": "^1.1.23"

What platform is your computer?

Microsoft Windows NT 10.0.19045.0 x64

What steps can reproduce the bug?

he schema defines the timePeriod type as a string with a format of time, which is expected to be in the format HH:MM:SS. The error message returned is: "Expected string to match 'time' format".

The issue occurs consistently when providing values for startTime in the format HH:MM:SS.

const timePeriod = t.String({
  format: "time",
  description: "Time in HH:MM:SS format",
  examples: ["08:00:00", "12:00:00"],
});

export const getFilteredActivitiesSchema = t.Object({
  startTime: t.Optional(timePeriod),
  endTime: t.Optional(timePeriod),
  limit: t.Optional(t.Number({ minimum: 1, maximum: 50, default: 50 })),
  offset: t.Optional(t.Numeric({ default: 0 })),
});

new Elysia().get("/hi", ({ query }) => query, {
  query: getFilteredActivitiesSchema,
});

What is the expected behavior?

When providing a value for the startTime property in the query, it should be validated against the expected format of HH:MM:SS (24-hour time format). If the value matches this format, the validation should pass, and the query should be processed successfully.

What do you see instead?

{
  "type": "validation",
  "on": "query",
  "summary": "Property 'startTime' should be time",
  "property": "/startTime",
  "message": "Expected string to match 'time' format",
  "expected": {},
  "found": {
    "startTime": "10:30:00",
    "limit": 50,
    "offset": 0,
  },
  "errors": [
    {
      "type": 50,
      "schema": {
        "format": "time",
        "description": "Time in HH:MM:SS format",
        "examples": [
          "08:00:00",
          "12:00:00"
        ],
        "type": "string"
      },
      "path": "/startTime",
      "value": "10:30:00",
      "message": "Expected string to match 'time' format",
      "summary": "Property 'startTime' should be time"
    }
  ]
}

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

no

lteacher commented 3 weeks ago

It looks to me like the code copies the logic from typebox, which takes the requirement from AJV that the time value must have a timezone. Obviously that doesn't align with ISO8601 though... I thought it would be fun to fix it but since that is done on purpose I dunno what the authors would like to do about that as fixing it involves dealing with the strictTimeZone argument as whilst true included for the time value the !tz check will fail.

I also wasn't convinced the regex was correct per the spec if this is supposed to match JSON schema since the regex does allow some exclusion of timezone minutes.

AJV Docs that says the timezone is required https://ajv.js.org/packages/ajv-formats.html#formats

Codebase issue location (strictTimeZone is always true) https://github.com/elysiajs/elysia/blob/main/src/formats.ts#L125

JSON Schema ref: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6