Files
vercel/packages/cli/src/util/dev/validate.ts
Nathan Rajlich e5131d5557 [cli] Update NowConfig references to VercelConfig (#6256)
The `NowConfig` interface is deprecated.
2021-05-21 12:21:38 -07:00

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;
}