From c88d395df46de89f5d495bafe5fd1012cb9bb46e Mon Sep 17 00:00:00 2001 From: cedar Date: Tue, 2 Jun 2026 08:14:43 +0800 Subject: [PATCH] Warn on validation-time errors --- packages/flow-runtime/src/TypeContext.js | 9 ++++++++- packages/flow-runtime/src/typed.test.js | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/flow-runtime/src/TypeContext.js b/packages/flow-runtime/src/TypeContext.js index 443ce3ba..bd504ba9 100644 --- a/packages/flow-runtime/src/TypeContext.js +++ b/packages/flow-runtime/src/TypeContext.js @@ -1027,7 +1027,14 @@ export default class TypeContext { } warn (type: Type, input: V, prefix: string = '', path?: string[]): V { - const validation = this.validate(type, input, prefix, path); + let validation; + try { + validation = this.validate(type, input, prefix, path); + } + catch (error) { + this.emitWarningMessage(error && error.message ? error.message : String(error)); + return input; + } const message = makeWarningMessage(validation); if (typeof message === 'string') { this.emitWarningMessage(message); diff --git a/packages/flow-runtime/src/typed.test.js b/packages/flow-runtime/src/typed.test.js index d2eb3380..8bc2bff1 100644 --- a/packages/flow-runtime/src/typed.test.js +++ b/packages/flow-runtime/src/typed.test.js @@ -6,6 +6,21 @@ import t from './globalContext'; const no = (input: any): any => equal(Boolean(input), false); describe('Typed API', () => { + it('should warn instead of throwing validation-time invariant errors', () => { + const context = t.createContext(); + const type = context.$shape(context.string()); + const input = {foo: 'bar'}; + const messages = []; + + context.emitWarningMessage = message => { + messages.push(message); + }; + + equal(context.warn(type, input), input); + equal(messages.length, 1); + ok(/Can only \$Shape object types\./.test(messages[0])); + }); + it('should check a string', () => { const type = t.string(); ok(type.accepts('helo world')); @@ -558,4 +573,4 @@ describe('Typed API', () => { )) ); }); -}); \ No newline at end of file +});