diff --git a/packages/flow-runtime/src/TypeContext.js b/packages/flow-runtime/src/TypeContext.js index 443ce3ba..96e1b2ab 100644 --- a/packages/flow-runtime/src/TypeContext.js +++ b/packages/flow-runtime/src/TypeContext.js @@ -1027,10 +1027,16 @@ export default class TypeContext { } warn (type: Type, input: V, prefix: string = '', path?: string[]): V { - const validation = this.validate(type, input, prefix, path); - const message = makeWarningMessage(validation); - if (typeof message === 'string') { - this.emitWarningMessage(message); + try { + const validation = this.validate(type, input, prefix, path); + const message = makeWarningMessage(validation); + if (typeof message === 'string') { + this.emitWarningMessage(message); + } + } + catch (error) { + const message = error && error.message ? error.message : String(error); + this.emitWarningMessage(`Warning: ${message}`); } return input; } diff --git a/packages/flow-runtime/src/typed.test.js b/packages/flow-runtime/src/typed.test.js index d2eb3380..13c7ad6f 100644 --- a/packages/flow-runtime/src/typed.test.js +++ b/packages/flow-runtime/src/typed.test.js @@ -457,6 +457,19 @@ describe('Typed API', () => { }); + it('should warn instead of throwing when validation throws', () => { + const context = t.createContext(); + const type = context.$shape(context.string()); + let warning; + const input = {}; + context.emitWarningMessage = (message) => { + warning = message; + }; + + equal(context.warn(type, input), input); + ok(warning.indexOf('Can only $Shape object types.') !== -1); + }); + it('should $Keys', () => { const A = t.object( t.property('name', t.string()), @@ -558,4 +571,4 @@ describe('Typed API', () => { )) ); }); -}); \ No newline at end of file +});