Skip to content

Commit

Permalink
fix: [#4545] Please upgrade zod package - botbuilder-core (#4562)
Browse files Browse the repository at this point in the history
* updates zod version in botbuilder-core

* update support of compat tests

* update botbuilder-core api signature

* update bf-schema api signature

---------

Co-authored-by: JhontSouth <[email protected]>
  • Loading branch information
sw-joelmut and JhontSouth authored Nov 9, 2023
1 parent ecb77e1 commit 45d7dda
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion libraries/botbuilder-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"lodash": "^4.17.21",
"node-fetch": "^2.6.7",
"url-parse": "^1.5.9",
"zod": "~1.11.17"
"zod": "^3.22.4"
},
"resolutions": {
"follow-redirects": "1.14.7"
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-ai/src/luisRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export interface LuisRecognizerOptionsV2 extends LuisRecognizerOptions {
// This is just meant to operate as a simple type assertion.
const UnsafeLuisRecognizerUnion = z.custom<LuisRecognizerOptionsV3 | LuisRecognizerOptionsV2 | LuisPredictionOptions>(
(val: unknown): val is LuisRecognizerOptionsV3 | LuisRecognizerOptionsV2 | LuisPredictionOptions =>
z.record(z.unknown()).check(val),
z.record(z.unknown()).safeParse(val).success,
{
message: 'LuisRecognizerOptionsV3 | LuisRecognizerOptionsV2 | LuisPredictionOptions',
}
Expand Down
15 changes: 7 additions & 8 deletions libraries/botbuilder-ai/src/luisRecognizerOptionsV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,13 @@ export class LuisRecognizerV3 extends LuisRecognizerInternal {
const values: unknown[] = Array.isArray(value) ? value : [];
if (instances?.length === values?.length) {
instances.forEach((childInstance) => {
if (
z
.object({ startIndex: z.number(), endIndex: z.number() })
.nonstrict()
.check(childInstance)
) {
const start = childInstance.startIndex;
const end = childInstance.endIndex;
const childInstanceParsed = z
.object({ startIndex: z.number(), endIndex: z.number() })
.nonstrict()
.safeParse(childInstance);
if (childInstanceParsed.success) {
const start = childInstanceParsed.data.startIndex;
const end = childInstanceParsed.data.endIndex;
externalEntities.push({
entityName: key,
startIndex: start,
Expand Down
12 changes: 8 additions & 4 deletions libraries/botbuilder-ai/src/qnaMakerDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,21 @@ export class QnAMakerDialog extends WaterfallDialog implements QnAMakerDialogCon
if (top) {
this.top = new IntExpression(top);
}

if (qnaSuggestionsActivityFactory.check(activeLearningTitleOrFactory)) {
const qnaSuggestionsActivityFactoryParsed = qnaSuggestionsActivityFactory.safeParse(
activeLearningTitleOrFactory
);
if (qnaSuggestionsActivityFactoryParsed.success) {
if (!cardNoMatchText) {
// Without a developer-provided cardNoMatchText, the end user will not be able to tell the convey to the bot and QnA Maker that the suggested alternative questions were not correct.
// When the user's reply to a suggested alternatives Activity matches the cardNoMatchText, the QnAMakerDialog sends this information to the QnA Maker service for active learning.
throw new Error('cardNoMatchText is required when using the suggestionsActivityFactory.');
}

this.suggestionsActivityFactory = activeLearningTitleOrFactory;
this.suggestionsActivityFactory = qnaSuggestionsActivityFactoryParsed.data;
} else {
this.activeLearningCardTitle = new StringExpression(activeLearningTitleOrFactory ?? this.defaultCardTitle);
this.activeLearningCardTitle = new StringExpression(
activeLearningTitleOrFactory?.toString() ?? this.defaultCardTitle
);
}

if (cardNoMatchText) {
Expand Down
9 changes: 5 additions & 4 deletions libraries/botbuilder-ai/tests/qnaMakerDialog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ describe('QnAMakerDialog', function () {
await rejects(
adapter.send('QnaMaker_TopNAnswer.json').startTest(),
(thrown) =>
thrown.message.includes('invalid_type at message') &&
thrown.message.includes('Expected object, received number')
thrown.message.includes('invalid_type') &&
thrown.message.includes('"expected": "object"') &&
thrown.message.includes('"received": "number"')
);
});

Expand Down Expand Up @@ -544,7 +545,7 @@ describe('QnAMakerDialog', function () {

await rejects(
adapter.send('QnaMaker_TopNAnswer.json').startTest(),
(thrown) => thrown.message.includes('invalid_type at message') && thrown.message.includes('Required')
(thrown) => thrown.message.includes('invalid_type') && thrown.message.includes('Required')
);

sandbox.verify();
Expand Down Expand Up @@ -586,7 +587,7 @@ describe('QnAMakerDialog', function () {

await rejects(
adapter.send('QnaMaker_TopNAnswer.json').startTest(),
(thrown) => thrown.message.includes('invalid_type at message') && thrown.message.includes('Required')
(thrown) => thrown.message.includes('invalid_type') && thrown.message.includes('Required')
);

sandbox.verify();
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-azure-blobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"botbuilder-stdlib": "4.1.6",
"get-stream": "^6.0.0",
"p-map": "^4.0.0",
"zod": "~1.11.17",
"zod": "^3.22.4",
"@azure/core-http": "^3.0.2"
},
"scripts": {
Expand Down
29 changes: 14 additions & 15 deletions libraries/botbuilder-core/etc/botbuilder-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import * as z from 'zod';
// @public
export class ActivityFactory {
static fromObject(lgResult: any): Partial<Activity>;
}
}

// @public
export class ActivityHandler extends ActivityHandlerBase {
Expand Down Expand Up @@ -191,7 +191,7 @@ export abstract class BotComponent {
// (undocumented)
abstract configureServices(services: ServiceCollection, configuration: Configuration): void;
// (undocumented)
static z: z.ZodType<BotComponent, z.ZodTypeDef>;
static z: z.ZodType<BotComponent, z.ZodTypeDef, BotComponent>;
}

export { BotFrameworkClient }
Expand Down Expand Up @@ -342,7 +342,7 @@ export abstract class CloudAdapterBase extends BotAdapter {
export class ComponentRegistration {
static add(componentRegistration: ComponentRegistration): void;
static get components(): ComponentRegistration[];
}
}

// @public
export class ConfigurationBotFrameworkAuthentication extends BotFrameworkAuthentication {
Expand All @@ -353,7 +353,7 @@ export class ConfigurationBotFrameworkAuthentication extends BotFrameworkAuthent
createBotFrameworkClient(): BotFrameworkClient;
createConnectorFactory(claimsIdentity: ClaimsIdentity): ConnectorFactory;
createUserTokenClient(claimsIdentity: ClaimsIdentity): Promise<UserTokenClient>;
}
}

// Warning: (ae-forgotten-export) The symbol "TypedOptions" needs to be exported by the entry point index.d.ts
//
Expand Down Expand Up @@ -385,7 +385,7 @@ export class ConsoleTranscriptLogger implements TranscriptLogger {
export class ConversationState extends BotState {
constructor(storage: Storage_2, namespace?: string);
getStorageKey(context: TurnContext): string | undefined;
}
}

// @public
export interface CoreAppCredentials {
Expand Down Expand Up @@ -436,7 +436,7 @@ export const INVOKE_RESPONSE_KEY: unique symbol;
export class InvokeException<T = unknown> extends Error {
constructor(status: StatusCodes, response?: T);
createInvokeResponse(): InvokeResponse;
}
}

export { InvokeResponse }

Expand Down Expand Up @@ -473,7 +473,7 @@ export class MemoryTranscriptStore implements TranscriptStore {
getTranscriptActivities(channelId: string, conversationId: string, continuationToken?: string, startDate?: Date): Promise<PagedResult<Activity>>;
listTranscripts(channelId: string, continuationToken?: string): Promise<PagedResult<TranscriptInfo>>;
logActivity(activity: Activity): void | Promise<void>;
}
}

// @public
export class MessageFactory {
Expand Down Expand Up @@ -528,7 +528,7 @@ export interface PagedResult<T> {
export class PrivateConversationState extends BotState {
constructor(storage: Storage_2, namespace?: string);
getStorageKey(context: TurnContext): string | undefined;
}
}

// @public
export interface PropertyManager {
Expand Down Expand Up @@ -577,15 +577,15 @@ export enum Severity {
export class ShowTypingMiddleware implements Middleware {
constructor(delay?: number, period?: number);
onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>;
}
}

// @public
export class SkillConversationIdFactory extends SkillConversationIdFactoryBase {
constructor(storage: Storage_2);
createSkillConversationIdWithOptions(options: SkillConversationIdFactoryOptions): Promise<string>;
deleteConversationReference(skillConversationId: string): Promise<void>;
getSkillConversationReference(skillConversationId: string): Promise<SkillConversationReference>;
}
}

// @public
export abstract class SkillConversationIdFactoryBase {
Expand Down Expand Up @@ -638,7 +638,6 @@ interface Storage_2 {
read(keys: string[]): Promise<StoreItems>;
write(changes: StoreItems): Promise<void>;
}

export { Storage_2 as Storage }

// @public
Expand Down Expand Up @@ -730,7 +729,7 @@ export class TelemetryLoggerMiddleware implements Middleware {
onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>;
protected onUpdateActivity(activity: Activity): Promise<void>;
get telemetryClient(): BotTelemetryClient;
}
}

// @public (undocumented)
export interface TelemetryPageView {
Expand Down Expand Up @@ -805,7 +804,7 @@ export class TestAdapter extends BotAdapter implements ExtendedUserTokenProvider
testActivities(activities: Partial<Activity>[], description?: string, timeout?: number): TestFlow;
throwOnExchangeRequest(connectionName: string, channelId: string, userId: string, exchangeableItem: string): void;
updateActivity(context: TurnContext, activity: Partial<Activity>): Promise<ResourceResponse | void>;
}
}

// @public
export class TestFlow {
Expand Down Expand Up @@ -856,7 +855,7 @@ export interface TranscriptLogger {
export class TranscriptLoggerMiddleware implements Middleware {
constructor(logger: TranscriptLogger);
onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>;
}
}

// @public
export interface TranscriptStore extends TranscriptLogger {
Expand Down Expand Up @@ -918,7 +917,7 @@ export function useBotState(botAdapter: BotAdapter, ...botStates: BotState[]): B
export class UserState extends BotState {
constructor(storage: Storage_2, namespace?: string);
getStorageKey(context: TurnContext): string | undefined;
}
}

// @public
export const verifyStateOperationName = "signin/verifyState";
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"botframework-connector": "4.1.6",
"botframework-schema": "4.1.6",
"uuid": "^8.3.2",
"zod": "~1.11.17"
"zod": "^3.22.4"
},
"devDependencies": {
"@microsoft/bf-chatdown": "^4.15.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class ConfigurationBotFrameworkAuthentication extends BotFrameworkAuthent
);
} catch (err) {
// Throw a new error with the validation details prominently featured.
if (z.instanceof(z.ZodError).check(err)) {
if (z.instanceof(z.ZodError).safeParse(err).success) {
throw new Error(JSON.stringify(err.errors, null, 2));
}
throw err;
Expand Down

0 comments on commit 45d7dda

Please sign in to comment.