Skip to content

Commit

Permalink
Ensure default config passes schema checks (#46656)
Browse files Browse the repository at this point in the history
This ensures our config schema correctly validates with the default
config we provide.

Fixes: #46626

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
  • Loading branch information
ijjk committed Mar 1, 2023
1 parent 5e112c0 commit c627912
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
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)
})
})

0 comments on commit c627912

Please sign in to comment.