diff --git a/.gitignore b/.gitignore index ad46b30..2cad553 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ typings/ # next.js build output .next + +# ignore package lock +package-lock.json diff --git a/index.js b/index.js index 99ba6d0..b3d179d 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,8 @@ 'use strict' -const Ajv = require('ajv') -const ajv = new Ajv({ allErrors: true, removeAdditional: true, useDefaults: true, coerceTypes: true }) +const Ajv = require('ajv').default -ajv.addKeyword('separator', { +const separator = { type: 'string', metaSchema: { type: 'string', @@ -13,9 +12,19 @@ ajv.addKeyword('separator', { valid: true, errors: false, compile: (schema) => (data, dataPath, parentData, parentDataProperty) => { - parentData[parentDataProperty] = data === '' ? [] : data.split(schema) + // In some cases parentData and parentDataProperty will be undefined. + // We need to fall back to the dataPath object to provide those values. + if (parentData && parentDataProperty) { + parentData[parentDataProperty] = data === '' ? [] : data.split(schema) + } else { + const { + parentData: pData, + parentDataProperty: pDataProperty + } = dataPath + pData[pDataProperty] = data === '' ? [] : data.split(schema) + } } -}) +} const optsSchema = { type: 'object', @@ -34,6 +43,18 @@ const optsSchema = { expandEnv: { type: ['boolean'], default: false } } } + +const ajv = new Ajv({ + allErrors: true, + removeAdditional: true, + useDefaults: true, + coerceTypes: true, + allowUnionTypes: true, + keywords: { + separator + } +}) + const optsSchemaValidator = ajv.compile(optsSchema) function loadAndValidateEnvironment (_opts) { diff --git a/package.json b/package.json index 9629622..8319e73 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "tsd": "^0.14.0" }, "dependencies": { - "ajv": "^6.10.2", + "ajv": "^7.1.1", "dotenv": "^8.2.0", "dotenv-expand": "^5.1.0" },