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

[@kubb/plugin-faker]: Min and max is not applied to the faker functions #1498

Closed
ksilverhav opened this issue Jan 8, 2025 · 1 comment
Closed
Assignees
Labels
bug Something isn't working plugin-faker

Comments

@ksilverhav
Copy link

What version of kubb is running?

3.4.0

What kind of platform do you use?

MacOS

How does your kubb.config.ts config look like

import { defineConfig } from "@kubb/core";
import { pluginOas } from "@kubb/plugin-oas";
import { pluginFaker } from "@kubb/plugin-faker";
import { pluginZod } from "@kubb/plugin-zod";
import { pluginTs } from "@kubb/plugin-ts";

export default defineConfig({
  input: {
    path: "./src/openapi/test.yaml",
  },
  output: {
    path: "./src/generated",
  },
  plugins: [pluginOas(), pluginTs(), pluginZod(), pluginFaker()],
});

Swagger/OpenAPI file?

openapi: 3.0.3
info:
  title: Test
  version: 1.0.0
paths:
  /item:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Item"
components:
  schemas:
    Item:
      type: object
      properties:
        name:
          type: string
          minLength: 3
          maxLength: 25
        price:
          type: number
          minimum: 1
      required:
        - name
        - price

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

"@kubb/cli": "^3.4.0",
"@kubb/core": "^3.4.0",
"@kubb/plugin-faker": "^3.4.0",
"@kubb/plugin-oas": "^3.4.0",
"@kubb/plugin-ts": "^3.4.0",
"@kubb/plugin-zod": "^3.4.0",

What steps can reproduce the bug?

Generate the code and look at the generated file createItem.ts, it contains:

export function createItem(data?: Partial<Item>) {
  return {
    ...{ name: faker.string.alpha(), price: faker.number.float() },
    ...(data || {}),
  }
}

And then compare it to itemSchema.ts:

export const itemSchema = z.object({
  name: z.string().min(3).max(25),
  price: z.number().min(1),
})

Notice that zod includes the min and max values while faker doesn't. This can cause a generated faker object not being correctly validated by zod.

How often does this bug happen?

Every time

What is the expected behavior?

Expected behavior: plugin-faker should take into account min and max values.
Expect the code to be something similar to this:

export function createItem(data?: Partial<Item>) {
  return {
    ...{ name: faker.string.alpha({length: {min: 3, max: 25}}), price: faker.number.float({min: 1}) },
    ...(data || {}),
  }
}

Additional information

It seems to be working for arrays when minItems and maxItems are defined in the api specification

@ksilverhav ksilverhav added the bug Something isn't working label Jan 8, 2025
@stijnvanhulle stijnvanhulle self-assigned this Jan 8, 2025
@stijnvanhulle
Copy link
Collaborator

v3.4.1 of Kubb will resolve this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working plugin-faker
Projects
None yet
Development

No branches or pull requests

2 participants