Enable strict checking of config during CLI.

Signed-off-by: Aramis Sennyey <sennyeya@amazon.com>
This commit is contained in:
Aramis Sennyey
2023-05-05 13:45:53 -04:00
parent 9ab7a4b538
commit 473db605a4
13 changed files with 64 additions and 11 deletions
+5 -2
View File
@@ -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>();
+13 -7
View File
@@ -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(