fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2275 from hey-api/fix/valibot-array-min-max-length

fix(valibot): properly handle array minLength and maxLength

authored by

Lubos and committed by
GitHub
63645d07 dedfeef3

+111 -83
+5
.changeset/bright-readers-thank.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix(valibot): properly handle array minLength and maxLength
+2 -1
packages/openapi-ts-tests/test/3.0.x.test.ts
··· 68 68 }, 69 69 { 70 70 config: createConfig({ 71 - input: 'array-items-one-of-length-1.json', 71 + input: 'array-items-one-of-length-1.yaml', 72 72 output: 'array-items-one-of-length-1', 73 + plugins: ['@hey-api/typescript', 'valibot', 'zod'], 73 74 }), 74 75 description: 75 76 'generates correct array when items are oneOf array with single item',
+2 -1
packages/openapi-ts-tests/test/3.1.x.test.ts
··· 68 68 }, 69 69 { 70 70 config: createConfig({ 71 - input: 'array-items-one-of-length-1.json', 71 + input: 'array-items-one-of-length-1.yaml', 72 72 output: 'array-items-one-of-length-1', 73 + plugins: ['@hey-api/typescript', 'valibot', 'zod'], 73 74 }), 74 75 description: 75 76 'generates correct array when items are oneOf array with single item',
+9
packages/openapi-ts-tests/test/__snapshots__/3.0.x/array-items-one-of-length-1/valibot.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + import * as v from 'valibot'; 4 + 5 + export const vBar = v.string(); 6 + 7 + export const vFoo = v.object({ 8 + foo: v.optional(v.pipe(v.array(vBar), v.minLength(1), v.maxLength(2147483647))) 9 + });
+9
packages/openapi-ts-tests/test/__snapshots__/3.0.x/array-items-one-of-length-1/zod.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + import { z } from 'zod'; 4 + 5 + export const zBar = z.string(); 6 + 7 + export const zFoo = z.object({ 8 + foo: z.array(zBar).min(1).max(2147483647).optional() 9 + });
+9
packages/openapi-ts-tests/test/__snapshots__/3.1.x/array-items-one-of-length-1/valibot.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + import * as v from 'valibot'; 4 + 5 + export const vBar = v.string(); 6 + 7 + export const vFoo = v.object({ 8 + foo: v.optional(v.pipe(v.array(vBar), v.minLength(1), v.maxLength(2147483647))) 9 + });
+9
packages/openapi-ts-tests/test/__snapshots__/3.1.x/array-items-one-of-length-1/zod.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + import { z } from 'zod'; 4 + 5 + export const zBar = z.string(); 6 + 7 + export const zFoo = z.object({ 8 + foo: z.array(zBar).min(1).max(2147483647).optional() 9 + });
+10 -5
packages/openapi-ts-tests/test/openapi-ts.config.ts
··· 37 37 // 'invalid', 38 38 // 'servers-entry.yaml', 39 39 // ), 40 - path: path.resolve(__dirname, 'spec', '2.0.x', 'full.yaml'), 40 + path: path.resolve( 41 + __dirname, 42 + 'spec', 43 + '3.1.x', 44 + 'array-items-one-of-length-1.yaml', 45 + ), 41 46 // path: path.resolve(__dirname, 'spec', 'v3-transforms.json'), 42 47 // path: path.resolve(__dirname, 'spec', 'v3.json'), 43 48 // path: 'http://localhost:4000/', ··· 136 141 enabled: false, 137 142 type: 'typescript+namespace', 138 143 }, 139 - // name: '@hey-api/typescript', 144 + name: '@hey-api/typescript', 140 145 // tree: true, 141 146 }, 142 147 { ··· 147 152 // client: false, 148 153 // include... 149 154 // instance: true, 150 - // name: '@hey-api/sdk', 155 + name: '@hey-api/sdk', 151 156 // operationId: false, 152 157 // responseStyle: 'data', 153 158 // throwOnError: true, ··· 193 198 definitions: 'z{{name}}Definition', 194 199 // exportFromIndex: true, 195 200 metadata: true, 196 - // name: 'valibot', 201 + name: 'valibot', 197 202 requests: { 198 203 // case: 'SCREAMING_SNAKE_CASE', 199 204 name: 'z{{name}}TestData', ··· 209 214 definitions: 'z{{name}}Definition', 210 215 // exportFromIndex: true, 211 216 // metadata: true, 212 - // name: 'zod', 217 + name: 'zod', 213 218 requests: { 214 219 // case: 'SCREAMING_SNAKE_CASE', 215 220 // name: 'z{{name}}TestData',
-31
packages/openapi-ts-tests/test/spec/3.0.x/array-items-one-of-length-1.json
··· 1 - { 2 - "openapi": "3.0.2", 3 - "info": { 4 - "title": "OpenAPI 3.0.2 array items oneOf length 1 example", 5 - "version": "1" 6 - }, 7 - "components": { 8 - "schemas": { 9 - "Foo": { 10 - "type": "object", 11 - "properties": { 12 - "foo": { 13 - "type": "array", 14 - "items": { 15 - "oneOf": [ 16 - { 17 - "$ref": "#/components/schemas/Bar" 18 - } 19 - ] 20 - }, 21 - "maxItems": 2147483647, 22 - "minItems": 1 23 - } 24 - } 25 - }, 26 - "Bar": { 27 - "type": "string" 28 - } 29 - } 30 - } 31 - }
+18
packages/openapi-ts-tests/test/spec/3.0.x/array-items-one-of-length-1.yaml
··· 1 + openapi: 3.0.2 2 + info: 3 + title: OpenAPI 3.0.2 array items oneOf length 1 example 4 + version: '1' 5 + components: 6 + schemas: 7 + Foo: 8 + type: object 9 + properties: 10 + foo: 11 + type: array 12 + items: 13 + oneOf: 14 + - $ref: '#/components/schemas/Bar' 15 + maxItems: 2147483647 16 + minItems: 1 17 + Bar: 18 + type: string
-31
packages/openapi-ts-tests/test/spec/3.1.x/array-items-one-of-length-1.json
··· 1 - { 2 - "openapi": "3.1.0", 3 - "info": { 4 - "title": "OpenAPI 3.1.0 array items oneOf length 1 example", 5 - "version": "1" 6 - }, 7 - "components": { 8 - "schemas": { 9 - "Foo": { 10 - "type": "object", 11 - "properties": { 12 - "foo": { 13 - "type": "array", 14 - "items": { 15 - "oneOf": [ 16 - { 17 - "$ref": "#/components/schemas/Bar" 18 - } 19 - ] 20 - }, 21 - "maxItems": 2147483647, 22 - "minItems": 1 23 - } 24 - } 25 - }, 26 - "Bar": { 27 - "type": "string" 28 - } 29 - } 30 - } 31 - }
+18
packages/openapi-ts-tests/test/spec/3.1.x/array-items-one-of-length-1.yaml
··· 1 + openapi: 3.1.0 2 + info: 3 + title: OpenAPI 3.1.0 array items oneOf length 1 example 4 + version: '1' 5 + components: 6 + schemas: 7 + Foo: 8 + type: object 9 + properties: 10 + foo: 11 + type: array 12 + items: 13 + oneOf: 14 + - $ref: '#/components/schemas/Bar' 15 + maxItems: 2147483647 16 + minItems: 1 17 + Bar: 18 + type: string
+20 -14
packages/openapi-ts/src/plugins/valibot/plugin.ts
··· 46 46 plugin: ValibotPlugin['Instance']; 47 47 schema: SchemaWithType<'array'>; 48 48 state: State; 49 - }): ts.CallExpression => { 49 + }): ts.Expression => { 50 50 const functionName = compiler.propertyAccessExpression({ 51 51 expression: identifiers.v, 52 52 name: identifiers.schemas.array, 53 53 }); 54 54 55 - let arrayExpression: ts.CallExpression | undefined; 55 + const pipes: Array<ts.CallExpression> = []; 56 56 57 57 if (!schema.items) { 58 - arrayExpression = compiler.callExpression({ 58 + const expression = compiler.callExpression({ 59 59 functionName, 60 60 parameters: [ 61 61 unknownTypeToValibotSchema({ ··· 65 65 }), 66 66 ], 67 67 }); 68 + pipes.push(expression); 68 69 } else { 69 70 schema = deduplicateSchema({ schema }); 70 71 ··· 79 80 }); 80 81 81 82 if (itemExpressions.length === 1) { 82 - arrayExpression = compiler.callExpression({ 83 + const expression = compiler.callExpression({ 83 84 functionName, 84 85 parameters: itemExpressions, 85 86 }); 87 + pipes.push(expression); 86 88 } else { 87 89 if (schema.logicalOperator === 'and') { 88 90 // TODO: parser - handle intersection ··· 94 96 // TODO: parser - handle union 95 97 // return compiler.typeArrayNode(compiler.typeUnionNode({ types: itemExpressions })); 96 98 97 - arrayExpression = compiler.callExpression({ 99 + const expression = compiler.callExpression({ 98 100 functionName, 99 101 parameters: [ 100 102 unknownTypeToValibotSchema({ ··· 104 106 }), 105 107 ], 106 108 }); 109 + pipes.push(expression); 107 110 } 108 111 } 109 112 110 113 if (schema.minItems === schema.maxItems && schema.minItems !== undefined) { 111 - arrayExpression = compiler.callExpression({ 114 + const expression = compiler.callExpression({ 112 115 functionName: compiler.propertyAccessExpression({ 113 - expression: arrayExpression, 116 + expression: identifiers.v, 114 117 name: identifiers.actions.length, 115 118 }), 116 119 parameters: [compiler.valueToExpression({ value: schema.minItems })], 117 120 }); 121 + pipes.push(expression); 118 122 } else { 119 123 if (schema.minItems !== undefined) { 120 - arrayExpression = compiler.callExpression({ 124 + const expression = compiler.callExpression({ 121 125 functionName: compiler.propertyAccessExpression({ 122 - expression: arrayExpression, 123 - name: compiler.identifier({ text: 'min' }), 126 + expression: identifiers.v, 127 + name: identifiers.actions.minLength, 124 128 }), 125 129 parameters: [compiler.valueToExpression({ value: schema.minItems })], 126 130 }); 131 + pipes.push(expression); 127 132 } 128 133 129 134 if (schema.maxItems !== undefined) { 130 - arrayExpression = compiler.callExpression({ 135 + const expression = compiler.callExpression({ 131 136 functionName: compiler.propertyAccessExpression({ 132 - expression: arrayExpression, 133 - name: compiler.identifier({ text: 'max' }), 137 + expression: identifiers.v, 138 + name: identifiers.actions.maxLength, 134 139 }), 135 140 parameters: [compiler.valueToExpression({ value: schema.maxItems })], 136 141 }); 142 + pipes.push(expression); 137 143 } 138 144 } 139 145 140 - return arrayExpression; 146 + return pipesToExpression(pipes); 141 147 }; 142 148 143 149 const booleanTypeToValibotSchema = ({