Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Validate configuration against a JSON schema #88

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"type": "object",
"required": [
"licenses"
],
"properties": {
"licenses": {
"type": "object",
"properties": {
"blueOak": {
"enum": [
"lead",
"bronze",
"silver",
"gold"
]
},
"spdx": {
"type": "array",
"items": {
"$ref": "#/definitions/nonEmptyString"
}
},
"osi": {
"type": "boolean"
}
},
"additionalProperties": false
},
"packages": {
"type": "object",
"patternProperties": {
".+": {
"$ref": "#/definitions/nonEmptyString"
}
}
}
},
"additionalProperties": false,
"definitions": {
"nonEmptyString": {
"type": "string",
"minLength": 1
}
}
}
34 changes: 11 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var satisfies = require('semver').satisfies
var spdxAllowed = require('spdx-whitelisted')

function licensee (configuration, path, callback) {
if (!validConfiguration(configuration)) {
var errors = configurationErrors(configuration)
if (errors) {
return callback(new Error('Invalid configuration'))
}
configuration.licenses = compileLicenseAllowlist(configuration)
Expand Down Expand Up @@ -51,29 +52,16 @@ function licensee (configuration, path, callback) {
}
}

function validConfiguration (configuration) {
return (
isObject(configuration) &&
has(configuration, 'licenses') &&
isObject(configuration.licenses) &&
has(configuration, 'packages')
? (
// Validate `packages` property.
isObject(configuration.packages) &&
Object.keys(configuration.packages)
.every(function (key) {
return isString(configuration.packages[key])
})
) : true
)
}
var Ajv = require('ajv')
var ajv = new Ajv({ allErrors: true })
var validate = ajv.compile(require('./configuration-schema'))

function isObject (argument) {
return argument && typeof argument === 'object'
}

function isString (argument) {
return typeof argument === 'string'
function configurationErrors (configuration) {
if (validate(configuration)) {
return false
} else {
return validate.errors
}
}

function findIssues (configuration, dependencies) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@blueoak/list": "^9.0.0",
"@npmcli/arborist": "^6.1.2",
"ajv": "^8.11.2",
"correct-license-metadata": "^1.4.0",
"docopt": "^0.6.2",
"has": "^1.0.3",
Expand Down
33 changes: 33 additions & 0 deletions tests/schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var tap = require('tap')
var Ajv = require('ajv')
var schema = require('../configuration-schema')

var ajv = new Ajv({ allErrors: true })
var validate = ajv.compile(schema)

tap.test('configuration schema', function (test) {
ajv.validateSchema(schema)
test.same(ajv.errors, null, 'no schema errors')
test.end()
})

tap.test('valid configuration', function (test) {
test.ok(
validate({
licenses: { osi: true, blueOak: 'silver' },
packages: { licensee: '1.0.0' }
}),
'valid configuration validates'
)
test.end()
})

tap.test('invalid configuration', function (test) {
test.notOk(
validate({
licenses: { blueoak: 'silver' }
}),
'configuration with lower-case "blueoak" fails'
)
test.end()
})