From d171f5476d260f90ff0fe9916aed4a0eea49dfde Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Tue, 4 Aug 2020 13:39:37 +0200 Subject: [PATCH] fix(Config Schema): Fix errors normalization with external refs --- lib/classes/ConfigSchemaHandler/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/classes/ConfigSchemaHandler/index.js b/lib/classes/ConfigSchemaHandler/index.js index 7abd92380..ef7324609 100644 --- a/lib/classes/ConfigSchemaHandler/index.js +++ b/lib/classes/ConfigSchemaHandler/index.js @@ -8,6 +8,20 @@ const normalizeAjvErrors = require('./normalizeAjvErrors'); const FUNCTION_NAME_PATTERN = '^[a-zA-Z0-9-_]+$'; const ERROR_PREFIX = 'Configuration error'; +const normalizeSchemaObject = (object, instanceSchema) => { + for (const [key, value] of _.entries(object)) { + if (!_.isObject(value)) continue; + if (!value.$ref) { + normalizeSchemaObject(value, instanceSchema); + continue; + } + if (!value.$ref.startsWith('#/definitions/')) { + throw new Error(`Unsupported reference ${value.$ref}`); + } + object[key] = _.get(instanceSchema, value.$ref.slice(2).split('/')); + } +}; + class ConfigSchemaHandler { constructor(serverless) { this.serverless = serverless; @@ -55,7 +69,9 @@ class ConfigSchemaHandler { this.relaxProviderSchema(); } - const ajv = new Ajv({ allErrors: true }); + const ajv = new Ajv({ allErrors: true, verbose: true }); + // Workaround https://github.com/ajv-validator/ajv/issues/1255 + normalizeSchemaObject(this.schema, this.schema); const validate = ajv.compile(this.schema); validate(userConfig);