From 413af28320a2e6439205b0541db23530f9345c4d Mon Sep 17 00:00:00 2001 From: Laurie Date: Tue, 12 Jan 2021 08:47:07 -0500 Subject: [PATCH] propTypes check --- .../src/components/static-image.server.tsx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/gatsby-plugin-image/src/components/static-image.server.tsx b/packages/gatsby-plugin-image/src/components/static-image.server.tsx index 7d8e33124c87e..a8b22b9edd40b 100644 --- a/packages/gatsby-plugin-image/src/components/static-image.server.tsx +++ b/packages/gatsby-plugin-image/src/components/static-image.server.tsx @@ -62,11 +62,30 @@ const StaticImage: React.FC< IStaticImageProps & IPrivateProps > = _getStaticImage(GatsbyImageServer) +const checkDimensionProps: PropTypes.Validator = ( + props: IStaticImageProps & IPrivateProps, + propName: keyof IStaticImageProps & IPrivateProps, + ...rest +) => { + if ( + props.layout === `fluid` && + (propName === `width` || propName === `height`) && + props[propName] + ) { + return new Error( + `"${propName}" ${props[propName]} may not be passed when layout is fluid.` + ) + } + return PropTypes.number(props, propName, ...rest) +} + const validLayouts = new Set([`fixed`, `fluid`, `constrained`]) export const propTypes = { src: PropTypes.string.isRequired, alt: PropTypes.string.isRequired, + width: checkDimensionProps, + height: checkDimensionProps, sizes: PropTypes.string, layout: (props: IStaticImageProps & IPrivateProps): Error | undefined => { if (props.layout === undefined) {