Skip to content

Commit

Permalink
fix: min and max was not applied to the faker functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Jan 8, 2025
1 parent 9b33d72 commit e10b59a
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-baboons-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-faker": patch
---

min and max was not applied to the faker functions
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: Changelog

# Changelog

## 3.4.1
- [`plugin-faker`](/plugins/plugin-faker): min and max was not applied to the faker functions

## 3.4.0
- [`plugin-client`](/plugins/plugin-client): decouple URI (with params) from fetching
- [`plugin-client`](/plugins/plugin-client): add header in response object
Expand Down
2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/react-query": "^5.62.16",
"@tanstack/react-query": "^5.63.0",
"@tanstack/solid-query": "^5.62.16",
"@tanstack/svelte-query": "^5.62.16",
"@tanstack/vue-query": "^5.62.16",
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/query-core": "^5.62.16",
"@tanstack/react-query": "^5.62.16",
"@tanstack/react-query": "^5.63.0",
"@tanstack/solid-query": "^5.62.16",
"@tanstack/svelte-query": "^5.62.16",
"@tanstack/vue-query": "^5.62.16",
Expand Down
2 changes: 2 additions & 0 deletions examples/advanced/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ components:
type: integer
format: int64
example: 10
minLength: 3
maxLength: 100
petId:
type: integer
format: int64
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import client from '../../../../axios-client.ts'
import type { RequestConfig, ResponseErrorConfig } from '../../../../axios-client.ts'
import type { AddPet405, AddPetMutationRequest, AddPetMutationResponse } from '../../../models/ts/petController/AddPet.ts'
import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from '../../../models/ts/petController/AddPet.ts'

export function getAddPetUrl() {
return new URL('/pet', 'https://petstore3.swagger.io/api/v3')
Expand Down
9 changes: 6 additions & 3 deletions examples/advanced/src/gen/docs.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/advanced/src/gen/mocks/createOrderFaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { faker } from '@faker-js/faker'
export function createOrderFaker(data?: Partial<Order>) {
return {
...{
id: faker.number.int(),
id: faker.number.int({ min: 3, max: 100 }),
petId: faker.number.int(),
quantity: faker.number.int(),
orderType: faker.helpers.arrayElement<any>(['foo', 'bar']),
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/mocks/tag/createTagFaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { faker } from '@faker-js/faker'

export function createTagTagFaker(data?: Partial<TagTag>) {
return {
...{ id: faker.number.int(), name: faker.string.alpha() },
...{ id: faker.number.int({ min: 5, max: 7 }), name: faker.string.alpha() },
...(data || {}),
}
}
2 changes: 2 additions & 0 deletions examples/advanced/src/gen/models/ts/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type OrderHttpStatusEnum = (typeof orderHttpStatusEnum)[keyof typeof orde

export type Order = {
/**
* @minLength 3
* @maxLength 100
* @type integer | undefined, int64
*/
id?: number
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/schemas/order.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "object",
"properties": {
"id": { "type": "integer", "format": "int64", "example": 10 },
"id": { "type": "integer", "format": "int64", "example": 10, "minLength": 3, "maxLength": 100 },
"petId": { "type": "integer", "format": "int64", "example": 198772 },
"quantity": { "type": "integer", "format": "int32", "example": 7 },
"orderType": { "type": "string", "enum": ["foo", "bar"] },
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/schemas2/order.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "object",
"properties": {
"id": { "type": "integer", "format": "int64", "example": 10 },
"id": { "type": "integer", "format": "int64", "example": 10, "minLength": 3, "maxLength": 100 },
"petId": { "type": "integer", "format": "int64", "example": 198772 },
"quantity": { "type": "integer", "format": "int32", "example": 7 },
"orderType": { "type": "string", "enum": ["foo", "bar"] },
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/orderSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ToZod } from '@kubb/plugin-zod/utils'
import { z } from 'zod'

export const orderSchema = z.object({
id: z.number().int().optional(),
id: z.number().int().min(3).max(100).optional(),
petId: z.number().int().optional(),
quantity: z.number().int().optional(),
orderType: z.enum(['foo', 'bar']).optional(),
Expand Down
4 changes: 2 additions & 2 deletions examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"@kubb/plugin-react-query": "workspace:*",
"@kubb/plugin-ts": "workspace:*",
"@kubb/react": "workspace:*",
"@tanstack/react-query": "^5.62.16",
"@tanstack/react-query-devtools": "^5.62.16",
"@tanstack/react-query": "^5.63.0",
"@tanstack/react-query-devtools": "^5.63.0",
"axios": "^1.7.9",
"react": "^18.3.1",
"react-dom": "^19.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-single/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@kubb/plugin-redoc": "workspace:*",
"@kubb/plugin-ts": "workspace:*",
"@kubb/plugin-zod": "workspace:*",
"@tanstack/react-query": "^5.62.16",
"@tanstack/react-query": "^5.63.0",
"axios": "^1.7.9",
"react": "^18.3.1",
"tsup": "^8.3.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-faker/src/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ exports[`faker parse > 'nullableAdditionalProperties' 1`] = `"{}"`;
exports[`faker parse > 'number' 1`] = `"faker.number.float()"`;
exports[`faker parse > 'object' 1`] = `"{"firstName": faker.string.alpha(),"address": faker.string.alpha()}"`;
exports[`faker parse > 'object' 1`] = `"{"firstName": faker.string.alpha({ length: { min: 2 } }),"address": faker.string.alpha()}"`;
exports[`faker parse > 'objectAnd' 1`] = `"{"firstName": faker.string.alpha(),"age": faker.number.float(),"address": Object.assign({}, faker.string.alpha(), faker.number.float())}"`;
exports[`faker parse > 'objectAnd' 1`] = `"{"firstName": faker.string.alpha({ length: { min: 2 } }),"age": faker.number.float({ min: 3 }),"address": Object.assign({}, faker.string.alpha(), faker.number.float())}"`;
exports[`faker parse > 'objectArray' 1`] = `"{"ids": faker.helpers.multiple(() => (faker.helpers.fromRegExp(new RegExp('^[a-zA-Z0-9]{1,13}$'))), { count: { min: 3, max: 10 }}) as any}"`;
Expand All @@ -60,7 +60,7 @@ exports[`faker parse > 'objectEmpty' 1`] = `"{}"`;
exports[`faker parse > 'objectEnum' 1`] = `"{"version": faker.helpers.arrayElement<any>(["A", "B", "C", 2])}"`;
exports[`faker parse > 'objectOptional' 1`] = `"{"firstName": faker.string.alpha(),"address": faker.string.alpha()}"`;
exports[`faker parse > 'objectOptional' 1`] = `"{"firstName": faker.string.alpha({ length: { min: 2 } }),"address": faker.string.alpha()}"`;
exports[`faker parse > 'primitiveDate' 1`] = `"faker.date.anytime()"`;
Expand Down
20 changes: 10 additions & 10 deletions packages/plugin-faker/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export function parse({ parent, current, siblings }: SchemaTree, options: Parser
return `"${name}": ${joinItems(
schemas
.sort(schemaKeywordSorter)
.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false }))
.map((schema) => parse({ parent: current, current: schema, siblings: schemas }, { ...options, canOverride: false }))
.filter(Boolean),
)}`
})
Expand Down Expand Up @@ -291,9 +291,9 @@ export function parse({ parent, current, siblings }: SchemaTree, options: Parser
}

if (isKeyword(current, schemaKeywords.string)) {
if (parent) {
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min)
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max)
if (siblings) {
const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)

return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args)
}
Expand All @@ -302,9 +302,9 @@ export function parse({ parent, current, siblings }: SchemaTree, options: Parser
}

if (isKeyword(current, schemaKeywords.number)) {
if (parent) {
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min)
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max)
if (siblings) {
const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)

return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args)
}
Expand All @@ -313,9 +313,9 @@ export function parse({ parent, current, siblings }: SchemaTree, options: Parser
}

if (isKeyword(current, schemaKeywords.integer)) {
if (parent) {
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min)
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max)
if (siblings) {
const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)

return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ts/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export function parse({ parent, current, siblings }: SchemaTree, options: Parser
const maxSchema = schemas.find((schema) => schema.keyword === schemaKeywords.max) as SchemaKeywordMapper['max'] | undefined
const matchesSchema = schemas.find((schema) => schema.keyword === schemaKeywords.matches) as SchemaKeywordMapper['matches'] | undefined

let type = schemas.map((schema) => parse({ parent: current, current: schema, siblings }, options)).filter(Boolean)[0] as ts.TypeNode
let type = schemas.map((schema) => parse({ parent: current, current: schema, siblings: schemas }, options)).filter(Boolean)[0] as ts.TypeNode

if (isNullable) {
type = factory.createUnionDeclaration({
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-zod/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export function parse({ parent, current, siblings }: SchemaTree, options: Parser
}

return `"${name}": ${sort(schemas)
.map((schema, array, siblings) => parse({ parent: current, current: schema, siblings }, options))
.map((schema, array, siblings) => parse({ parent: current, current: schema, siblings: schemas }, options))
.filter(Boolean)
.join('')}`
})
Expand Down
38 changes: 19 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e10b59a

Please sign in to comment.