Skip to content

Commit f66c541

Browse files
committed
Use valibot for factory function validation
1 parent 21f940d commit f66c541

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

__tests__/html2/middleware/activity/invalidMiddleware.createdOutsideOfFactory.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
// THEN: console.warn() should have been called.
9292
expect(console.warn.mock.calls[0][0]).toBe(
93-
'botframework-webchat: activity.middleware must be created via factory function'
93+
'botframework-webchat: activity.middleware must be created using its factory function'
9494
);
9595
});
9696
</script>

packages/middleware/src/private/templatePolyMiddleware.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
type ProviderProps,
1212
type ProxyProps
1313
} from 'react-chain-of-responsibility/preview';
14-
import { array, function_, safeParse, type InferOutput } from 'valibot';
14+
import { array, check, function_, pipe, safeParse, type InferOutput } from 'valibot';
1515

1616
const arrayOfFunctionSchema = array(function_());
1717

@@ -35,6 +35,11 @@ function templatePolyMiddleware<Request, Props extends {}>(name: string) {
3535

3636
const middlewareFactoryMarker = Symbol();
3737

38+
const middlewareSchema = pipe(
39+
function_(),
40+
check(value => middlewareFactoryMarker in value)
41+
);
42+
3843
const createMiddleware = (enhancer: TemplatedEnhancer): TemplatedMiddleware => {
3944
const factory: TemplatedMiddleware = init => (init === name ? enhancer : BYPASS_ENHANCER);
4045

@@ -57,15 +62,15 @@ function templatePolyMiddleware<Request, Props extends {}>(name: string) {
5762
return Object.freeze(
5863
middleware
5964
.map(middleware => {
60-
if (!(middlewareFactoryMarker in middleware)) {
61-
console.warn(`botframework-webchat: ${name}.middleware must be created via factory function`);
65+
if (!safeParse(middlewareSchema, middleware).success) {
66+
console.warn(`botframework-webchat: ${name}.middleware must be created using its factory function`);
6267

6368
return false;
6469
}
6570

6671
const result = middleware(name);
6772

68-
if (typeof result !== 'function' && result) {
73+
if (typeof result !== 'function' && result !== false) {
6974
console.warn(`botframework-webchat: ${name}.middleware must return enhancer function or false`);
7075

7176
return false;

0 commit comments

Comments
 (0)