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
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
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
exports.Contract = require('./contract')
exports.Blueprint = require('./blueprint')
exports.buildTemplate = require('./partials').buildTemplate
exports.checkContract = require('./validation').checkContract

exports.query = (universe, layout, skeleton) => {
const blueprint = new exports.Blueprint(layout, skeleton)
Expand Down
190 changes: 190 additions & 0 deletions lib/schema/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{
"$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,
"pattern": "^[~\\^]*\\d+(\\.[~\\^]*\\d+(\\.[~\\^]*\\d+)*)*$"
},
"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": {
"$ref": "contract.json#/definitions/recursiveOR"
},
"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"
]
}
]
}
96 changes: 96 additions & 0 deletions lib/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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)

// eslint-disable-next-line jsdoc/require-example
/**
* @summary Merge supplied schema with base contract schema
* @function
* @private
*
* @param {Object} schema - JSON schema
* @returns {Object} - Merged schema
*
*/
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

*
* @param {Object} contract - the contract to validate
* @param {Object} schema - optional schema
* @returns {Object} result
*
* @description
* This function will validate that the supplied contract is valid according to
* the json schema specification. Accepts an optional schema that can reference and extend
* the base schema. If a custom schema is passed, both the generic and custom validation
* rules will apply
*
* @example
* validation.checkContract({
* slug: 'slug',
* type: 'type',
* version: '1',
* componentVersion: 'componentVersion',
* aliases: [],
* data: {},
* tags: []
* }
*/
exports.checkContract = (contract, schema) => {
let success = false
if (schema) {
success = ajv.validate(mergeWithBase(schema), contract)
} else {
success = ajv.validate('contract.json', contract)
}

if (success) {
return {
success: true, errors: []
}
}
return {
success: false, errors: [ ajv.errorsText() ]
}
}
Loading