Skip to content

Commit

Permalink
Merge pull request #3 from GitbookIO/validate-value-as-string
Browse files Browse the repository at this point in the history
Accept string value as array
  • Loading branch information
gregberge authored Sep 19, 2024
2 parents 4bead5c + abc3ed6 commit 13b1bef
Show file tree
Hide file tree
Showing 7 changed files with 23,612 additions and 19,935 deletions.
Binary file modified bun.lockb
Binary file not shown.
28 changes: 27 additions & 1 deletion src/compileOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ export function compileOperation(
const parameter = compiler.resolveMaybeRef(refParameter);
const paramValueIdentifier = builders.identifier(`queryParam${index}`);
const resultIdentifier = builders.identifier(`queryParamResult${index}`);

const schemaFn = compileValueSchema(compiler, parameter.schema);

const isArrayType = 'type' in parameter.schema && parameter.schema.type === 'array';

// Assign the query parameter to a variable
nodes.push(
builders.variableDeclaration('const', [
builders.variableDeclaration('let', [
builders.variableDeclarator(
paramValueIdentifier,
builders.memberExpression(
Expand Down Expand Up @@ -179,6 +182,29 @@ export function compileOperation(
: [],
),
builders.blockStatement([
// If expected type is array, convert to array if it's a string
...(isArrayType
? [
builders.ifStatement(
// typeof paramValue === 'string'
builders.binaryExpression(
'===',
builders.unaryExpression('typeof', paramValueIdentifier),
builders.literal('string'),
),
builders.blockStatement([
// paramValue = [paramValue]
builders.expressionStatement(
builders.assignmentExpression(
'=',
paramValueIdentifier,
builders.arrayExpression([paramValueIdentifier]),
),
),
]),
),
]
: []),
// Validate the value
builders.variableDeclaration('const', [
builders.variableDeclarator(
Expand Down
4 changes: 2 additions & 2 deletions src/compileValidateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { annotateWithJSDocComment } from './comments';

const COMMENT = `
Validate a request against the OpenAPI spec
@param {{ method: string; path: string; body?: any; query: Record<string, string>; headers: Record<string, string>; }} request - Input request to validate
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | null } }} [context] - Context object to pass to validation functions
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string>; body?: any; headers: Record<string, string>; }}
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
`;

/**
Expand Down
Loading

0 comments on commit 13b1bef

Please sign in to comment.