Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added contract validation #8

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
203 changes: 203 additions & 0 deletions lib/schema/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "contract.json",

"definitions": {
"baseContract": {
"$id": "baseContract",
"type": "object",
"additionalProperties": false,
"properties": {
"slug": {
"type": "string",
"pattern": "^[a-z0-9-]+$"
},
"version": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a regex here, since we control contract versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I used a regex which includes both fixed version and some of the ranges we support as atm we are using the same version field to validate both cases.

"type": "string",
"minLength": 1
},
"componentVersion": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"pattern": "^[a-z0-9-.]+$"
},
"name": {
"type": "string",
"pattern": "^.*\\S.*$"
},
"aliases": {
"type": "array",
"additionalItems": false,
"uniqueItems": true,
"items": {
"$ref": "#/properties/slug"
}
},
"data": {
"type": "object"
},
"extends": {
"allOf": [
{
"$ref": "#"
},
{
"required": [ "slug", "aliases" ]
}
]
},
"requires": {
"$ref": "contract.json#/definitions/recursiveOR"
},
"tags": {
"type": "array",
"uniqueItems": true,
"additionalItems": false,
"items": {
"type": "string",
"pattern": "^[\\S]+(?: [\\S]+)*$"
}
},
"capabilities": {
"type": "array",
"additionalItems": false,
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"slug": {
"$ref": "#/properties/slug"
},
"componentVersion": {
"$ref": "#/properties/componentVersion"
}
},
"required": [ "slug" ]
}
},
"conflicts": {
"type": "array",
"additionalItems": false,
"items": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have the same structure as requires

"type": "object",
"additionalProperties": false,
"properties": {
"slug": {
"$ref": "#/properties/slug"
},
"version": {
"$ref": "#/properties/version"
}
},
"required": [ "slug" ]
}
},
"assets": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^.*$": {
"type": "object",
"properties": {
"url": {
"type": "string",
"pattern": "^[a-z0-9-]+:\/\/(([a-z0-9-.])+\/)*[a-z0-9-.]+$"
},
"name": {
"type": "string",
"pattern": "[a-z0-9-.]+$"
},
"checksum": {
"type": "string",
"minLength": 1
},
"checksumType": {
"type": "string",
"enum": [ "sha256" ]
}
},
"required": [ "url" ],
"dependencies": {
"checksum": [ "checksumType" ]
}
}
}
},
"children": {
"$id": "childNamespace",
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^.*$": {
"oneOf": [
{
"$ref": "contract.json"
},
{
"$ref": "childNamespace"
}
]
}
}
}
}
},

"partialContract": {
"$id": "partialContract",
"type": "object",
"allOf": [
{
"$ref": "contract.json#/definitions/baseContract"
},
{
"dependencies": {
"version": [ "slug" ]
}
}
]
},

"recursiveOR": {
"$id": "recursiveOR",
"type": "array",
"additionalItems": false,
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"or": {
"$ref": "recursiveOR"
}
}
},
{
"$ref": "contract.json#/definitions/partialContract"
}
]
}
}
},

"allOf": [
{
"$ref": "#/definitions/baseContract"
},
{
"required": [
"slug",
"version",
"type",
"componentVersion",
"aliases",
"tags",
"data"
]
}
]
}
63 changes: 63 additions & 0 deletions lib/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2017 resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict'

const Ajv = require('ajv')
const ajv = new Ajv({
schemaId: '$id'
})
const baseContractSchema = require('./schema/contract.json')
ajv.addSchema(baseContractSchema)

const mergeWithBase = (schema) => {
return {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: schema.$id,
allOf: [
{
$ref: 'contract.json'
},
schema
]
}
}

/**
* @module validation
*/

/**
* @summary Checks if a contract is valid.
* @function
* @memberof module:validation
* @public
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an @example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you define the arguments and return values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*
* }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a weird closing bracket here

*/
exports.checkValidContract = (contract, schema) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe .checkContract() is better since its shorter and the meaning is preserved?

let success = false
if (schema) {
success = ajv.validate(mergeWithBase(schema), contract)
} else {
success = ajv.validate('contract.json', contract)
}

if (success) {
return true
}
throw new Error(ajv.errorsText())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it'd be better to return an object with a success boolean flag or something like that, and an array of potential errors or something like that, so we don't have to unnecessary enclose this in try/catch for certain use cases.

}
57 changes: 33 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading