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

Ensure default config passes schema checks #46656

Merged
merged 1 commit into from
Mar 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 25 additions & 10 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const configSchema = {
additionalProperties: false,
properties: {
canonicalBase: {
minLength: 1,
nullable: true,
type: 'string',
},
},
Expand All @@ -21,7 +21,7 @@ const configSchema = {
type: 'string',
},
assetPrefix: {
minLength: 1,
nullable: true,
type: 'string',
},
basePath: {
Expand Down Expand Up @@ -170,6 +170,9 @@ const configSchema = {
compress: {
type: 'boolean',
},
configOrigin: {
type: 'string',
},
crossOrigin: {
oneOf: [
false,
Expand Down Expand Up @@ -342,7 +345,7 @@ const configSchema = {
type: 'boolean',
},
outputFileTracingRoot: {
minLength: 1,
nullable: true,
type: 'string',
},
outputFileTracingExcludes: {
Expand Down Expand Up @@ -521,6 +524,7 @@ const configSchema = {
},
i18n: {
additionalProperties: false,
nullable: true,
properties: {
defaultLocale: {
minLength: 1,
Expand Down Expand Up @@ -568,17 +572,17 @@ const configSchema = {
},
images: {
additionalProperties: false,
nullable: true,
properties: {
remotePatterns: {
nullable: true,
items: {
additionalProperties: false,
properties: {
hostname: {
minLength: 1,
type: 'string',
},
pathname: {
minLength: 1,
type: 'string',
},
port: {
Expand All @@ -601,35 +605,39 @@ const configSchema = {
type: 'boolean',
},
contentSecurityPolicy: {
minLength: 1,
type: 'string',
nullable: true,
},
contentDispositionType: {
enum: ['inline', 'attachment'] as any, // automatic typing does not like enum
type: 'string',
nullable: true,
},
dangerouslyAllowSVG: {
type: 'boolean',
nullable: true,
},
deviceSizes: {
items: {
type: 'integer',
minimum: 1,
maximum: 10000,
},
minItems: 1,
maxItems: 25,
type: 'array',
nullable: true,
},
disableStaticImages: {
type: 'boolean',
nullable: true,
},
domains: {
items: {
type: 'string',
},
maxItems: 50,
type: 'array',
nullable: true,
},
formats: {
items: {
Expand All @@ -638,6 +646,7 @@ const configSchema = {
} as any,
maxItems: 4,
type: 'array',
nullable: true,
},
imageSizes: {
items: {
Expand All @@ -648,23 +657,26 @@ const configSchema = {
minItems: 0,
maxItems: 25,
type: 'array',
nullable: true,
},
loader: {
// automatic typing does not like enum
enum: VALID_LOADERS as any,
type: 'string',
nullable: true,
},
loaderFile: {
minLength: 1,
type: 'string',
nullable: true,
},
minimumCacheTTL: {
type: 'integer',
minimum: 0,
nullable: true,
},
path: {
minLength: 1,
type: 'string',
nullable: true,
},
},
type: 'object',
Expand Down Expand Up @@ -737,6 +749,9 @@ const configSchema = {
swcMinify: {
type: 'boolean',
},
target: {
type: 'string',
},
trailingSlash: {
type: 'boolean',
},
Expand Down Expand Up @@ -768,7 +783,7 @@ const configSchema = {
'must be a function that returns a webpack configuration object',
} as any,
},
} as JSONSchemaType<NextConfig>
} as JSONSchemaType<NextConfig & { configOrigin?: any; target?: any }>

// module.exports is used to get around an export bug with TypeScript
// and the Ajv automatic typing
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type NextConfigComplete = Required<NextConfig> & {
configOrigin?: string
configFile?: string
configFileName: string
target?: string
}

export interface I18NConfig {
Expand Down Expand Up @@ -569,7 +570,6 @@ export interface NextConfig extends Record<string, any> {
export const defaultConfig: NextConfig = {
env: {},
webpack: null,
webpackDevMiddleware: null,
eslint: {
ignoreDuringBuilds: false,
},
Expand Down Expand Up @@ -610,7 +610,7 @@ export const defaultConfig: NextConfig = {
excludeDefaultMomentLocales: true,
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: null,
reactStrictMode: false,
httpAgentOptions: {
keepAlive: true,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,11 @@ export async function compile_config_schema(task, opts) {
keyword: 'isFunction',
schemaType: 'boolean',
compile() {
return (data) => data instanceof Function
return (data) => data == null || data instanceof Function
},
code(ctx) {
const { data } = ctx
ctx.fail(Ajv._`!(${data} instanceof Function)`)
ctx.fail(Ajv._`!(${data} == null || ${data} instanceof Function)`)
},
metaSchema: {
anyOf: [{ type: 'boolean' }],
Expand Down
3 changes: 3 additions & 0 deletions test/integration/config-schema-check/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (phase, { defaultConfig }) => {
return defaultConfig
}
3 changes: 3 additions & 0 deletions test/integration/config-schema-check/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}
20 changes: 20 additions & 0 deletions test/integration/config-schema-check/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env jest */

import { join } from 'path'
import { nextBuild } from 'next-test-utils'
import stripAnsi from 'strip-ansi'

const appDir = join(__dirname, '../')

describe('next.config.js schema validating', () => {
it('should validate against defaultConfig', async () => {
const result = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
const output = stripAnsi(result.stderr + result.stdout)

expect(output).not.toContain('Invalid next.config.js options detected')
expect(result.code).toBe(0)
})
})