Skip to content

Commit

Permalink
chore(semantic-release): Use semantic release
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The code has been refactord to take advantage of
es6 features only available in node6 or higher.
  • Loading branch information
elliotttf committed Oct 25, 2016
1 parent 5127c59 commit 3e71703
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 173 deletions.
75 changes: 0 additions & 75 deletions .eslintrc

This file was deleted.

28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "script"
},
"extends": "airbnb-base",
"rules": {
"brace-style": ["error", "stroustrup"],
"max-len": ["warn", {
"code": 100,
"comments": 80,
"ignoreUrls": true,
"ignorePattern": "(logger\\.|new Error\\)|new TypeError\\()",
"ignoreTrailingComments": true,
"tabWidth": 2
}],
"no-warning-comments": "warn",
"quotes": ["error", "single", {"avoidEscape": true, "allowTemplateLiterals": true}],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}],
"valid-jsdoc": "error"
}
}
25 changes: 21 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
sudo: false
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- "0.12"
- "4"
- "node"
- '6'
- 'node'
before_install:
- npm i -g npm@^2.0.0
before_script:
- npm prune
script: npm run lint && npm t
after_success: npm run coveralls
after_success:
- npm run coveralls
- 'curl -Lo travis_after_all.py https://git.io/travis_after_all'
- python travis_after_all.py
- export $(cat .to_export_back) &> /dev/null
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
33 changes: 17 additions & 16 deletions bin/jsonapi-validator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#! /usr/bin/env node

var fs = require('fs');
var path = require('path');
var Validator = require('../').Validator;
/* eslint-disable no-console */

var argv = require('yargs')
'use strict';

const fs = require('fs');
const path = require('path');
const Validator = require('../').Validator;

const argv = require('yargs')
.usage('Usage: $0 -f [file] [-q]')
.demand(['f'])
.alias('f', 'file')
Expand All @@ -17,30 +21,27 @@ var argv = require('yargs')
.alias('h', 'help')
.argv;

var v = new Validator();
var file = require(path.resolve(argv.f));
const v = new Validator();

try {
const file = JSON.parse(fs.readFileSync(path.resolve(argv.f)));
v.validate(file);
}
catch (e) {
if (!argv.q) {
console.error(e.message);
e.errors.forEach(function (message) {
console.error(' ' + message.message + '.');
console.error(' schemaPath: ' + message.schemaPath);
for (param in message.params) {
if (!message.params.hasOwnProperty(param)) {
return;
}
console.error(' ' + param + ': ' + message.params[param]);
}
e.errors.forEach((message) => {
console.error(` ${message.message}.`);
console.error(` schemaPath: ${message.schemaPath}`);
Object.keys(message.params).forEach(param => console.error(
` ${param}: ${message.params[param]}`
));
});
}
process.exit(1);
}

if (!argv.q) {
console.log(argv.f + ' is valid JSON API.');
console.log(`${argv.f} is valid JSON API.`);
}

103 changes: 56 additions & 47 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,69 @@
var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true, verbose: true})
'use strict';

/**
* Validator class with an optional JSON API schema.
*
* @param {*} schema
* (optional) An object, or path relative to __dirname for a valid JSON API
* schema. If one is not provided ./schema.json will be used.
*/
var Validator = function (schema) {
if (typeof schema === 'undefined') {
schema = require('./schema.json');
}
const defaultSchema = require('./schema.json');
const Ajv = require('ajv');

this._validate = ajv.compile(schema);
};
const ajv = new Ajv({ allErrors: true, verbose: true });

/**
* Validation method.
*
* @param {object} doc
* A JavaScript object to validate against JSON API.
*
* @throws {Error}
* Thrown if the document is not valid json. An additional element, errors,
* is added to the error object that lists the problems encountered.
* Validator class with an optional JSON API schema.
* @class
*/
Validator.prototype.validate = function (doc) {
var valid = this._validate(doc);
if (!valid) {
var e = new Error('Invalid JSON API.');
e.errors = this._validate.errors;
throw e;
class Validator {
/**
* @constructor
*
* @param {*} schema
* (optional) An object, or path relative to __dirname for a valid JSON API
* schema. If one is not provided ./schema.json will be used.
*/
constructor(schema = defaultSchema) {
this.validator = ajv.compile(schema);
}
};

/**
* Boolean validation method.
*
* @param {object} doc
* A JavaScript object to validate against JSON API.
*
* @return {boolean}
* true if the document is valid, else false.
*/
Validator.prototype.isValid = function (doc) {
try {
this.validate(doc);
} catch (e) {
return false;
/**
* Validation method.
*
* @param {object} doc
* A JavaScript object to validate against JSON API.
*
* @return {undefined}
*
* @throws {Error}
* Thrown if the document is not valid json. An additional element, errors,
* is added to the error object that lists the problems encountered.
*/
validate(doc) {
const valid = this.validator(doc);
if (!valid) {
const e = new Error('Invalid JSON API.');
e.errors = this.validator.errors;
throw e;
}
}

return true;
};
/**
* Boolean validation method.
*
* @param {object} doc
* A JavaScript object to validate against JSON API.
*
* @return {boolean}
* true if the document is valid, else false.
*/
isValid(doc) {
try {
this.validate(doc);
}
catch (e) {
return false;
}

return true;
}
}

module.exports = {
Validator: Validator
Validator,
};

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "jsonapi-validator",
"version": "2.2.0",
"description": "JSON API validation module.",
"main": "lib/validator.js",
"bin": {
Expand All @@ -10,11 +9,12 @@
"test": "istanbul cover --print both nodeunit ./test",
"lint": "eslint .",
"coverage": "istanbul check-coverage --statements 100 --lines 100 --branches 100 --functions 100",
"coveralls": "cat ./coverage/lcov.info | coveralls"
"coveralls": "cat ./coverage/lcov.info | coveralls",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "git+https://github.com/elliotttf/jsonapi-validator.git"
"url": "https://github.com/elliotttf/jsonapi-validator.git"
},
"keywords": [
"json",
Expand All @@ -29,10 +29,13 @@
"homepage": "https://github.com/elliotttf/jsonapi-validator#readme",
"devDependencies": {
"coveralls": "^2.11.9",
"eslint": "^2.13.1 <3",
"eslint": "^3.8.1",
"eslint-config-airbnb-base": "^9.0.0",
"eslint-plugin-import": "^2.0.1",
"ghooks": "^1.2.4",
"istanbul": "^0.4.4",
"nodeunit": "^0.10.2"
"nodeunit": "^0.10.2",
"semantic-release": "^4.3.5"
},
"config": {
"ghooks": {
Expand All @@ -45,6 +48,6 @@
"yargs": "^5.0.0"
},
"engines": {
"node": ">=0.12"
"node": ">=6"
}
}
Loading

0 comments on commit 3e71703

Please sign in to comment.