-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
122 lines (101 loc) · 2.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'
const Ajv = require('ajv')
const separator = {
keyword: 'separator',
type: 'string',
metaSchema: {
type: 'string',
description: 'value separator'
},
modifying: true,
valid: true,
errors: false,
compile: (schema) => (data, { parentData: pData, parentDataProperty: pDataProperty }) => {
pData[pDataProperty] = data === '' ? [] : data.split(schema)
}
}
const optsSchema = {
type: 'object',
required: ['schema'],
properties: {
schema: { type: 'object', additionalProperties: true },
data: {
oneOf: [
{ type: 'array', items: { type: 'object' }, minItems: 1 },
{ type: 'object' }
],
default: {}
},
env: { type: 'boolean', default: true },
dotenv: { type: ['boolean', 'object'], default: false },
expandEnv: { type: ['boolean'], default: false },
ajv: { type: 'object', additionalProperties: true }
}
}
const sharedAjvInstance = getDefaultInstance()
const optsSchemaValidator = sharedAjvInstance.compile(optsSchema)
function envSchema (_opts) {
const opts = Object.assign({}, _opts)
if (opts.schema && opts.schema[Symbol.for('fluent-schema-object')]) {
opts.schema = opts.schema.valueOf()
}
const isOptionValid = optsSchemaValidator(opts)
if (!isOptionValid) {
const error = new Error(sharedAjvInstance.errorsText(optsSchemaValidator.errors, { dataVar: 'opts' }))
error.errors = optsSchemaValidator.errors
throw error
}
const { schema } = opts
schema.additionalProperties = false
let { data, dotenv, env, expandEnv } = opts
if (!Array.isArray(data)) {
data = [data]
}
if (dotenv) {
require('dotenv').config(Object.assign({}, dotenv))
}
/* istanbul ignore else */
if (env) {
data.unshift(process.env)
}
const merge = {}
data.forEach(d => Object.assign(merge, d))
if (expandEnv) {
require('dotenv-expand').expand({ ignoreProcessEnv: true, parsed: merge })
}
const ajv = chooseAjvInstance(sharedAjvInstance, opts.ajv)
const valid = ajv.validate(schema, merge)
if (!valid) {
const error = new Error(ajv.errorsText(ajv.errors, { dataVar: 'env' }))
error.errors = ajv.errors
throw error
}
return merge
}
function chooseAjvInstance (defaultInstance, ajvOpts) {
if (!ajvOpts) {
return defaultInstance
} else if (typeof ajvOpts === 'object' && typeof ajvOpts.customOptions === 'function') {
const ajv = ajvOpts.customOptions(getDefaultInstance())
if (!(ajv instanceof Ajv)) {
throw new TypeError('customOptions function must return an instance of Ajv')
}
return ajv
}
return ajvOpts
}
function getDefaultInstance () {
return new Ajv({
allErrors: true,
removeAdditional: true,
useDefaults: true,
coerceTypes: true,
allowUnionTypes: true,
addUsedSchema: false,
keywords: [separator]
})
}
envSchema.keywords = { separator }
module.exports = envSchema
module.exports.default = envSchema
module.exports.envSchema = envSchema