Skip to content

Commit fe4b329

Browse files
ascorbicgatsbybot
andauthored
feat(gatsby-plugin-image): Better error message for missing alt (#29981)
* feat(gatsby-plugin-image): Better error message for missing alt * Handle invalid types too * Add support in StaticImage Co-authored-by: gatsbybot <[email protected]>
1 parent 9429b3b commit fe4b329

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { PlaceholderProps } from "./placeholder"
1818
import { MainImageProps } from "./main-image"
1919
import { Layout } from "../image-utils"
2020
import { getSizer } from "./layout-wrapper"
21+
import { propTypes } from "./gatsby-image.server"
2122

2223
// eslint-disable-next-line @typescript-eslint/naming-convention
2324
export interface GatsbyImageProps
@@ -215,4 +216,7 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
215216
) {
216217
return <GatsbyImageHydrator {...props} />
217218
}
219+
220+
GatsbyImage.propTypes = propTypes
221+
218222
GatsbyImage.displayName = `GatsbyImage`

packages/gatsby-plugin-image/src/components/gatsby-image.server.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import React, { ElementType, FunctionComponent, CSSProperties } from "react"
1+
import React, {
2+
ElementType,
3+
FunctionComponent,
4+
CSSProperties,
5+
WeakValidationMap,
6+
} from "react"
27
import { GatsbyImageProps, IGatsbyImageData } from "./gatsby-image.browser"
38
import { getWrapperProps, getMainProps, getPlaceholderProps } from "./hooks"
49
import { Placeholder } from "./placeholder"
510
import { MainImage, MainImageProps } from "./main-image"
611
import { LayoutWrapper } from "./layout-wrapper"
12+
import PropTypes from "prop-types"
713

814
const removeNewLines = (str: string): string => str.replace(/\n/g, ``)
915

@@ -123,3 +129,21 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
123129
</GatsbyImageHydrator>
124130
)
125131
}
132+
133+
export const altValidator: PropTypes.Validator<string> = (
134+
props: GatsbyImageProps,
135+
propName,
136+
componentName,
137+
...rest
138+
): Error | undefined => {
139+
if (!props.alt && props.alt !== ``) {
140+
return new Error(
141+
`The "alt" prop is required in ${componentName}. If the image is purely presentational then pass an empty string: e.g. alt="". Learn more: https://a11y-style-guide.com/style-guide/section-media.html`
142+
)
143+
}
144+
return PropTypes.string(props, propName, componentName, ...rest)
145+
}
146+
export const propTypes = {
147+
image: PropTypes.object.isRequired,
148+
alt: altValidator,
149+
} as WeakValidationMap<GatsbyImageProps>

packages/gatsby-plugin-image/src/components/static-image.server.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React, { FunctionComponent, ReactElement } from "react"
2-
import { GatsbyImage as GatsbyImageServer } from "./gatsby-image.server"
2+
import {
3+
altValidator,
4+
GatsbyImage as GatsbyImageServer,
5+
} from "./gatsby-image.server"
36
import { GatsbyImageProps, IGatsbyImageData } from "./gatsby-image.browser"
47
import PropTypes from "prop-types"
58
import { ISharpGatsbyImageArgs } from "../image-utils"
@@ -83,7 +86,7 @@ const validLayouts = new Set([`fixed`, `fullWidth`, `constrained`])
8386

8487
export const propTypes = {
8588
src: PropTypes.string.isRequired,
86-
alt: PropTypes.string.isRequired,
89+
alt: altValidator,
8790
width: checkDimensionProps,
8891
height: checkDimensionProps,
8992
sizes: PropTypes.string,

0 commit comments

Comments
 (0)