Skip to content

Commit

Permalink
Merge pull request #6 from fastify/avoid-value-of
Browse files Browse the repository at this point in the history
Avoid value of for fluent-schema
  • Loading branch information
mcollina authored Jul 1, 2019
2 parents 58f2655 + f4652bf commit 1ac9bd9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ console.log(config)
// output: { PORT: 3000 }
```

**NB:** internally this plugin force to not have additional properties, so the `additionalProperties` flag is forced to be `false`
It is possible to also use [fluent-schema](http://npm.im/fluent-schema):

```js
const envSchema = require('env-schema')
const S = require('fluent-schema')

const config = envSchema({
schema: S.object().prop('port', S.string().default('3000').required()),
data: data // optional, default: process.env
dotenv: true // load .env if it's there, default: false
})

console.log(config)
// output: { PORT: 3000 }
```

**NB:** internally this plugin force to not have additional properties,
so the `additionalProperties` flag is forced to be `false`

## Acknowledgements

Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const optsSchema = {
}
const optsSchemaValidator = ajv.compile(optsSchema)

function loadAndValidateEnvironment (opts) {
function loadAndValidateEnvironment (_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(optsSchemaValidator.errors.map(e => e.message))
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "env-schema",
"version": "0.0.1",
"version": "0.1.0",
"description": "Validate your env variable using Ajv and dotenv",
"main": "index.js",
"scripts": {
"test": "standard | snazzy && tap test/*.js"
"test": "standard | snazzy && tap test/*.test.js"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +26,7 @@
},
"homepage": "https://github.com/fastify/env-schema#readme",
"devDependencies": {
"fluent-schema": "^0.7.3",
"pre-commit": "^1.2.2",
"snazzy": "^8.0.0",
"standard": "^12.0.1",
Expand Down
18 changes: 1 addition & 17 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
'use strict'

const t = require('tap')
const envSchema = require('../index')
const makeTest = require('./make-test')

process.env.VALUE_FROM_ENV = 'pippo'

function makeTest (t, options, isOk, confExpected, errorMessage) {
t.plan(1)
options = Object.assign({ confKey: 'config' }, options)

try {
const conf = envSchema(options)
t.strictSame(conf, confExpected)
} catch (err) {
if (isOk) {
t.fail(err)
return
}
t.strictSame(err.message, errorMessage)
}
}

const tests = [
{
name: 'empty ok',
Expand Down
27 changes: 27 additions & 0 deletions test/fluent-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const t = require('tap')

if (parseInt(process.versions.node.split('.')[0]) <= 8) {
t.skip('not supported')
} else {
run()
}

function run () {
const S = require('fluent-schema')
const makeTest = require('./make-test')

t.test('simple object - fluent-schema', t => {
const options = {
schema: S.object().prop('PORT', S.string()),
data: {
PORT: '44'
}
}

makeTest(t, options, true, {
PORT: '44'
})
})
}
21 changes: 21 additions & 0 deletions test/make-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const envSchema = require('../index')

function makeTest (t, options, isOk, confExpected, errorMessage) {
t.plan(1)
options = Object.assign({ confKey: 'config' }, options)

try {
const conf = envSchema(options)
t.strictSame(conf, confExpected)
} catch (err) {
if (isOk) {
t.fail(err)
return
}
t.strictSame(err.message, errorMessage)
}
}

module.exports = makeTest

0 comments on commit 1ac9bd9

Please sign in to comment.