@@ -22,87 +22,28 @@ import { setHttpClientAndAgentOptions } from './setup-http-agent-env'
22
22
import { pathHasPrefix } from '../shared/lib/router/utils/path-has-prefix'
23
23
import { matchRemotePattern } from '../shared/lib/match-remote-pattern'
24
24
25
- import { ZodParsedType , util as ZodUtil } from 'next/dist/compiled/zod'
26
- import type { ZodError , ZodIssue } from 'next/dist/compiled/zod'
25
+ import type { ZodError } from 'next/dist/compiled/zod'
27
26
import { hasNextSupport } from '../server/ci-info'
28
27
import { transpileConfig } from '../build/next-config-ts/transpile-config'
29
28
import { dset } from '../shared/lib/dset'
29
+ import { normalizeZodErrors } from '../shared/lib/zod'
30
30
31
31
export { normalizeConfig } from './config-shared'
32
32
export type { DomainLocale , NextConfig } from './config-shared'
33
33
34
- function processZodErrorMessage ( issue : ZodIssue ) {
35
- let message = issue . message
36
-
37
- let path = ''
38
-
39
- if ( issue . path . length > 0 ) {
40
- if ( issue . path . length === 1 ) {
41
- const identifier = issue . path [ 0 ]
42
- if ( typeof identifier === 'number' ) {
43
- // The first identifier inside path is a number
44
- path = `index ${ identifier } `
45
- } else {
46
- path = `"${ identifier } "`
47
- }
48
- } else {
49
- // joined path to be shown in the error message
50
- path = `"${ issue . path . reduce < string > ( ( acc , cur ) => {
51
- if ( typeof cur === 'number' ) {
52
- // array index
53
- return `${ acc } [${ cur } ]`
54
- }
55
- if ( cur . includes ( '"' ) ) {
56
- // escape quotes
57
- return `${ acc } ["${ cur . replaceAll ( '"' , '\\"' ) } "]`
58
- }
59
- // dot notation
60
- const separator = acc . length === 0 ? '' : '.'
61
- return acc + separator + cur
62
- } , '' ) } "`
63
- }
64
- }
65
-
66
- if (
67
- issue . code === 'invalid_type' &&
68
- issue . received === ZodParsedType . undefined
69
- ) {
70
- // missing key in object
71
- return `${ path } is missing, expected ${ issue . expected } `
72
- }
73
- if ( issue . code === 'invalid_enum_value' ) {
74
- // Remove "Invalid enum value" prefix from zod default error message
75
- return `Expected ${ ZodUtil . joinValues ( issue . options ) } , received '${
76
- issue . received
77
- } ' at ${ path } `
78
- }
79
-
80
- return message + ( path ? ` at ${ path } ` : '' )
81
- }
82
-
83
- function normalizeZodErrors (
34
+ function normalizeNextConfigZodErrors (
84
35
error : ZodError < NextConfig >
85
36
) : [ errorMessages : string [ ] , shouldExit : boolean ] {
86
37
let shouldExit = false
38
+ const issues = normalizeZodErrors ( error )
87
39
return [
88
- error . issues . flatMap ( ( issue ) => {
89
- const messages = [ processZodErrorMessage ( issue ) ]
40
+ issues . flatMap ( ( { issue, message } ) => {
90
41
if ( issue . path [ 0 ] === 'images' ) {
91
42
// We exit the build when encountering an error in the images config
92
43
shouldExit = true
93
44
}
94
45
95
- if ( 'unionErrors' in issue ) {
96
- issue . unionErrors
97
- . map ( normalizeZodErrors )
98
- . forEach ( ( [ unionMessages , unionShouldExit ] ) => {
99
- messages . push ( ...unionMessages )
100
- // If any of the union results shows exit the build, we exit the build
101
- shouldExit = shouldExit || unionShouldExit
102
- } )
103
- }
104
-
105
- return messages
46
+ return message
106
47
} ) ,
107
48
shouldExit ,
108
49
]
@@ -1085,7 +1026,9 @@ export default async function loadConfig(
1085
1026
// error message header
1086
1027
const messages = [ `Invalid ${ configFileName } options detected: ` ]
1087
1028
1088
- const [ errorMessages , shouldExit ] = normalizeZodErrors ( state . error )
1029
+ const [ errorMessages , shouldExit ] = normalizeNextConfigZodErrors (
1030
+ state . error
1031
+ )
1089
1032
// ident list item
1090
1033
for ( const error of errorMessages ) {
1091
1034
messages . push ( ` ${ error } ` )
0 commit comments