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

fix: add naive lower and upper bound checks

Lubos aaece238 6d2a873a

Changed files
+125 -64
packages
openapi-ts
src
plugins
valibot
v1
zod
mini
toAst
v3
toAst
v4
toAst
+33 -17
packages/openapi-ts/src/plugins/valibot/v1/toAst/number.ts
··· 51 51 } 52 52 } 53 53 54 - const integerLimit = getIntegerLimit(schema.format); 55 - if (integerLimit) { 56 - pipes.push( 57 - $(v) 58 - .attr(identifiers.actions.minValue) 59 - .call( 60 - maybeBigInt(integerLimit.minValue, schema.format), 61 - $.literal(integerLimit.minError), 62 - ), 63 - $(v) 64 - .attr(identifiers.actions.maxValue) 65 - .call( 66 - maybeBigInt(integerLimit.maxValue, schema.format), 67 - $.literal(integerLimit.maxError), 68 - ), 69 - ); 70 - } 54 + let hasLowerBound = false; 55 + let hasUpperBound = false; 71 56 72 57 if (schema.exclusiveMinimum !== undefined) { 73 58 pipes.push( ··· 75 60 .attr(identifiers.actions.gtValue) 76 61 .call(maybeBigInt(schema.exclusiveMinimum, schema.format)), 77 62 ); 63 + hasLowerBound = true; 78 64 } else if (schema.minimum !== undefined) { 79 65 pipes.push( 80 66 $(v) 81 67 .attr(identifiers.actions.minValue) 82 68 .call(maybeBigInt(schema.minimum, schema.format)), 83 69 ); 70 + hasLowerBound = true; 84 71 } 85 72 86 73 if (schema.exclusiveMaximum !== undefined) { ··· 89 76 .attr(identifiers.actions.ltValue) 90 77 .call(maybeBigInt(schema.exclusiveMaximum, schema.format)), 91 78 ); 79 + hasUpperBound = true; 92 80 } else if (schema.maximum !== undefined) { 93 81 pipes.push( 94 82 $(v) 95 83 .attr(identifiers.actions.maxValue) 96 84 .call(maybeBigInt(schema.maximum, schema.format)), 97 85 ); 86 + hasUpperBound = true; 87 + } 88 + 89 + const integerLimit = getIntegerLimit(schema.format); 90 + if (integerLimit) { 91 + if (!hasLowerBound) { 92 + pipes.push( 93 + $(v) 94 + .attr(identifiers.actions.minValue) 95 + .call( 96 + maybeBigInt(integerLimit.minValue, schema.format), 97 + $.literal(integerLimit.minError), 98 + ), 99 + ); 100 + hasLowerBound = true; 101 + } 102 + 103 + if (!hasUpperBound) { 104 + pipes.push( 105 + $(v) 106 + .attr(identifiers.actions.maxValue) 107 + .call( 108 + maybeBigInt(integerLimit.maxValue, schema.format), 109 + $.literal(integerLimit.maxError), 110 + ), 111 + ); 112 + hasUpperBound = true; 113 + } 98 114 } 99 115 100 116 return pipesToAst(pipes, plugin);
+1 -2
packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts
··· 52 52 return $(v).attr(identifiers.schemas.literal).call($.literal(schema.const)); 53 53 } 54 54 55 - const pipes: Array<ReturnType<typeof $.call>> = []; 56 - pipes.push($(v).attr(identifiers.schemas.string).call()); 55 + const pipes = [$(v).attr(identifiers.schemas.string).call()]; 57 56 58 57 if (schema.format) { 59 58 const args: FormatResolverArgs = { $, pipes, plugin, schema };
+33 -17
packages/openapi-ts/src/plugins/zod/mini/toAst/number.ts
··· 43 43 44 44 const checks: Array<ReturnType<typeof $.call>> = []; 45 45 46 - const integerLimit = getIntegerLimit(schema.format); 47 - if (integerLimit) { 48 - checks.push( 49 - $(z) 50 - .attr(identifiers.minimum) 51 - .call( 52 - maybeBigInt(integerLimit.minValue, schema.format), 53 - $.object().prop('error', $.literal(integerLimit.minError)), 54 - ), 55 - $(z) 56 - .attr(identifiers.maximum) 57 - .call( 58 - maybeBigInt(integerLimit.maxValue, schema.format), 59 - $.object().prop('error', $.literal(integerLimit.maxError)), 60 - ), 61 - ); 62 - } 46 + let hasLowerBound = false; 47 + let hasUpperBound = false; 63 48 64 49 if (schema.exclusiveMinimum !== undefined) { 65 50 checks.push( ··· 67 52 .attr(identifiers.gt) 68 53 .call(maybeBigInt(schema.exclusiveMinimum, schema.format)), 69 54 ); 55 + hasLowerBound = true; 70 56 } else if (schema.minimum !== undefined) { 71 57 checks.push( 72 58 $(z) 73 59 .attr(identifiers.gte) 74 60 .call(maybeBigInt(schema.minimum, schema.format)), 75 61 ); 62 + hasLowerBound = true; 76 63 } 77 64 78 65 if (schema.exclusiveMaximum !== undefined) { ··· 81 68 .attr(identifiers.lt) 82 69 .call(maybeBigInt(schema.exclusiveMaximum, schema.format)), 83 70 ); 71 + hasUpperBound = true; 84 72 } else if (schema.maximum !== undefined) { 85 73 checks.push( 86 74 $(z) 87 75 .attr(identifiers.lte) 88 76 .call(maybeBigInt(schema.maximum, schema.format)), 89 77 ); 78 + hasUpperBound = true; 79 + } 80 + 81 + const integerLimit = getIntegerLimit(schema.format); 82 + if (integerLimit) { 83 + if (!hasLowerBound) { 84 + checks.push( 85 + $(z) 86 + .attr(identifiers.minimum) 87 + .call( 88 + maybeBigInt(integerLimit.minValue, schema.format), 89 + $.object().prop('error', $.literal(integerLimit.minError)), 90 + ), 91 + ); 92 + hasLowerBound = true; 93 + } 94 + 95 + if (!hasUpperBound) { 96 + checks.push( 97 + $(z) 98 + .attr(identifiers.maximum) 99 + .call( 100 + maybeBigInt(integerLimit.maxValue, schema.format), 101 + $.object().prop('error', $.literal(integerLimit.maxError)), 102 + ), 103 + ); 104 + hasUpperBound = true; 105 + } 90 106 } 91 107 92 108 if (checks.length) {
+29 -14
packages/openapi-ts/src/plugins/zod/v3/toAst/number.ts
··· 41 41 } 42 42 } 43 43 44 - const integerLimit = getIntegerLimit(schema.format); 45 - if (integerLimit) { 46 - numberExpression = numberExpression 47 - .attr(identifiers.min) 48 - .call( 49 - maybeBigInt(integerLimit.minValue, schema.format), 50 - $.object().prop('message', $.literal(integerLimit.minError)), 51 - ) 52 - .attr(identifiers.max) 53 - .call( 54 - maybeBigInt(integerLimit.maxValue, schema.format), 55 - $.object().prop('message', $.literal(integerLimit.maxError)), 56 - ); 57 - } 44 + let hasLowerBound = false; 45 + let hasUpperBound = false; 58 46 59 47 if (schema.exclusiveMinimum !== undefined) { 60 48 numberExpression = numberExpression 61 49 .attr(identifiers.gt) 62 50 .call(maybeBigInt(schema.exclusiveMinimum, schema.format)); 51 + hasLowerBound = true; 63 52 } else if (schema.minimum !== undefined) { 64 53 numberExpression = numberExpression 65 54 .attr(identifiers.gte) 66 55 .call(maybeBigInt(schema.minimum, schema.format)); 56 + hasLowerBound = true; 67 57 } 68 58 69 59 if (schema.exclusiveMaximum !== undefined) { 70 60 numberExpression = numberExpression 71 61 .attr(identifiers.lt) 72 62 .call(maybeBigInt(schema.exclusiveMaximum, schema.format)); 63 + hasUpperBound = true; 73 64 } else if (schema.maximum !== undefined) { 74 65 numberExpression = numberExpression 75 66 .attr(identifiers.lte) 76 67 .call(maybeBigInt(schema.maximum, schema.format)); 68 + hasUpperBound = true; 69 + } 70 + 71 + const integerLimit = getIntegerLimit(schema.format); 72 + if (integerLimit) { 73 + if (!hasLowerBound) { 74 + numberExpression = numberExpression 75 + .attr(identifiers.min) 76 + .call( 77 + maybeBigInt(integerLimit.minValue, schema.format), 78 + $.object().prop('message', $.literal(integerLimit.minError)), 79 + ); 80 + hasLowerBound = true; 81 + } 82 + 83 + if (!hasUpperBound) { 84 + numberExpression = numberExpression 85 + .attr(identifiers.max) 86 + .call( 87 + maybeBigInt(integerLimit.maxValue, schema.format), 88 + $.object().prop('message', $.literal(integerLimit.maxError)), 89 + ); 90 + hasUpperBound = true; 91 + } 77 92 } 78 93 79 94 return numberExpression;
+29 -14
packages/openapi-ts/src/plugins/zod/v4/toAst/number.ts
··· 41 41 } 42 42 } 43 43 44 - const integerLimit = getIntegerLimit(schema.format); 45 - if (integerLimit) { 46 - result.expression = result.expression 47 - .attr(identifiers.min) 48 - .call( 49 - maybeBigInt(integerLimit.minValue, schema.format), 50 - $.object().prop('error', $.literal(integerLimit.minError)), 51 - ) 52 - .attr(identifiers.max) 53 - .call( 54 - maybeBigInt(integerLimit.maxValue, schema.format), 55 - $.object().prop('error', $.literal(integerLimit.maxError)), 56 - ); 57 - } 44 + let hasLowerBound = false; 45 + let hasUpperBound = false; 58 46 59 47 if (schema.exclusiveMinimum !== undefined) { 60 48 result.expression = result.expression 61 49 .attr(identifiers.gt) 62 50 .call(maybeBigInt(schema.exclusiveMinimum, schema.format)); 51 + hasLowerBound = true; 63 52 } else if (schema.minimum !== undefined) { 64 53 result.expression = result.expression 65 54 .attr(identifiers.gte) 66 55 .call(maybeBigInt(schema.minimum, schema.format)); 56 + hasLowerBound = true; 67 57 } 68 58 69 59 if (schema.exclusiveMaximum !== undefined) { 70 60 result.expression = result.expression 71 61 .attr(identifiers.lt) 72 62 .call(maybeBigInt(schema.exclusiveMaximum, schema.format)); 63 + hasUpperBound = true; 73 64 } else if (schema.maximum !== undefined) { 74 65 result.expression = result.expression 75 66 .attr(identifiers.lte) 76 67 .call(maybeBigInt(schema.maximum, schema.format)); 68 + hasUpperBound = true; 69 + } 70 + 71 + const integerLimit = getIntegerLimit(schema.format); 72 + if (integerLimit) { 73 + if (!hasLowerBound) { 74 + result.expression = result.expression 75 + .attr(identifiers.min) 76 + .call( 77 + maybeBigInt(integerLimit.minValue, schema.format), 78 + $.object().prop('error', $.literal(integerLimit.minError)), 79 + ); 80 + hasLowerBound = true; 81 + } 82 + 83 + if (!hasUpperBound) { 84 + result.expression = result.expression 85 + .attr(identifiers.max) 86 + .call( 87 + maybeBigInt(integerLimit.maxValue, schema.format), 88 + $.object().prop('error', $.literal(integerLimit.maxError)), 89 + ); 90 + hasUpperBound = true; 91 + } 77 92 } 78 93 79 94 return result as Omit<Ast, 'typeName'>;