-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
d448bb9
9573fea
dfec441
7a27421
5f80170
9956ce0
43960b6
8ac4991
229b094
2f19cbf
dfb0c61
de6aacf
c70ed99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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": { | ||
"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": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have the same structure as |
||
"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" | ||
] | ||
} | ||
] | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you define the arguments and return values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
* | ||
* } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a weird closing bracket here |
||
*/ | ||
exports.checkValidContract = (contract, schema) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.