mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-10 04:22:12 +00:00
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import Ajv from 'ajv';
|
|
import {
|
|
routesSchema,
|
|
cleanUrlsSchema,
|
|
headersSchema,
|
|
redirectsSchema,
|
|
rewritesSchema,
|
|
trailingSlashSchema,
|
|
} from '@vercel/routing-utils';
|
|
import { VercelConfig } from './types';
|
|
import {
|
|
functionsSchema,
|
|
buildsSchema,
|
|
NowBuildError,
|
|
getPrettyError,
|
|
} from '@vercel/build-utils';
|
|
import { fileNameSymbol } from '@vercel/client';
|
|
|
|
const vercelConfigSchema = {
|
|
type: 'object',
|
|
// These are not all possibilities because `vc dev`
|
|
// doesn't need to know about `regions`, `public`, etc.
|
|
additionalProperties: true,
|
|
properties: {
|
|
builds: buildsSchema,
|
|
routes: routesSchema,
|
|
cleanUrls: cleanUrlsSchema,
|
|
headers: headersSchema,
|
|
redirects: redirectsSchema,
|
|
rewrites: rewritesSchema,
|
|
trailingSlash: trailingSlashSchema,
|
|
functions: functionsSchema,
|
|
},
|
|
};
|
|
|
|
const ajv = new Ajv();
|
|
const validate = ajv.compile(vercelConfigSchema);
|
|
|
|
export function validateConfig(config: VercelConfig): NowBuildError | null {
|
|
if (!validate(config)) {
|
|
if (validate.errors && validate.errors[0]) {
|
|
const error = validate.errors[0];
|
|
const fileName = config[fileNameSymbol] || 'vercel.json';
|
|
const niceError = getPrettyError(error);
|
|
niceError.message = `Invalid ${fileName} - ${niceError.message}`;
|
|
return niceError;
|
|
}
|
|
}
|
|
|
|
if (config.functions && config.builds) {
|
|
return new NowBuildError({
|
|
code: 'FUNCTIONS_AND_BUILDS',
|
|
message:
|
|
'The `functions` property cannot be used in conjunction with the `builds` property. Please remove one of them.',
|
|
link: 'https://vercel.link/functions-and-builds',
|
|
});
|
|
}
|
|
|
|
return null;
|
|
}
|