Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-image): Better error message for missing alt #29981

Merged
merged 4 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PlaceholderProps } from "./placeholder"
import { MainImageProps } from "./main-image"
import { Layout } from "../image-utils"
import { getSizer } from "./layout-wrapper"
import { propTypes } from "./gatsby-image.server"

// eslint-disable-next-line @typescript-eslint/naming-convention
export interface GatsbyImageProps
Expand Down Expand Up @@ -215,4 +216,7 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
) {
return <GatsbyImageHydrator {...props} />
}

GatsbyImage.propTypes = propTypes

GatsbyImage.displayName = `GatsbyImage`
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React, { ElementType, FunctionComponent, CSSProperties } from "react"
import React, {
ElementType,
FunctionComponent,
CSSProperties,
WeakValidationMap,
} from "react"
import { GatsbyImageProps, IGatsbyImageData } from "./gatsby-image.browser"
import { getWrapperProps, getMainProps, getPlaceholderProps } from "./hooks"
import { Placeholder } from "./placeholder"
import { MainImage, MainImageProps } from "./main-image"
import { LayoutWrapper } from "./layout-wrapper"
import PropTypes from "prop-types"

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

Expand Down Expand Up @@ -123,3 +129,21 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
</GatsbyImageHydrator>
)
}

export const altValidator: PropTypes.Validator<string> = (
props: GatsbyImageProps,
propName,
componentName,
...rest
): Error | undefined => {
if (!props.alt && props.alt !== ``) {
return new Error(
`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`
)
}
return PropTypes.string(props, propName, componentName, ...rest)
}
export const propTypes = {
image: PropTypes.object.isRequired,
alt: altValidator,
} as WeakValidationMap<GatsbyImageProps>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { FunctionComponent, ReactElement } from "react"
import { GatsbyImage as GatsbyImageServer } from "./gatsby-image.server"
import {
altValidator,
GatsbyImage as GatsbyImageServer,
} from "./gatsby-image.server"
import { GatsbyImageProps, IGatsbyImageData } from "./gatsby-image.browser"
import PropTypes from "prop-types"
import { ISharpGatsbyImageArgs } from "../image-utils"
Expand Down Expand Up @@ -83,7 +86,7 @@ const validLayouts = new Set([`fixed`, `fullWidth`, `constrained`])

export const propTypes = {
src: PropTypes.string.isRequired,
alt: PropTypes.string.isRequired,
alt: altValidator,
width: checkDimensionProps,
height: checkDimensionProps,
sizes: PropTypes.string,
Expand Down