Skip to content

Commit

Permalink
Allow stringFormats to transform value (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse authored Nov 28, 2024
1 parent f24202a commit 4e94797
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/compileValidateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { annotateWithJSDocComment } from './comments';
const COMMENT = `
Validate a request against the OpenAPI spec
@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
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
`;

Expand Down
14 changes: 14 additions & 0 deletions src/compileValueSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,20 @@ function compileStringSchema(compiler: Compiler, schema: OpenAPIStringSchema) {
builders.blockStatement([builders.returnStatement(formatResult)]),
),
);
nodes.push(
builders.ifStatement(
builders.binaryExpression(
'===',
builders.unaryExpression('typeof', formatResult),
builders.literal('string'),
),
builders.blockStatement([
builders.expressionStatement(
builders.assignmentExpression('=', value, formatResult),
),
]),
),
);
}

if (schema.minLength) {
Expand Down
Loading

0 comments on commit 4e94797

Please sign in to comment.