Skip to content

Commit

Permalink
feat: add validation basics
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Oct 21, 2020
1 parent 70e592f commit 6128b85
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 4 deletions.
34 changes: 31 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
'use strict'

const joi = require('joi')
const path = require('path')

exports.validate = ({ config }) => {
const contents = require(path.join(process.cwd(), '.wiby.json'))
console.log(contents)
const dependentSchema = joi.object({
repository: joi.string().uri({
scheme: [
'https',
'git',
'git+https'
]
})
}).unknown(false)

exports.schema = joi.object({
dependents: joi.array().items(dependentSchema)
}).unknown(false)

exports.validate = ({ config: configFilePath }) => {
if (!configFilePath) {
configFilePath = path.join(process.cwd(), '.wiby.json')
}

configFilePath = path.resolve(configFilePath)

const contents = require(configFilePath)

const result = exports.schema.validate(contents)

if (result.error) {
throw result.error
}

console.log(`✅ ${configFilePath}`)
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@
"nock": "^13.0.3",
"standard": "^14.3.4",
"tap": "^14.10.7"
},
"standard": {
"ignore": [
"test/fixtures/config"
]
}
}
36 changes: 35 additions & 1 deletion test/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
'use strict'

const path = require('path')
const tap = require('tap')
const wiby = require('..')

tap.test('Exists', (tap) => {
const invalidConfigs = {
'fail-bad-json.json': 'Unexpected end of JSON input',
'fail-unknown-root-key.json': '"not-allowed" is not allowed',
'fail-unknown-dependent-key.json': '"dependents[0].sub-key-not-allowed" is not allowed',
'fail-dependent-not-url.json': '"dependents[0].repository" must be a valid uri'
}

const validConfigs = [
'pass-empty.json',
'pass-empty-dependents.json',
'pass-wiby.js'
]

tap.test('should pass with the .wiby.json of the wiby itself', (tap) => {
wiby.validate({})
tap.end()
})

tap.test('Invalid configs', async (tap) => {
for (const [file, expectedError] of Object.entries(invalidConfigs)) {
tap.test(file, (tap) => {
tap.throws(() => {
wiby.validate({ config: path.join(__dirname, 'fixtures', 'config', file) })
}, { message: expectedError })
tap.end()
})
}
})

tap.test('Valid configs', async (tap) => {
for (const file of validConfigs) {
tap.test(file, (tap) => {
wiby.validate({ config: path.join(__dirname, 'fixtures', 'config', file) })
tap.end()
})
}
})
1 change: 1 addition & 0 deletions test/fixtures/config/fail-bad-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"deliberately broken"
7 changes: 7 additions & 0 deletions test/fixtures/config/fail-dependent-not-url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependents": [
{
"repository": "not-a-url"
}
]
}
7 changes: 7 additions & 0 deletions test/fixtures/config/fail-unknown-dependent-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependents": [
{
"sub-key-not-allowed": true
}
]
}
3 changes: 3 additions & 0 deletions test/fixtures/config/fail-unknown-root-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"not-allowed": true
}
3 changes: 3 additions & 0 deletions test/fixtures/config/pass-empty-dependents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependents": []
}
1 change: 1 addition & 0 deletions test/fixtures/config/pass-empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/fixtures/config/pass-wiby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../../.wiby.json')

0 comments on commit 6128b85

Please sign in to comment.