Enable strict checking of config during CLI.
Signed-off-by: Aramis Sennyey <sennyeya@amazon.com>
This commit is contained in:
@@ -180,14 +180,17 @@ export function loadConfigSchema(
|
||||
): Promise<ConfigSchema>;
|
||||
|
||||
// @public
|
||||
export type LoadConfigSchemaOptions =
|
||||
export type LoadConfigSchemaOptions = (
|
||||
| {
|
||||
dependencies: string[];
|
||||
packagePaths?: string[];
|
||||
}
|
||||
| {
|
||||
serialized: JsonObject;
|
||||
};
|
||||
}
|
||||
) & {
|
||||
noUndeclaredProperties?: boolean;
|
||||
};
|
||||
|
||||
// @public
|
||||
export function mergeConfigSchemas(schemas: JSONSchema7[]): JSONSchema7;
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
CONFIG_VISIBILITIES,
|
||||
ConfigVisibility,
|
||||
} from './types';
|
||||
import { SchemaObject } from 'json-schema-traverse';
|
||||
|
||||
/**
|
||||
* This takes a collection of Backstage configuration schemas from various
|
||||
@@ -35,6 +36,9 @@ import {
|
||||
*/
|
||||
export function compileConfigSchemas(
|
||||
schemas: ConfigSchemaPackageEntry[],
|
||||
options?: {
|
||||
noUndeclaredProperties?: boolean;
|
||||
},
|
||||
): ValidationFunc {
|
||||
// The ajv instance below is stateful and doesn't really allow for additional
|
||||
// output during validation. We work around this by having this extra piece
|
||||
@@ -102,6 +106,18 @@ export function compileConfigSchemas(
|
||||
|
||||
const merged = mergeConfigSchemas(schemas.map(_ => _.value));
|
||||
|
||||
if (options?.noUndeclaredProperties) {
|
||||
traverse(merged, (schema: SchemaObject) => {
|
||||
/**
|
||||
* The `additionalProperties` key can only be applied to `type: object` in the JSON
|
||||
* schema.
|
||||
*/
|
||||
if (schema?.type === 'object') {
|
||||
schema.additionalProperties ||= false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const validate = ajv.compile(merged);
|
||||
|
||||
const visibilityBySchemaPath = new Map<string, ConfigVisibility>();
|
||||
|
||||
@@ -32,12 +32,16 @@ import {
|
||||
* @public
|
||||
*/
|
||||
export type LoadConfigSchemaOptions =
|
||||
| {
|
||||
dependencies: string[];
|
||||
packagePaths?: string[];
|
||||
}
|
||||
| {
|
||||
serialized: JsonObject;
|
||||
| (
|
||||
| {
|
||||
dependencies: string[];
|
||||
packagePaths?: string[];
|
||||
}
|
||||
| {
|
||||
serialized: JsonObject;
|
||||
}
|
||||
) & {
|
||||
noUndeclaredProperties?: boolean;
|
||||
};
|
||||
|
||||
function errorsToError(errors: ValidationError[]): Error {
|
||||
@@ -77,7 +81,9 @@ export async function loadConfigSchema(
|
||||
schemas = serialized.schemas as ConfigSchemaPackageEntry[];
|
||||
}
|
||||
|
||||
const validate = compileConfigSchemas(schemas);
|
||||
const validate = compileConfigSchemas(schemas, {
|
||||
noUndeclaredProperties: options.noUndeclaredProperties,
|
||||
});
|
||||
|
||||
return {
|
||||
process(
|
||||
|
||||
Reference in New Issue
Block a user