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

Added contract validation #8

wants to merge 13 commits into from

Conversation

nazrhom
Copy link
Contributor

@nazrhom nazrhom commented May 17, 2018

Added checkValidContract function.
It takes a contract and an optional schema. If no schema is supplied it will validate the contract against the base contract schema. If the schema is supplied it will validate against both schemas. The user supplied schema can reference the base contract schema via { $ref: 'contract.json' }
The function will throw if the contract is not valid

Fixes #7
Change-type: patch

@nazrhom nazrhom requested review from jviotti and nghiant2710 May 17, 2018 17:09
@nazrhom nazrhom changed the title Added validation contract validation Added contract validation May 17, 2018
Copy link
Contributor

@jviotti jviotti left a comment

Choose a reason for hiding this comment

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

I added some comments but overall looks pretty good

* @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.

There is a weird closing bracket here

* @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

*
* }
*/
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?

* @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 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

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.

tags: []
}

ava.test('should validate base contract', (test) => {
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 create a JSON file in ie test/contracts.js that includes an array of potential contracts and their expected results, and then this file can require that, iterate through the test cases, and generate the ava.test calls over it?

"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.

"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

@nazrhom nazrhom requested a review from jviotti May 28, 2018 09:20
)
})

ava.test('Should reject invald tagged contract', (test) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo in "invald" (same in test below)

ava.test('should validate require contract', (test) => {
test.deepEqual(
{
success: true, errors: []
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 put keys in different lines?

@@ -0,0 +1,111 @@
/*
* Copyright 2017 resin.io
Copy link
Contributor

Choose a reason for hiding this comment

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

2018?

lib/index.js Outdated
@@ -24,6 +24,7 @@
exports.Contract = require('./contract')
exports.Blueprint = require('./blueprint')
exports.buildTemplate = require('./partials').buildTemplate
exports.validation = require('./validation')
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 just expose a top level .isValid(contract) function or something like that.

@nazrhom nazrhom requested a review from jviotti June 1, 2018 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants