Skip to content

Commit fa5ee10

Browse files
committed
Allow stringFormats to transform value
1 parent f24202a commit fa5ee10

5 files changed

+98
-24
lines changed

src/compileValidateRequest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { annotateWithJSDocComment } from './comments';
1010
const COMMENT = `
1111
Validate a request against the OpenAPI spec
1212
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
13-
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | null } }} [context] - Context object to pass to validation functions
13+
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
1414
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
1515
`;
1616

src/compileValueSchema.ts

+14
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,20 @@ function compileStringSchema(compiler: Compiler, schema: OpenAPIStringSchema) {
814814
builders.blockStatement([builders.returnStatement(formatResult)]),
815815
),
816816
);
817+
nodes.push(
818+
builders.ifStatement(
819+
builders.binaryExpression(
820+
'===',
821+
builders.unaryExpression('typeof', formatResult),
822+
builders.literal('string'),
823+
),
824+
builders.blockStatement([
825+
builders.expressionStatement(
826+
builders.assignmentExpression('=', value, formatResult),
827+
),
828+
]),
829+
),
830+
);
817831
}
818832

819833
if (schema.minLength) {

0 commit comments

Comments
 (0)