Skip to content

Commit c577c85

Browse files
committed
Fix enum with negative values
1 parent 8771ee7 commit c577c85

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/compileValueSchema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,11 @@ function compileEnumableCheck(
987987
builders.ifStatement(
988988
schema.enum.reduce(
989989
(acc, val) => {
990-
const test = builders.binaryExpression('!==', value, builders.literal(val));
990+
// Handle negative numbers by creating a unary expression instead of a negative literal
991+
const literalValue = typeof val === 'number' && val < 0
992+
? builders.unaryExpression('-', builders.literal(-val))
993+
: builders.literal(val);
994+
const test = builders.binaryExpression('!==', value, literalValue);
991995

992996
if (!acc) {
993997
return test;

src/tests/__snapshots__/compileValueSchema.test.ts.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,3 +1254,44 @@ function obj0(path, value, context) {
12541254
return result;
12551255
}"
12561256
`;
1257+
1258+
exports[`Integer negative 1`] = `
1259+
"/**
1260+
Validate a request against the OpenAPI spec
1261+
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
1262+
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
1263+
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
1264+
*/
1265+
export function validateRequest(request, context) {
1266+
return new RequestError(404, 'no operation match path');
1267+
}
1268+
/**
1269+
Map of all components defined in the spec to their validation functions.
1270+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
1271+
*/
1272+
export const componentSchemas = {};
1273+
export class RequestError extends Error {
1274+
/** @param {number} code HTTP code for the error
1275+
@param {string} message The error message*/
1276+
constructor(code, message) {
1277+
super(message);
1278+
/** @type {number} HTTP code for the error*/
1279+
this.code = code;
1280+
}
1281+
}
1282+
export class ValidationError extends RequestError {
1283+
/** @param {string[]} path The path that failed validation
1284+
@param {string} message The error message*/
1285+
constructor(path, message) {
1286+
super(409, message);
1287+
/** @type {string[]} The path that failed validation*/
1288+
this.path = path;
1289+
}
1290+
}
1291+
function obj0(path, value, context) {
1292+
if (value !== -1 && value !== 0 && value !== 1) {
1293+
return new ValidationError(path, 'expected one of the enum value');
1294+
}
1295+
return value;
1296+
}"
1297+
`;

0 commit comments

Comments
 (0)