Skip to content

How to custom dayjs scheme #201

@seepine

Description

@seepine

I use fastify with swagger and fastify-type-provider-zod

My code

export const dayjsDateSchema = z
  .custom<Dayjs>((val: any) => dayjs(val).isValid() || val === null, {
    params: { expected: 'Dayjs', received: z.string() },
  })


  schema: {
    operationId: 'passkeyList',
    tags: ['oauth'],
    summary: 'Get current passkey list',
    response: {
      200: z.object({
        code: z.int().min(0).max(500),
        data: z.array(
          z.object({
            id: z.string(),
            displayName: z.string(),
            createAt: dayjsDateSchema,
            loginAt: dayjsDateSchema,
          }),
        ),
      }),
    },
  },

And the openapi json

"responses": {
          "200": {
            "description": "Default Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "code": {
                      "type": "integer",
                      "minimum": 0,
                      "maximum": 500
                    },
                    "data": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "displayName": {
                            "type": "string"
                          },

                          // this is empty
                          "createAt": {

                          },
                          "loginAt": {

                          }
                        },
                        "required": [
                          "id",
                          "displayName",
                          "createAt",
                          "loginAt"
                        ],
                        "additionalProperties": false
                      }
                    }
                  },
                  "required": [
                    "code",
                    "data"
                  ],
                  "additionalProperties": false
                }
              }
            }
          }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions